C# DrawImage 使用 DoubleBuffered 不工作
C# DrawImage using DoubleBuffered not working
您好,我有一个覆盖 OnPaint
方法的 c# 用户控件。
在我的 OnPaint 方法中,我有以下代码:
// Draw the new button.
protected override void OnPaint(PaintEventArgs e) {
Color btnColor = this.ButtonColor;
if (!this.ButtonEnabled) {
btnColor = Color.LightGray;
}
Bitmap GraphicsImage = new Bitmap(24, 24, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics.FromImage(GraphicsImage).Clear(btnColor);
Graphics graphics = e.Graphics;
SolidBrush myBrush = new SolidBrush(btnColor);
Pen myPen = new Pen(btnColor);
// Draw the button in the form of a circle
graphics.DrawEllipse(myPen, 0, 0, 40, 40);
graphics.FillEllipse(myBrush, new Rectangle(0, 0, 40, 40));
if (!DesignMode) {
Image iconImg = null;
switch (this.ButtonImage) {
case CircleButtonImage.ArrowDown:
iconImg = POS.Framework.Utility.Common.GetResourceImage("arrowdown_16.png");
break;
case CircleButtonImage.ArrowUp:
iconImg = POS.Framework.Utility.Common.GetResourceImage("arrowup_16.png");
break;
case CircleButtonImage.Cross:
iconImg = POS.Framework.Utility.Common.GetResourceImage("close_16.png");
break;
case CircleButtonImage.Plus:
iconImg = POS.Framework.Utility.Common.GetResourceImage("plus_16.png");
break;
}
graphics = Graphics.FromImage(GraphicsImage);
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
graphics.DrawImage(iconImg, 5, 5);
this.CreateGraphics().DrawImageUnscaled(GraphicsImage, new Point(7, 6));
}
myBrush.Dispose();
myPen.Dispose();
}
这很好用,但为了避免闪烁,我在我的构造函数中添加了:
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
然后 png
图像不再显示。
没有双缓冲:
双缓冲:
Any clue on how to fix this. I want to avoid flickering but need the image to be rendered.
不要使用 CreateGraphics
。您需要使用的 Graphics
对象作为参数传递给 OnPaint
,在 PaintEventArgs
.
中
另外,你真的应该自己清理一下:
using (var graphics = Graphics.FromImage(GraphicsImage))
{
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
graphics.DrawImage(iconImg, 5, 5);
}
GDI+ 对象是非常有限的资源 :) 除此之外,SmoothingMode
对您在这里所做的事情几乎没有用处 - 至少阅读文档而不是仅仅阅读货物确实有帮助。
您好,我有一个覆盖 OnPaint
方法的 c# 用户控件。
在我的 OnPaint 方法中,我有以下代码:
// Draw the new button.
protected override void OnPaint(PaintEventArgs e) {
Color btnColor = this.ButtonColor;
if (!this.ButtonEnabled) {
btnColor = Color.LightGray;
}
Bitmap GraphicsImage = new Bitmap(24, 24, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics.FromImage(GraphicsImage).Clear(btnColor);
Graphics graphics = e.Graphics;
SolidBrush myBrush = new SolidBrush(btnColor);
Pen myPen = new Pen(btnColor);
// Draw the button in the form of a circle
graphics.DrawEllipse(myPen, 0, 0, 40, 40);
graphics.FillEllipse(myBrush, new Rectangle(0, 0, 40, 40));
if (!DesignMode) {
Image iconImg = null;
switch (this.ButtonImage) {
case CircleButtonImage.ArrowDown:
iconImg = POS.Framework.Utility.Common.GetResourceImage("arrowdown_16.png");
break;
case CircleButtonImage.ArrowUp:
iconImg = POS.Framework.Utility.Common.GetResourceImage("arrowup_16.png");
break;
case CircleButtonImage.Cross:
iconImg = POS.Framework.Utility.Common.GetResourceImage("close_16.png");
break;
case CircleButtonImage.Plus:
iconImg = POS.Framework.Utility.Common.GetResourceImage("plus_16.png");
break;
}
graphics = Graphics.FromImage(GraphicsImage);
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
graphics.DrawImage(iconImg, 5, 5);
this.CreateGraphics().DrawImageUnscaled(GraphicsImage, new Point(7, 6));
}
myBrush.Dispose();
myPen.Dispose();
}
这很好用,但为了避免闪烁,我在我的构造函数中添加了:
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
然后 png
图像不再显示。
没有双缓冲:
双缓冲:
Any clue on how to fix this. I want to avoid flickering but need the image to be rendered.
不要使用 CreateGraphics
。您需要使用的 Graphics
对象作为参数传递给 OnPaint
,在 PaintEventArgs
.
另外,你真的应该自己清理一下:
using (var graphics = Graphics.FromImage(GraphicsImage))
{
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
graphics.DrawImage(iconImg, 5, 5);
}
GDI+ 对象是非常有限的资源 :) 除此之外,SmoothingMode
对您在这里所做的事情几乎没有用处 - 至少阅读文档而不是仅仅阅读货物确实有帮助。