绘图中的位图未在Load_Form外部呈现 [英] Bitmap from Drawing not render outside of Load_Form

查看:104
本文介绍了绘图中的位图未在Load_Form外部呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将从图形创建的位图渲染到屏幕,但是仅在最小化和最大化后才渲染.

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

我按照以下步骤操作:在C#中为持久图形使用位图

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

但是只能在Load_Form外部的屏幕中渲染位图.

But only can render bitmap in screen outside of Load_Form.

如果我输入代码:

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.

bmp 是一个位图全局变量,我在表单事件 Load_Form1

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);

要重绘的Form绘画事件:

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.

推荐答案

不告诉我们有关总体情况的信息,这很难推荐最佳的行动方案,但让我简单地假设两个可能的目标:

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.

首先,您需要编写Paint事件,如下所示:

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);
}

如果控制图形的数据是动态的,则应将它们存储在类级别的变量或变量列表中,并根据需要进行更改,以便可以在Paint事件中使用它们.

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.

因此,您可能首先创建一个类级别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();
}

请注意,在更改Bitmap之后我如何Invalidate Form,所以触发了Paint事件.

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

还请注意,如果这些更新确实经常发生,则最好在每次调用之间保持Graphics对象处于活动状态;通过将其设为类似于Bitmap的类变量,或者将其保留在进行所有更新的方法中,然后将其作为参数传递给绘图方法,即可.

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..

在表单的Paint事件中,您所需要做的只是

In the form's Paint event all you need is

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

还请注意,32bppARGB是建议的默认格式.在任何情况下都可用于显示的任何内容,因此从一开始使用它是最有效的.

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..

这篇关于绘图中的位图未在Load_Form外部呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆