How to get bitmap from painted panel in C#

扶醉桌前 提交于 2019-12-10 19:12:19

问题


I have a panel and I use it's Graphics gr = panel1.CreateGraphics() to draw lines and other stuff. I need to get pixel color of the point where mouse is clicked, so I decided to use GetPixel method of Bitmap. I create bitmap this way:

Bitmap b = new Bitmap(width, height);            
panel1.DrawToBitmap(b, new Rectangle(0, 0, width, height));
b.Save("D:/aaa.bmp");

but I get only white rectangle even if I've drawn anything. What's the problem?


回答1:


Only things that are drawn in the Paint event will be rendered by DrawToBitmap. Instead of explicitly call panel1.CreateGraphics(), handle the Paint event of the panel and do your drawing using e.Graphics.



来源:https://stackoverflow.com/questions/8553794/how-to-get-bitmap-from-painted-panel-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!