How to save Graphics object as image in C#?

后端 未结 2 1414
没有蜡笔的小新
没有蜡笔的小新 2020-12-31 18:18

I have panel and various controls on it. I would like to save an image of this panel into a file, how can I do this ?

Ineed to do something like screenshot, but I n

2条回答
  •  说谎
    说谎 (楼主)
    2020-12-31 18:46

    In response to your edit:

    If you are drawing on the panel using a Graphics object returned by the CreateGraphics method, your graphics are not permanent. Anything that you draw on the object will be erased the next time that the control is redrawn. (For more detailed information on this subject, see: http://www.bobpowell.net/picturebox.htm and http://www.bobpowell.net/creategraphics.htm)

    When you use the DrawToBitmap method as suggested by Hans Passant's answer, the panel control is getting redrawn, which is causing your drawings to be lost.

    Instead, if you want your drawings to be permanent, you need to handle the Paint event of the panel control. This event is raised every time that the control needs to be redrawn, and provides an instance of PaintEventArgs that contains a Graphics object you can use to draw permanently on the control's surface in the same way that you were using the Graphics object returned by the CreateGraphics method.

    Once you've corrected your drawing code, you can use Hans's solution.

提交回复
热议问题