【发布时间】:2015-07-04 15:59:01
【问题描述】:
我正在尝试将从绘图创建的位图渲染到屏幕,但仅在再次最小化和最大化后渲染。
我按照以下步骤操作:Using Bitmaps for Persistent Graphics in C#
但只能在 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();
在其他地方,例如按钮,我需要最小化和最大化才能看到图像。
编辑:
bmp是一个Bitmap全局变量我在表单事件Load_Form1
中创建了一个实例bmp = new Bitmap(this.ClientRectangle.Width,
this.ClientRectangle.Height,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
重绘Form的Paint事件:
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics graphicsObj = e.Graphics;
graphicsObj.DrawImage(myBitmap, 0, 0, myBitmap.Width, myBitmap.Height);
graphicsObj.Dispose();
}
但我需要在创建图纸后立即进行绘制。
【问题讨论】:
-
您展示的代码正在创建一个位图(并泄漏一支笔)。 在哪里你认为它应该显示?您是否将其分配给某物,例如某物的图像或背景图像? mini/max 'trick' 听起来好像你缺少一个 Invalidate 来显示在 Paint 事件中绘制的东西,这与上面的代码无关..
-
我想在打开的表单中显示。我只能在 Paint_Form1 像“编辑”中的显示那样被触发时进行绘制,但我需要在生成位图后进行绘制。我尝试使用 graphicsObj.DrawImage(myBitmap, 0, 0, myBitmap.Width, myBitmap.Height); 显示,但仅适用于 Form1_Load。我无法从创建位图的按钮单击中显示。
-
好的,如果这就是你想要的(听起来很奇怪),你需要做的就是在创建位图后添加一个
this.Invalidate();。并且请不要处置您没有创建的任何东西,例如e.Grahphics!! - 当然,只需查看您向我们展示的代码,您根本不需要任何代码,当然,除了:using( Pen myPen = new Pen(Color.Plum, 3) e.Graphics.DrawEllipse(myPen, rectangleObj);' 和 Rectangle。其余的似乎没用,除非您实际上还需要位图用于其他目的.. - 最后:不建议使用 24bpp,除非您真的需要它,请使用 32bpp
标签: c# .net bitmap drawing system.drawing