How to take a screenshot and send by email programaticly on dotnet

坚强是说给别人听的谎言 提交于 2019-11-30 16:21:38

First of all, to send the screenshot, you can use the following code:

//Will contain screenshot
Bitmap screenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
Graphics screenshotGraphics = Graphics.FromImage(bmpScreenshot);
//Make the screenshot
screenshotGraphics.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
screenshot.save("a place to temporarily save the file", ImageFormat.Png);

To send the mail through outlook, you could use the method described here

The following code will perform the screenshot bit of your question:

public byte[] TakeScreenshot()
{
    byte[] bytes;
    Rectangle bounds = Screen.PrimaryScreen.Bounds;
using (Bitmap bmp = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb)) { using (Graphics gfx = Graphics.FromImage(bmp)) { gfx.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy);
using (MemoryStream ms = new MemoryStream()) { bmp.Save(ms, ImageFormat.Jpeg); bytes = ms.ToArray(); } } }
return bytes; }

This will return a byte array containing the screenshot of the main screen. If you need to deal with multiple monitors, then you need to also look at the AllScreens property of Screen.

Libraries like this one can handle all of your unhandled exceptions, take screenshots and email them, and much more, but they will most likely try to send the screenshot themselves instead of attaching it to a new Outlook email.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!