我如何获取控件的屏幕截图?DrawToBitmap不起作用我有一个通过dll调用从外部绘制的pictureBox。privatevoidmyPictureBox_Paint(objectsender,PaintEventArgse){dllClass.RefreshEx(LWHANDLE,0,0);这行得通,但我现在需要获取该图片框的屏幕截图,但行不通。这是我尝试过的:ControlctrlToDraw=myPictureBox;System.Drawing.Bitmapbmp=newSystem.Drawing.Bitmap(ctrlToDraw.Width,ctrlToDraw.Height);ctrlToDraw.DrawToBitmap(bmp,ctrlToDraw.ClientRectangle);我也尝试通过WindowsAPI来实现它,但它产生的结果与上面的代码完全相同:空白(非空)图像。除了截取整个屏幕的屏幕截图之外,任何人都可以提出其他建议吗?我不知道你对外部程序有多少控制,或者它如何绘制到你的图片框,但如果你使用createGraphics,它就不会工作。privatevoidbutton1_Click(objectsender,EventArgse){//这里我调用的是PictureBox的图形对象,这会绘制到图片框//但是DrawToBitmap,不会反映这个变化,一旦Picturebox需要被更新,这将消失。图形g=pictureBox1.CreateGraphics();g.DrawRectangle(Pens.Blue,10,10,20,20);System.Drawing.Bitmapbmp=newSystem.Drawing.Bitmap(pictureBox1.Width,pictureBox1.Height);矩形边界=newRectangle(Left,Top,Width,Height);pictureBox1.DrawToBitmap(bmp,pictureBox1.ClientRectangle);//将PictureBox中的任何内容绘制到FormsBackgroundImagethis.BackgroundImage=bmp;//ItwillnotdrawtheBluerectangle}如果您的外部程序要绘制到位图,那么您可以将位图设置为图片框背景Bitmapbuffer;publicForm1(){InitializeComponent();buffer=newBitmap(pictureBox1.Width,pictureBox1.Height);}privatevoidbutton1_Click(objectsender,EventArgse){//使用(Graphicsg=Graphics.FromImage(buffer)){g.DrawRectangle(Pens.Blue,10,10,20,20);}//将图片框图像分配给缓冲区pictureBox1.Image=buffer;//现在这将显示蓝色矩形System.Drawing.Bitmapbmp=newSystem.Drawing.Bitmap(pictureBox1.Width,pictureBox1.Height);矩形边界=newRectangle(Left,Top,Width,Height);pictureBox1.DrawToBitmap(bmp,pictureBox1.ClientRectangle);this.BackgroundImage=bmp;}编辑第三次魅力这将截取屏幕截图,剪掉图片框,然后我更改了表格背景,只是为了证明它有效你需要添加使用System.Drawing.Imaging;它以像素格式提供。privatevoidbutton1_Click(objectsender,EventArgse){使用(GraphicsG=pictureBox1.CreateGraphics()){G.DrawRectangle(Pens.Blue,10,10,10,10);位图BMP=newBitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height,PixelFormat.Format32bppArgb);使用(GraphicsGFX=Graphics.FromImage(BMP)){GFX.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,Screen.PrimaryScreen.Bounds.Y,0,0,Screen.PrimaryScreen.Bounds.Size,CopyPixelOperation.SourceCopy);}位图YourPictureBoxImage=newBitmap(pictureBox1.Width,pictureBox1.Height);使用(Graphicsg=Graphics.FromImage(YourPictureBoxImage)){Pointnp=pictureBox1.PointToScreen(newPoint(0,0));g.DrawImage(BMP,newRectangle(0,0,100,100),newRectangle(np,pictureBox1.Size),GraphicsUnit.Pixel);}this.BackgroundImage=YourPictureBoxImage;您可以使用ALT+PRINTSCREEN截取活动窗口的屏幕截图。这会将当前活动窗口作为位图捕获到剪贴板。将其粘贴到位图编辑工具中,裁剪到您想要的区域。或者使用允许在屏幕上裁剪目标区域的屏幕捕获实用程序,例如Evernote版本2的OldScreenClipper。以上就是C#学习教程:如何获取控件的截图?DrawToBitmap不起作用分享的所有内容,如果对你有用,需要了解更多C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
