简体   繁体   中英

Interop Outlook - How to add bitmap into mail body/attachment

I have a button in my application which takes a screenshot of the current window and I try to add the bitmap result into an Outlook mail.

I have created the bitmap of my application with:

    FrameworkElement element = (FrameworkElement)o;
    double width = element.ActualWidth;
    double height = element.ActualHeight;
    RenderTargetBitmap bmpCopied = new RenderTargetBitmap((int)Math.Round(width), (int)Math.Round(height), 96, 96, PixelFormats.Default);
    DrawingVisual dv = new DrawingVisual();
    using (DrawingContext dc = dv.RenderOpen())
    {
        VisualBrush vb = new VisualBrush(element);
        dc.DrawRectangle(vb, null, new Rect(new Point(), new Size(width, height)));
    }
    bmpCopied.Render(dv);
    Clipboard.SetImage(bmpCopied);

For create the mail, I use the dll Interop.Outlook where I manage my new mail:

    Microsoft.Office.Interop.Outlook.Application outlookApp = new Microsoft.Office.Interop.Outlook.Application();
    Microsoft.Office.Interop.Outlook._MailItem mailItem = (Microsoft.Office.Interop.Outlook._MailItem)outlookApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
    mailItem.Attachments.Add(Clipboard.GetImage());

    mailItem.Display(true);

However, the Attachements.Add() doesn't work with the bitmap and the command crashed without any explanation...

Is there a way to do it without create a temporary file on the computer to attach it to the mail?

From the documentation , for the parameter Source when adding Attachments:

The source of the attachment. This can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment.

It would appear this isn't possible without saving the image as a file.

There is a similar question ( Embedding picture to outlook email body ) which provides a method for attaching an image inline from a file which should help you if saving the image to disk is an option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM