我在手机上有一个下载的图片应用。每次成功下载图像时,它会自动绘制到临时位图,然后onPaint方法绘制整个临时位图。它引起了恶作剧的发生。
我的导师建议我每次加载一个图像时使用GDI函数将临时位图绘制到屏幕上。但他的建议对于这两种方法是如此普遍。
[DllImport("coredll.dll")]
static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("coredll.dll")]
static extern void ReleaseDC(IntPtr dc);
在他建议的情况下,在这种情况下给我更清楚的建议吗?提前谢谢。
更新
//This is my buffer bitmap
private Graphics offGraph;
private Bitmap offBitmap;
//everytime an image is loaded, it raise an event and then I draw it on buffer.
private void ImageLoadDone(object sender, EventArgs e)
{
ImageObj LoadedImg = (ImageObj)sender;
LoadedImg.Render(offGraph);
this.BeginInvoke(new EventHandler(ImageUpdate));
}
private void ImageUpdate(object sender, EventArgs myE)
{
this.Render();
}
//and then onPaint draw the offbitmap.
private void Render()
{
CtrlGraph.DrawImage(offBitmap,0,0);
}
答案 0 :(得分:2)
是的 - 您需要进行双缓冲以防止闪烁问题,但您可以在GDI +中执行此操作而无需使用API。这基本上是你需要做的:
private Bitmap backgroundBitmap;
private Graphics backgroundGraphics;
private Rectangle rect;
private Rectangle srcRect;
// create background bitmap the same size as your screen
backgroundBitmap = new Bitmap(this.Width, this.Height);
// create background buffer
backgroundGraphics = Graphics.FromImage(backgroundBitmap);
// get current screen graphics
g = this.CreateGraphics();
rect = new Rectangle(0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height);
srcRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
Then when you receive your event and need to update the screen, call your own method:
Render();
private void Render()
{
// draw your image onto the background buffer
backgroundGraphics.DrawImage(bmp, rect, srcRect, GraphicsUnit.Pixel);
// draw whatever you need to on the background graphics
backgroundGraphics.DrawImage(.....)
// after all images are drawn onto the background surface
// blit back buffer to on the screen in one go
g.DrawImage(backgroundBitmap, this.ClientRectangle,
new Rectangle(0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height), GraphicsUnit.Pixel);
}
当您尝试更新屏幕时,不要调用this.Refresh()或paint事件,因为这是导致闪烁的原因,只需在需要更新屏幕时直接调用Render()方法。 在应用程序加载时首次绘制屏幕时,只需调用Paint方法
我从我的紧凑框架游戏中获取了这段代码
答案 1 :(得分:0)
Check this out for information on GDI+
而fyi,你描述的闪烁听起来像是没有双缓冲形式的产物。阅读here以了解如何对表单进行双重缓冲。