可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Does anyone know how to take a screenshot using C# and limit it to take a picture of a specific container/portion of an application. I do not want the whole screen or whole window of the application.
My Panel is simply called: panel1
User would click a "button" and take screenshot of panel1 and attach to email.
I would like to take a screen shot of that section only and then save locally to the C:\ Drive and/or attach or embed into an outlook email.
I read other various things on the internet but most of them had to deal with creating complex changes in take a screenshot of a web browser control which I am not looking for.
回答1:
I do this using something like
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);
}
I hope this helps
回答2:
If you just want to the Panel's
screenshot, you can use the built-in DrawToBitmap method.
Bitmap bmp = new Bitmap(myPanel.Width, myPanel.Height);
myPanel.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
bmp.Save(@"C:\MyPanelImage.bmp");
Just note that some controls may not work with this functionality such as the WebBrowser
and RichTextBox
controls but it should work for most other controls (textbox, labels etc..)
回答3:
Slight modification of Killercam's answer:
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;
}
This method will take screenshot immediately. For example, if you change visibility of a button within the panel just before calling this function, the bitmap will contain the button. If this is not desired, use keyboardP's answer.
回答4:
An improvement to Asif´s answer:
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;
}
回答5:
I've found this to save a control as bitmap:
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();
}
}
I've found something about outlook here, but I couldn't test it, because I don't have outlook installed on my PC.