代码之家  ›  专栏  ›  技术社区  ›  David Lay

如何在dotnet上截图并通过电子邮件编程发送

  •  2
  • David Lay  · 技术社区  · 17 年前

    背景:

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

    对于这类异常,我们现在提供了一个很好的对话框,其中包含错误详细信息,用户可以截图,并通过电子邮件发送一些备注。

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

    问题:

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

    该应用程序采用Microsoft.Net Framework 2.0、C#或VB

    2 回复  |  直到 17 年前
        1
  •  6
  •   dutchflyboy    17 年前

    首先,要发送屏幕截图,可以使用以下代码:

    //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);
    

    要通过outlook发送邮件,可以使用下面介绍的方法 here

        2
  •  6
  •   adrianbanks    17 年前

    以下代码将执行问题的屏幕截图部分:

    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; }

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

    this 你可以处理所有未处理的异常,拍摄截图并通过电子邮件发送,等等,但他们很可能会尝试自己发送截图,而不是将其附加到新的Outlook电子邮件中。

    推荐文章