嗨,我有一个c#用户控件,我重写了OnPaint方法。
在我的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图像就不再显示了。
无双缓冲的:

有双重缓冲的:

有什么好办法解决的吗。我想避免闪烁,但需要图像被渲染。
发布于 2016-03-31 17:01:13
不要使用CreateGraphics。您需要使用的Graphics对象作为参数传递给OnPaint,在PaintEventArgs中。
另外,你真的应该自己清理一下:
using (var graphics = Graphics.FromImage(GraphicsImage))
{
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
graphics.DrawImage(iconImg, 5, 5);
}GDI+对象是一个非常有限的资源:)除此之外,SmoothingMode对于您在这里所做的工作几乎没有什么用处--至少阅读文档而不仅仅是对货物的截取非常有帮助。
https://stackoverflow.com/questions/36339399
复制相似问题