Bitmap from Drawing not render outside of Load_For

2019-09-17 21:58发布

I'm trying to render bitmap created from drawing to screen but only render after minimize and maximize again.

I follow these steps: Using Bitmaps for Persistent Graphics in C#

But only can render bitmap in screen outside of Load_Form.

If I put the code:

using System.Drawing;
...

Graphics graphicsObj;
myBitmap = new Bitmap(this.ClientRectangle.Width, 
        this.ClientRectangle.Height, 
        Imaging.PixelFormat.Format24bppRgb);
graphicsObj = Graphics.FromImage(myBitmap);

Pen myPen = new Pen(Color.Plum, 3);
Rectangle rectangleObj = new Rectangle(10, 10, 200, 200);
graphicsObj.DrawEllipse(myPen, rectangleObj);
graphicsObj.Dispose();

In other place, for example a button, I need to minimize and maximize to see the image.

Edit:

bmp is a Bitmap global variable I create an instance in form event Load_Form1

bmp = new Bitmap(this.ClientRectangle.Width,
                 this.ClientRectangle.Height,
                 System.Drawing.Imaging.PixelFormat.Format24bppRgb);

Paint event of Form for redraw:

private void Form1_Paint(object sender, PaintEventArgs e)
{
    Graphics graphicsObj = e.Graphics;
    graphicsObj.DrawImage(myBitmap, 0, 0, myBitmap.Width, myBitmap.Height);
    graphicsObj.Dispose();
}

But I need draw inmediately after create the drawing.

1条回答
老娘就宠你
2楼-- · 2019-09-17 22:35

Not telling us about the bigger picture makes it hard to recommend the best course of action but let me simply assume two possible aims:

  • either you simply want something drawn onto the Form

  • or you want to display a bitmap into which you sucessively draw more and more things.

For the first all you need is to code the Paint event like this:

private void Form1_Paint(object sender, PaintEventArgs e)
{
    Rectangle rectangleObj = new Rectangle(10, 10, 200, 200);
    using (Pen myPen = new Pen(Color.Plum, 3))
      e.Graphics.DrawEllipse(myPen, rectangleObj);
}

If the data that control the drawing are dynamic you should store them at class level variables or lists of them and change them as needed so you can use them in the Paint event.

For the latter aim there will be various times when you add to the Bitmap.

So you start by creating a, probably, class level Bitmap:

    public Form1()
    {
        InitializeComponent();
        bmp = new Bitmap(this.ClientRectangle.Width,
                         this.ClientRectangle.Height);         
    }

    Bitmap bmp = null;

And have one or more places where you draw into it like this:

void drawALittle()
{
    Rectangle rectangleObj = new Rectangle(10, 10, 200, 200);
    using (Pen myPen = new Pen(Color.Plum, 3))
    using (Graphics G = Graphics.FromImage(bmp))
    {
        G.DrawEllipse(myPen, rectangleObj);
        //..
    }
    this.Invalidate();
}

Note how I Invalidate the Form after changing the Bitmap, so the Paint event is triggered.

Also note that if these update happen really often it will be a good idea to keep the Graphics object alive between calls; either by making it a class variable like the Bitmap or by keeping it locally in the method that does all the updates and passing it out as a parameter to the drawing method..

In the form's Paint event all you need is

private void Form1_Paint(object sender, PaintEventArgs e)
{
      e.Graphics.DrawImage(bmp, 0, 0);
}       

Also note that 32bppARGB is the recommended default format. Is is used for anything you display in any case, so it is most efficient to use it to begin with..

查看更多
登录 后发表回答