Image in MSG document is not rendered correctly

I use the following code to convert an MSG file to a bitmap:

var pageImage = new Bitmap(816, 1056);
pageImage.SetResolution(96, 96);
using(var gfx = Graphics.FromImage(pageImage))
{
    gfx.Clear(Color.White);
    document.RenderToScale(0, gfx, 0, 0, (float) 1);
}

The MSG contains an image and this image is not rendered correctly to the resulting bitmap. Instead of the image I see a red cross.
The attachment (Testmail.zip) contains the original Testmail.msg and a screenshot of the result I get.

I tested this with the latest version of Aspose.Words(13.6 .0 .0)

Can you please take a look at this?

Thanks,

Corne Los

Hi Corne,

Thanks for your inquiry. What I understand, you’re first converting ‘Testmail.msg’ file to Mhtml format by using Aspose.Email and then rendering image from that intermediate Mhtml file to Png format by using Aspose.Words. If this is found to be the case, please try run the following code snippet to achieve what you’re looking for.

Aspose.Email.Mail.MailMessage aeDoc = Aspose.Email.Mail.MailMessage.Load(@"C:\Temp\testMail\Testmail.msg", Aspose.Email.Mail.MessageFormat.Msg);
aeDoc.Save(@"C:\Temp\testMail\outAE.mhtml");
Document doc = new Document(@"C:\Temp\testMail\outAE.mhtml");
PageInfo pageInfo = doc.GetPageInfo(0);
// Let's say we want the image at 50% zoom.
const float MyScale = 0.50 f;
// Let's say we want the image at this resolution.
const float MyResolution = 200.0 f;
Size pageSize = pageInfo.GetSizeInPixels(MyScale, MyResolution);
using(Bitmap img = new Bitmap(pageSize.Width, pageSize.Height))
{
    img.SetResolution(MyResolution, MyResolution);
    using(Graphics gr = Graphics.FromImage(img))
    {
        gr.Clear(Color.White);
        // You can apply various settings to the Graphics object.
        gr.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
        // Fill the page background.
        gr.FillRectangle(Brushes.White, 0, 0, pageSize.Width, pageSize.Height);
        // Render the page using the zoom.
        doc.RenderToScale(0, gr, 0, 0, MyScale);
    }
    img.Save(@"C:\Temp\testMail\out.png");
}

I hope, this helps.

Best regards,

Hi Awais,

Thank you for your answer, your code example was very useful. I added the option: MailMessageSaveOptions.None to make sure only the mail message body is converted ta a bitmap.

There is however one problem: the image in the mail does not fit on the page. Please take a look at result.png I attached. It seems that the resulting bitmap is not wide enough causing the right part of the image in the mail to be cut off…

As an experiment I tried this to make the bitmap 500px wider:

Size imgSize = pageInfo.GetSizeInPixels(scale / 100 f, dpi);
imgSize = new Size(imgSize.Width + 500, imgSize.Height);

But then I just get a white border at the right side.

Do you know how I can fix this?

Hi Corne,

Thanks for your inquiry. You can resize a wider image to fit it inside the page bounds by adding the following code to the code mentioned in my previous post:


Document doc = new Document(@"C:\Temp\testmail\outAE.mhtml");
PageSetup ps = doc.FirstSection.PageSetup;
double effectiveWidth = ps.PageWidth - (ps.LeftMargin + ps.RightMargin);
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);
foreach(Shape shape in shapes)
    shape.Width = (shape.Width> effectiveWidth) ? effectiveWidth : shape.Width;
PageInfo pageInfo = doc.GetPageInfo(0);


I hope, this helps.

Best regards,

Hi Awais,

This helped certainly!

I changed your code a little bit to make sure the shapes are resized while their width/height ratio stays the same:

PageSetup ps = document.FirstSection.PageSetup;
double effectiveWidth = ps.PageWidth - (ps.LeftMargin + ps.RightMargin);
NodeCollection shapes = document.GetChildNodes(NodeType.Shape, true);
foreach(Shape shape in shapes)
if (shape.Width> effectiveWidth)
{
    var shrinkRatio = effectiveWidth / shape.Width;
    shape.Width = effectiveWidth;
    shape.Height *= shrinkRatio;
}

Problem solved, thanks for your help.

Hi Corne,

Thanks for your feedback. In case you have further inquires or need any help, please let us know. We are always glad to help you.

Best regards,