DrawToBitmap returning blank image

前端 未结 1 862
北荒
北荒 2020-11-30 15:32

I have a problem on creating bitmap image out of my winform application.

Situation:

I have a UserControl named as \"CanvasControl\"

相关标签:
1条回答
  • 2020-11-30 16:15

    The code in the question gave a first hint but the code in the link showed the source of the problem: You use a 'wrong' instance of the Graphics object for drawing:

    protected override void OnPaint(PaintEventArgs e)
    {
      // If there is an image and it has a location,
      // paint it when the Form is repainted.
      Graphics graphics = this.CreateGraphics();
      ..
    

    This is one of the most common mistakes with winforms graphics! Never use CreateGraphics ! You always should draw onto the control surface with the Graphics object in a Paint or DrawXXX event. These events have a parameter e.Graphics which is the only one that can draw persistent graphics.

    Persistent means that it will always be refreshed when necessary, not just when you trigger it. This is a nasty error because everything seems to work until you come upon a situation when an outside event makes redrawing necessary:

    • Minimizing and then maximizing the form
    • Moving it off the screen and back again
    • Calling DrawToBitmap
    • ...

    Note that all will only really work if you use the valid and current Graphics object from the PaintEventArgs e parameter.

    So, the solution is simple:

    protected override void OnPaint(PaintEventArgs e)
    {
      // If there is an image and it has a location,
      // paint it when the Form is repainted.
      Graphics graphics = e.Graphics();  // << === !!
      ..
    

    But what is the CreateGraphics good for? It is only good for luring newbies into that error??

    Not quite; here are some uses for it:

    • Drawing non-persistent graphics like a rubber-band rectangle or a special mouse cursor
    • Measuring text sizes without actually drawing it with a TextRenderer or the MeasureString method
    • Querying the screen or Bitmap resolution with Graphics.DpiX/Y

    and probably some others I can't think of at the moment..

    So for normal drawing onto controls always use the e.Grapahics object! You can pass it on to subroutines to make the code more structured, but do not try to cache it; it needs to be current!

    0 讨论(0)
提交回复
热议问题