C#在应用程序中获取.net控件的ScreenShot并附加到Outlook Email

有没有人知道如何使用C#截取屏幕截图并限制它拍摄特定容器/应用程序的一部分。 我不想要应用程序的整个屏幕或整个窗口。

简单地调用我的面板:panel1用户将单击“按钮”并获取panel1的屏幕截图并附加到电子邮件。

我想仅拍摄该部分的屏幕截图,然后在本地保存到C:\ Drive和/或附加或嵌入到outlook电子邮件中。

我在互联网上阅读了其他各种各样的东西,但大多数都不得不处理创建复杂的变化,拍摄我不想要的网页浏览器控件的屏幕截图。

我用这样的东西做这个

public static void TakeCroppedScreenShot( string fileName, int x, int y, int width, int height, ImageFormat format) { Rectangle r = new Rectangle(x, y, width, height); Bitmap bmp = new Bitmap(r.Width, r.Height, PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(bmp); g.CopyFromScreen(r.Left, r.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy); bmp.Save(fileName, format); } 

我希望这有帮助

如果您只想使用Panel's屏幕截图,则可以使用内置的DrawToBitmap方法。

 Bitmap bmp = new Bitmap(myPanel.Width, myPanel.Height); myPanel.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height)); bmp.Save(@"C:\MyPanelImage.bmp"); 

请注意,某些控件可能无法使用此function,例如WebBrowserRichTextBox控件,但它应该适用于大多数其他控件(文本框,标签等..)

对Killercam的回答略有修改:

  public static Bitmap takeComponentScreenShot(Control control) { // find absolute position of the control in the screen. Control ctrl = control; Rectangle rect = new Rectangle(Point.Empty, ctrl.Size); do { rect.Offset(ctrl.Location); ctrl = ctrl.Parent; } while (ctrl != null); Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(bmp); g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy); return bmp; } 

此方法将立即截取屏幕截图。 例如,如果在调用此函数之前更改面板内按钮的可见性,则位图将包含该按钮。 如果不需要,请使用keyboardP的答案 。

Asif答案的改进:

 public static Bitmap takeComponentScreenShot(Control control) { // find absolute position of the control in the screen. Rectangle rect=control.RectangleToScreen(control.Bounds); Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(bmp); g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy); return bmp; } 

我发现这个将控件保存为位图:

 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Button1_Click(object sender, EventArgs e) { SaveAsBitmap(panel1,"C:\\path\\to\\your\\outputfile.bmp"); } public void SaveAsBitmap(Control control, string fileName) { //get the instance of the graphics from the control Graphics g = control.CreateGraphics(); //new bitmap object to save the image Bitmap bmp = new Bitmap(control.Width, control.Height); //Drawing control to the bitmap control.DrawToBitmap(bmp, new Rectangle(0, 0, control.Width, control.Height)); bmp.Save(fileName); bmp.Dispose(); } } 

我在这里发现了一些关于outlook的东西 ,但是我无法测试它,因为我的PC上没有安装outlook。