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

后端 未结 2 1819
栀梦
栀梦 2021-01-03 15:19

Background:

I\'m developing a bussiness application, and in the final stages we are encountering some extrange errors, mostly with connection and so

2条回答
  •  醉梦人生
    2021-01-03 16:13

    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.

提交回复
热议问题