Draw Image with GDI functions
I have a downloaded images app in mobile. Every times an image is download successful, it auto draw on to a temp bitmap and then onPaint method draw the whole temp bitmap. It caused the flikering happened to much.
My instructor advised me to use GDI functions to draw the temp bitmap onto the screen every times one image loaded. But his suggestion is so general with the two method.
[DllImport("coredll.dll")]
static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("coredll.dll")]
static extern void ReleaseDC(IntPtr dc);
So any more clearer advice for me in this situation in what he suggested? Thanks in advance.
UPDATE
//This is my buffer bitmap
privat开发者_Python百科e 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);
}
Yes - you need to do double-buffering to prevent the flicker problem, but you can do this in GDI+ without needing to resort to the API. Here's essentially what you need to do:
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);
}
DONT call this.Refresh() or the paint event when you are trying to update the screen since this is what causes the flicker, just call the Render() method directly when you need to update the screen. You just call the Paint method when the screen is first drawn when the application loads
I took this code from my compact framework game
IIRC, and if your tags are correct, if you're using C# then you can use GDI+. GDI comes from Win32 and is essentially legacy for WinForms.
Check this out for information on GDI+.
And fyi, the flickering you describe sounds like a product of not having double-buffered form. Read here to learn how to double-buffer your forms.
精彩评论