截取部分表格的截图?我目前有一个winform,在运行时,我需要截图其中的一部分并保存为图像。特别是,我想要一个名为“panel1”的面板的屏幕截图。我已经能够找到有关如何截取整个网络表单或整个屏幕的屏幕截图的信息……但是我很难找到有关截取屏幕一部分的信息。有没有人有任何信息可以指出我正确的方向?即使是我应该查找的MSDN站点上的文档名称也会提供巨大的帮助!谢谢!从Panel派生的Control类有一个DrawToBitmap()方法。你可以使用它。http://msdn.microsoft.com/en-us/library/system.windows.forms.control.drawtobitmap.aspx查看Graphics.CopyFromScreen的文档。您传递2组X/Y坐标——屏幕上的和位图中的。以上是C#学习教程:部分窗体截图?如果分享的内容对你有用,需要了解更多C#学习教程,希望你多多关注——#region-Capture&GraphicAPI-#regionGDI32publicconstintSRCCOPY=0x00CC0020;//BitBltdwRop参数[DllImport("gdi32.dll")]publicstaticexternboolBitBlt(IntPtrhObject,intnXDest,intnYDest,intnWidth,intnHeight,IntPtrhObjectSource,intnXSrc,intnYSrc,intdwRop);[DllImport("gdi32.dll")]publicstaticexternboolStretchBlt(IntPtrhdcDest,//目标DC的句柄intnXOriginDest,//目标左上角的x坐标intnYOriginDest,//目标上角的y坐标-leftcornerintnWidthDest,//目标矩形的宽度intnHeightDest,//目标矩形的高度IntPtrhdcSrc,//源DC的句柄intnXOriginSrc,//源左上角的x坐标intnYOriginSrc,//y-源左上角的坐标intnWidthSrc,//源矩形的宽度intnHeightSrc,//源矩形的高度intdwRop//光栅操作码);[DllImport("gdi32.dll")]publicstaticexternIntPtrCreateCompatibleBitmap(IntPtrhDC,intnWidth,intnHeight);[DllImport("gdi32.dll")]publicstaticexternIntPtrCreateCompatibleDC(IntPtrhDC);[DllImport("gdi32.dll")]publicstaticexternboolDeleteDC(IntPtrhDC);[DllImport("gdi32.dll")]publicstaticexternboolDeleteObject(IntPtrhObject);[DllImport("gdi32.dll")]publicstaticexternIntPtrSelectObject(IntPtrhDC,IntPtrhObject);#endregion#region-USER32-[StructLayout(LayoutKind.Sequential)]publicstructRECT{publicintleft;公共诠释顶部;公共权利;公共底部;}[DllImport("user32.dll")]publicstaticexternIntPtrGetDesktopWindow();[DllImport("user32.dll")]publicstaticexternIntPtrGetWindowDC(IntPtrhWnd);[DllImport("user32.dll")]publicstaticexternIntPtrReleaseDC(IntPtrhWnd,IntPtrhDC);[DllImport("user32.dll")]publicstaticexternIntPtrGetWindowRect(IntPtrhWnd,参考RECT矩形);#endregion#endregionprivatevoidrun_test(){Rectanglerc=newRectangle();图片img=ScreenToImage(refrc,false);img.Save(文件名,System.Drawing.Imaging.ImageFormat.Jpeg);img.Dispose();}privateImageScreenToImage(refRectanglercDest,boolIsPapaer){IntPtrhandle=this.Handle;//this.Handle//获取目标窗口的hDCIntPtrhdcSrc=GetWindowDC(handle);//获取大小RECTwindowRect=newRECT();GetWindowRect(句柄,refwindowRect);intnWidth=windowRect.right-windowRect.left;intnHeight=windowRect.bottom-windowRect.top;if(IsPapaer){floatfRate=(float)rcDest.Width/nWidth;//浮动fHeight=nHeight*fRate;//rcDest.Height=(int)(nHeight*fRate);//rcDest.Width=(int)(rcDest.Width);//*fRate);rcDest.X=0;rcDest.Y=0;rcDest.Height=(int)(nHeight*fRate);//rcDest.Width=(int)(nWidth*fRate);}else{rcDest.X=0;rcDest.Y=0;rcDest.Height=nHeight;rcDest.Width=nWidth;}//创建一个我们可以复制到IntPtr的设备上下文hdcDest=CreateCompatibleDC(hdcSrc);//创建一个我们可以将其复制到的位图,//使用GetDeviceCaps获取宽度/高度IntPtrhBitmap=CreateCompatibleBitmap(hdcSrc,rcDest.Width,rcDest.Height);//选择位图对象IntPtrhOld=SelectObject(hdcDest,hBitmap);//bitbltoverStretchBlt(hdcDest,rcDest.X,rcDest.Y,rcDest.Width,rcDest.Height,hdcSrc,0,0,nWidth,nHeight,SRCCOPY);//恢复选择SelectObject(hdcDest,hOld);//清理DeleteDC(hdcDest);ReleaseDC(句柄,hdcSrc);//为它获取一个.NET图像对象Imageimg=Image.FromHbitmap(hBitmap);//释放位图对象DeleteObject(hBitmap);返回图像;}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。
