如何拍摄截图并通过电子邮件发送到dotnet上

时间:2020-12-31 19:18:43

Background:

I'm developing a bussiness application, and in the final stages we are encountering some extrange errors, mostly with connection and some edge use cases.

我正在开发一个业务应用程序,在最后阶段我们遇到一些额外的错误,主要是连接和一些边缘用例。

For this kind of exceptions, we now provide a nice dialog with error details, of which the user take a screenshot, and send by email with some remarks.

对于这种异常,我们现在提供一个包含错误详细信息的精彩对话框,用户可以截取屏幕截图,并通过电子邮件发送一些评论。

Problem:

I would like to provide a better experience, and provide a single button in the same dialog, wich uppon click, would open outlook and prepare the email, with a screenshot as attachment and maybe a log file, then the user can add remarks and press the send button.

我想提供更好的体验,并在同一个对话框中提供单个按钮,点击,打开Outlook并准备电子邮件,屏幕截图作为附件,也许是日志文件,然后用户可以添加备注并按发送按钮。

Question:

How can I take this screenshot programaticly, and then add it as attachment in a outlook mail?

如何以编程方式截取此屏幕截图,然后将其作为附件添加到Outlook邮件中?

Remarks:

The app is in Microsoft .Net Framework 2.0, C# or VB

该应用程序位于Microsoft .Net Framework 2.0,C#或VB中

2 个解决方案

#1


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

要通过outlook发送邮件,您可以使用此处描述的方法

#2


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.

这将返回包含主屏幕截​​图的字节数组。如果您需要处理多个监视器,那么您还需要查看Screen的AllScreens属性。

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.

像这样的库可以处理所有未处理的异常,截取屏幕截图和发送电子邮件等等,但他们很可能会尝试自己发送屏幕截图,而不是将其附加到新的Outlook电子邮件中。

#1


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

要通过outlook发送邮件,您可以使用此处描述的方法

#2


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.

这将返回包含主屏幕截​​图的字节数组。如果您需要处理多个监视器,那么您还需要查看Screen的AllScreens属性。

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.

像这样的库可以处理所有未处理的异常,截取屏幕截图和发送电子邮件等等,但他们很可能会尝试自己发送屏幕截图,而不是将其附加到新的Outlook电子邮件中。