【问题标题】:How to take a screenshot and send by email programaticly on dotnet如何在 dotnet 上以编程方式截取屏幕截图并通过电子邮件发送
【发布时间】:2009-07-03 16:36:25
【问题描述】:

背景:

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

对于这种异常,我们现在提供了一个带有错误详细信息的漂亮对话框,用户可以截屏,并通过电子邮件发送一些备注。

问题:

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

问题:

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

备注:

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

【问题讨论】:

    标签: .net .net-2.0 outlook screenshot attachment


    【解决方案1】:

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

    //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描述的方法

    【讨论】:

    • 虽然这不是更简洁的代码,但您在这里为我指明了正确的方向。那篇文章本身并不是很有帮助,但多亏了它,我在 mskb is.gd/1myNS 中找到了另一篇文章,解释了如何截取保存的屏幕截图并将其添加为附件。
    【解决方案2】:

    以下代码将执行您问题的截图:

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

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

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

    【讨论】:

      猜你喜欢
      • 2014-04-17
      • 1970-01-01
      • 2011-05-04
      • 2011-01-13
      • 1970-01-01
      • 2011-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多