[问题]如何将form中的某一区域截取并用image对象保存? 如何将form中的某一区域截取并用image对象保存?提示:Graphics.CopyFromScreen(); 等请给出教完整代码. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 from Stoitcho Goutsev's reply to a similar postpublic static System.Drawing.Bitmap PrintWindow(IntPtr hWnd, PrintWindowFlags flags) { //Rectangle screenBounds = Rectangle.Empty; Rectangle visibleBounds = Rectangle.Empty; System.Drawing.Bitmap image = null; using(Graphics grfx = Graphics.FromHdc(GetWindowDC(hWnd))) { visibleBounds = Rectangle.Round(grfx.VisibleClipBounds); } image = new System.Drawing.Bitmap( visibleBounds.Width, visibleBounds.Height); System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(image); IntPtr hDC = graphics.GetHdc(); //paint control onto graphics using provided options try { User32.PrintWindow(hWnd, hDC, (uint)flags); } finally { graphics.ReleaseHdc(hDC); } return image; } public enum PrintWindowFlags : uint { /// <summary> /// /// </summary> PW_ALL = 0x00000000, /// <summary> /// Only the client area of the window is copied. By default, the entire /// window is copied. /// </summary> PW_CLIENTONLY = 0x00000001 } } 记得Graphics有个方法,可以设置点的坐标,就可以截取,你查查msdn to:jiangsheng(蒋晟.Net[MVP]) image = new System.Drawing.Bitmap( visibleBounds.Width, visibleBounds.Height);这里得到的不只是一个空图吗?还有,User32.PrintWindow(hWnd, hDC, (uint)flags); 具体有做些什么呢? to:cyy1981(McRain)我的问题是如何把截取后的对象保存 ? 可以考虑下使用DrawToBitmap方法。比如:using(Bitmap bit = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height)){ this.DrawToBitmap(bit, this.ClientRectangle); bit.Save("D:\\temp\bmp.bmp");} to : hbxtlhx(平民百姓-自已动手,丰衣足食) 你提供了一个很好的方法,谢谢,不过DrawToBitmap(arg,arg)有一些限制,能考虑在窗体重绘的时候将重绘的数据保存吗,期待 bitmap bm = new bitmap(宽,高);Graphis g = Graphis.FromImage(bm);g.DrawImage(老图片,Rect,Rect,GraphicsUnit.Pix);g.Dispose 试试:[DllImportAttribute("gdi32.dll")]public static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, System.Int32 dwRop);public Bitmap Capture(Control ctrl) { Graphics g = Graphics.FromHwnd(ctrl.Handle); Bitmap bitmap = new Bitmap(ctrl.Width, ctrl.Height, g); Graphics g1 = Graphics.FromImage(bitmap); IntPtr hdc = g.GetHdc(); IntPtr hdcBitmap = g1.GetHdc(); BitBlt(hdcBitmap, 0, 0, bitmap.Width, bitmap.Height, hdc, 0, 0, 0x00CC0020); g1.ReleaseHdc(hdcBitmap); g.ReleaseHdc(hdc); g1.Dispose(); g.Dispose(); return bitmap; } Bitmap bt = new Bitmap(this.Width, this.Height); Graphics gr = Graphics.FromImage(bt); gr.CopyFromScreen(this.Location, new Point(0, 0), this.Size); bt.Save(@"D:/temp.bmp");刚研究GDI+,对里面许多方法不熟,借用了一下作者的提供方法,这可以保存整个窗体的背景,如果想截取窗体里面特定的矩形,我想应该改一下起始点和size就可以了 上面那段代码前窗体应该是已经显示完毕,并且调用了Application.DoEvent();测试通过 呵呵,方法越多越好~! QQ截屏时要stop 一下~ 好象很不错~ 在winform中边读边写文件。 如何让光标随着文字往后移??? WPF 事件处理函数问题。。。 C#listview中如何获取变色的数据行 我想知道在graphicspath里,怎么对子路径进行拾取~~~ 如何查询并返回几条相同记录? 请大家进来帮一下忙,不胜感激. 如何改变datagridview中不同行的颜色? 请问,关于DataTable 怎样用DrawImage的方法画出平滑的动画效果? 如何将“25 10 2005"这样的字符串转换为TDateTime类型 用c#写winform程序时,怎么生成全局变量?
public static System.Drawing.Bitmap PrintWindow(IntPtr hWnd,
PrintWindowFlags flags)
{ //Rectangle screenBounds = Rectangle.Empty;
Rectangle visibleBounds = Rectangle.Empty;
System.Drawing.Bitmap image = null; using(Graphics grfx = Graphics.FromHdc(GetWindowDC(hWnd)))
{ visibleBounds = Rectangle.Round(grfx.VisibleClipBounds); } image = new System.Drawing.Bitmap(
visibleBounds.Width,
visibleBounds.Height); System.Drawing.Graphics graphics =
System.Drawing.Graphics.FromImage(image); IntPtr hDC = graphics.GetHdc();
//paint control onto graphics using provided options
try
{
User32.PrintWindow(hWnd, hDC, (uint)flags);
}
finally
{
graphics.ReleaseHdc(hDC);
} return image;
} public enum PrintWindowFlags : uint
{
/// <summary>
///
/// </summary>
PW_ALL = 0x00000000,
/// <summary>
/// Only the client area of the window is copied. By default, the entire
/// window is copied.
/// </summary>
PW_CLIENTONLY = 0x00000001
}
}
visibleBounds.Width,
visibleBounds.Height);这里得到的不只是一个空图吗?还有,User32.PrintWindow(hWnd, hDC, (uint)flags);
具体有做些什么呢?
{
this.DrawToBitmap(bit, this.ClientRectangle);
bit.Save("D:\\temp\bmp.bmp");
}
Graphis g = Graphis.FromImage(bm);
g.DrawImage(老图片,Rect,Rect,GraphicsUnit.Pix);
g.Dispose
public static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, System.Int32 dwRop);public Bitmap Capture(Control ctrl)
{
Graphics g = Graphics.FromHwnd(ctrl.Handle);
Bitmap bitmap = new Bitmap(ctrl.Width, ctrl.Height, g);
Graphics g1 = Graphics.FromImage(bitmap);
IntPtr hdc = g.GetHdc();
IntPtr hdcBitmap = g1.GetHdc();
BitBlt(hdcBitmap, 0, 0, bitmap.Width, bitmap.Height, hdc, 0, 0, 0x00CC0020);
g1.ReleaseHdc(hdcBitmap);
g.ReleaseHdc(hdc);
g1.Dispose();
g.Dispose();
return bitmap;
}
Graphics gr = Graphics.FromImage(bt);
gr.CopyFromScreen(this.Location, new Point(0, 0), this.Size);
bt.Save(@"D:/temp.bmp");
刚研究GDI+,对里面许多方法不熟,借用了一下作者的提供方法,这可以保存整个窗体的背景,如果想截取窗体里面特定的矩形,我想应该改一下起始点和size就可以了