美好的一天,
如何在c#电子邮件中将图像的大小缩小/限制为内联附件?电子邮件中显示的图像实际上是巨大的,我想将其缩小到大约475px x 475px。
HTML:
...
<td style="max-width: 475px; max-height: 475px">
<img style="width:475px; height: 475px;" id="Img1" src="cid:Product" />
</td>
<td style="width: 475px">
<div class="jamHeader">
<img id="jamHeaderImage" src="cid:Header" />
</div>
<div class="labelContainer">
<h1 class="title-block">
<p id="SoftwareName">"xxxxxxxxxx"</p>
</h1>
<div class="productInfo">
<div id="EmailDescription">
xxxxxxxxxx
This link expires in 24 hours if not redeemed."
</div>
</div>
</div>
</td>
...
附加图片的代码
if (!string.IsNullOrEmpty(productImage))
{
System.Net.Mail.Attachment product = new System.Net.Mail.Attachment(productImage);
product.ContentId = "Product";
product.ContentDisposition.Inline = true;
product.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
message.Attachments.Add(product);
}
如this网站所示,Outlook 2007中不再支持max-width
和max-height
css样式。从磁盘读取图像,作为附件添加,并给出与html页面上的内容ID占位符图像标记匹配的内容ID。图像不会调整到较小的比例,并且......它会让页面上的其他元素感觉真的很小......它们会吓到它们。
我怎样才能克服这个?
答案 0 :(得分:2)
我已经解决了这个问题。真正的问题是你在Outlook中使用的不是嵌入式IE ,而是 Word渲染引擎。它有更多的怪癖,然后IE6和IE7结合起来。我通过缩放服务器中的图像并使用它解决了这个问题。
答案 1 :(得分:0)
巨大的解决方法,但这是对@Margus的回应和回答。
在服务器上缩放图像:我使用的代码 -
private void ResizeImage(string path)
{
var image = System.Drawing.Image.FromFile(path);
var widthAndHeightMax = 375D; //375px by 375 px.
var resizeImagePath = string.Concat(path.Substring(0, path.LastIndexOf('\\')), "\\Resized");
var newImageName = path.Remove(0, path.LastIndexOf('\\') + 1);
var newImageFullPath = string.Concat(resizeImagePath, "\\", newImageName);
if (image.Width > widthAndHeightMax || image.Height > widthAndHeightMax || !File.Exists(newImageFullPath))
{
//Get Image Ratio
var ratioX = widthAndHeightMax / image.Width;
var ratioY = widthAndHeightMax / image.Height;
var ratio = Math.Min(ratioX, ratioY);
var newWidth = (int)(image.Width * ratio);
var newHeight = (int)(image.Height * ratio);
var newImage = new System.Drawing.Bitmap(newWidth, newHeight);
var resizedImage = System.Drawing.Graphics.FromImage(newImage);
resizedImage.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
resizedImage.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
resizedImage.DrawImage(image, 0, 0, newWidth, newHeight);
if (!Directory.Exists(resizeImagePath))
{
Directory.CreateDirectory(resizeImagePath);
}
// Get an ImageCodecInfo object that represents the JPEG codec.
var imageCodecInfo = ImageCodecInfo.GetImageDecoders().SingleOrDefault(c => c.FormatID == ImageFormat.Jpeg.Guid);
// Create an Encoder object for the Quality parameter.
var encoder = Encoder.Quality;
// Create an EncoderParameters object.
var encoderParameters = new EncoderParameters(1);
// Save the image as a JPEG file with quality level.
var encoderParameter = new EncoderParameter(encoder, 100L);
encoderParameters.Param[0] = encoderParameter;
//newImage.Save(newImageFullPath, imageCodecInfo, encoderParameters); throws GDI+ general error. normally security related
using (var ms = new MemoryStream())
{
try
{
using (var fs = new FileStream(string.Concat(resizeImagePath, "\\", newImageName), FileMode.Create, FileAccess.ReadWrite))
{
newImage.Save(ms, imageCodecInfo, encoderParameters);
var bytes = ms.ToArray();
fs.Write(bytes, 0, bytes.Length);
}
}
catch
{
//If the image exists, it may already be used by another process (which means it exists)
}
}
productImage = newImageFullPath;
}
}