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);
    }

このサイトで見られるように、max-widthおよびmax-heightcss スタイルは Outlook 2007 以降ではサポートされなくなりました。画像はディスクから読み取られ、添付ファイルとして追加され、html ページのコンテンツ ID プレースホルダー画像タグと一致するコンテンツ ID が与えられます。画像は小さい比率にサイズ変更されず、... ページ上の他の要素を本当に... 本当に小さく感じさせることで怖がらせます.

どうすればこれを克服できますか?

4

2 に答える 2

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 に答える