0

再会,

如何减少/限制图像的大小作为 ac# 电子邮件中的内联附件?电子邮件中显示的图像非常大,我想将其缩小到 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);
    }

正如网站所见,Outlook 2007 不再支持max-width和css 样式。max-height图像从磁盘读取,作为附件添加,并提供与 html 页面上的内容 ID 占位符图像标签匹配的内容 ID。图像不会调整到更小的比例,并且......它通过让页面上的其他元素感觉非常......非常小来吓唬页面上的其他元素。

我该如何克服呢?

4

2 回答 2

2

我已经处理了这个问题。真正的问题是您在 Outlook中使用的不是嵌入式 IE ,而是Word 渲染引擎。它比 IE6 和 IE7 加起来有更多的怪癖。我通过缩放服务器中的图像并使用它解决了这个问题。

于 2014-07-31T09:24:02.727 回答
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;
    }
}
于 2014-07-31T11:41:30.437 回答