有没有人知道如何使用C#截取屏幕截图并限制它拍摄特定容器/部分应用程序的图片。我不想要整个屏幕或应用程序的整个窗口。
我的面板简称为:panel1 用户可以单击“按钮”并获取panel1的屏幕截图并附加到电子邮件。
我只想拍摄该部分的屏幕截图,然后在本地保存到C:\ Drive和/或附加或嵌入到Outlook电子邮件中。
我在互联网上阅读了其他各种各样的内容,但大多数人都不得不处理创建复杂的更改,拍摄我不想要的网页浏览器控件的截图。
答案 0 :(得分:5)
我使用类似
之类的东西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);
}
我希望这会有所帮助
答案 1 :(得分:5)
如果您只想使用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");
请注意,某些控件可能无法使用此功能,例如WebBrowser
和RichTextBox
控件,但它适用于大多数其他控件(文本框,标签等)。
答案 2 :(得分:2)
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;
}
此方法将立即截取 。例如,如果在调用此函数之前更改面板内按钮的可见性,则位图将包含该按钮。如果不需要,请使用keyboardP's answer。
答案 3 :(得分:2)
对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;
}
答案 4 :(得分:1)
我发现这可以将控件保存为位图:
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 here的内容,但我无法测试它,因为我的PC上没有安装Outlook。
答案 5 :(得分:1)
修改KeyboardP's答案。修改为静态方法,因此可以用作辅助方法。
public static Bitmap ScreenShotaControl(Control aControl)
{
try
{
Bitmap bmp = new Bitmap(aControl.Width, aControl.Height);
aControl.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
return bmp;
}
catch (Exception)
{
throw;
}
}
用法示例如下:
Bitmap bmp = ScreenShotaControl(aControl);
Clipboard.SetImage(bmp); //to copy to clipboard
Bitmap bmp = ScreenShotaControl(aControl);
bmp.Save(@"C:\MyPanelImage.bmp"); //save to specified path
Bitmap bmp = ScreenShotaControl(aControl);
MemoryStream ms= new MemoryStream();
bmp.Save(ms, ImageFormat.Jpeg);
ContentType ct= new ContentType();
ct.MediaType = MediaTypeNames.Image.Jpeg;
MailMessage mail = new MailMessage();
mail.Attachments.Add(new Attachment(ms, ct)); //attach to email