-6

我认为我们可以通过执行类似这样的代码创建一个文本图像并将其转换为 jpeg 文件,但是如何将此图像嵌入到邮件和发送中。感谢任何帮助

string Text = HttpContext.Current.Request.QueryString["Text"]; 
Color FontColor = Color.Blue; 
Color BackColor = Color.White; 
String FontName = "Times New Roman"; 
int FontSize = 10; 
int Height = 150; 
int Width = 150;

Bitmap bitmap = new Bitmap(Width, Height); 
Graphics graphics = Graphics.FromImage(bitmap); 
Color color = Color.Gray; 
System.Drawing.Font font = new System.Drawing.Font(FontName, FontSize);
PointF point = new PointF(5.0F, 5.0F); 

SolidBrush BrushForeColor = new SolidBrush(FontColor);
SolidBrush BrushBackColor = new SolidBrush(BackColor);
Pen BorderPen = new Pen(color); 

System.Drawing.Rectangle displayRectangle = new System.Drawing.Rectangle(new Point(0, 0), new Size(Width - 1, Height - 1));
graphics.FillRectangle(BrushBackColor, displayRectangle);
graphics.DrawRectangle(BorderPen, displayRectangle);
StringFormat format1 = new StringFormat(StringFormatFlags.NoClip);
StringFormat format2 = new StringFormat(format1);
graphics.DrawString(Text, font, Brushes.Red, (RectangleF)displayRectangle, format2);

HttpContext.Current.Response.ContentType = "image/jpeg";
bitmap.Save(HttpContext.Current.Response.OutputStream, ImageFormat.Jpeg);
4

1 回答 1

2

下面的代码示例跳过了创建消息并添加主题、正文和地址。它显示了用于嵌入最初存储在字节数组中的图像的代码。关键是将您的图像放入内存流中。

//... other System.Net.Mail.MailMessage creation code
// CustomerSignature is a byte array containing the image
System.IO.MemoryStream ms = new System.IO.MemoryStream(CustomerSignature);
System.Net.Mime.ContentType contentType = new System.Net.Mime.ContentType();
contentType.MediaType = System.Net.Mime.MediaTypeNames.Image.Jpeg;
contentType.Name = "signature.jpg";
System.Net.Mail.Attachment imageAttachment = new System.Net.Mail.Attachment(ms, contentType);
mailMessage.Attachments.Add(imageAttachment);
System.Net.Mail.LinkedResource signature = new System.Net.Mail.LinkedResource(ms, "image/jpeg");
signature.ContentId = "CustomerSignature";
System.Net.Mail.AlternateView aView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(mailMessage.Body, new System.Net.Mime.ContentType("text/html"));
aView.LinkedResources.Add(signature);
mailMessage.AlternateViews.Add(aView);

我遇到了嵌入图像在某些电子邮件程序中显示的问题,而在其他程序中则没有。我修改了创建链接资源的行,以及创建新 AlternativeView 和图像的行,现在可以在更广泛的程序中查看。

于 2012-08-10T18:28:07.567 回答