fast bitmap drawing

NeOBz

Member
Joined
Sep 29, 2003
Messages
9
i have a big problem with my game in vb.net. i have a background picture in a panel, its 800*1200. i have many link label and picture so when i scroll down or anything, the game become slow like hell. if i put off the picture, the speed is ok. So any idea to draw the big picture faster than making it the background picture of a panel ?
 
Dont forget to set DoubleBuffering and two other styles which will improve the drawing:
Code:
Me.SetStyle(ControlStyles.DoubleBuffer, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Me.SetStyle(ControlStyles.UserPaint, True)
You can find a lot of info on those in MSDN. basically double buffering is drawing in memory first and then copying the result to the screen.
 
As far as I know, UserPaint and whatnot has no effect when used directly on the surface of a form. I believe you will have to subclass (simply Override WndProc) and capture the WM_PAINT message.
 
AllPaintingInWmPaint tells the application to ignore the WM_ERASEBKGND which will lower the flicker, and it should only be set if UserPaint is set. This would mean that it does work :), I could be wrong and its never too late to learn.
 
Well, I havent tested on a form, but the other day when making an analog clock control (a UserControl), settings those styles did nothing to the flicker. When I subclassed for WM_PAINT and doublebuffered myself (with a temporary Graphics object and Bitmap) it was totally flickerless.
 
VolteFace, are you saying that you did not use DoubleBuffer from SetStyles?

If so, how do double buffing with GDI+ manually?
 
You create a bitmap object, a graphics object for that bitmap. Then you draw using that Graphics object onto that bitmap and finally draw and present the bitmap when its finished.
 
Thats it? And without using SetStyle, the Image will be "blitted" to the screen; DoubleBuffed?

It does not work.
C#:
Bitmap b=new Bitmap(wbb.Bmp.Size.Width, wbb.Bmp.Size.Height);
Graphics g= Graphics.FromImage(b);
g.DrawImage(wbb.Bmp, new Rectangle(0,0, wbb.Bmp.Width, wbb.Bmp.Height));
e.Graphics.DrawImage(b, wbb.Bounds, wbb.ClipBounds, GraphicsUnit.Pixel);
g.Dispose();
 
Last edited by a moderator:
Thats sort of half right. You need two objects.
C#:
Bitmap b = new Bitmap(wbb.Bmp.Size.Width, wbb.Bmp.Size.Height);
Graphics db = Graphics.FromImage(b); // double buffer
Graphics g = Me.CreateGraphics();
// draw your stuff on db, the backbuffer
g.DrawImage(db, /* etc */); // show the memory bitmap
Something like that.
 
Im assuming you trying to do that with a panel because you were mentioning it. If you want to do that for a panel you need to inherit a control from a panel, apply those styles in the constructor and use that control instead of the original panel. You have to do it like this becasue SetStyle is protected method.
If you are using a form then what is your excat code?
 
its in my sub load form1
...
Dim g as Graphics = me.createGraphics
Dim mybitmap as new bitmap("c:\carte.jpg")
g.drawimage(myBitmap, 0, 0)
g.dispose()
me.setstyle(controlstyles.doublebuffer,true)
me.setstyle(controlStyles.UserPaint, true)
...

ok here is the problem i have

when i add this
me.setstyles.(Controlstyles.allpaintinginwmpaint, true)
the picture dont show

and when i scroll down the rest of the picture isnt there
i imagine i have to redraw image each time i scroll


and even if my control over the form have transparent backcolor, their backcolor appeir has the form backcolor.
 
Thats the problem with me.CreateGraphics(), it is not automatically updated when the screen is redrawn. If you want it to "stick" you must draw in a paint event or better yet an overriden OnPaint of the Form or the Panel.

Then you use the SetStyle.DoubleBuffer procedure. That is the easiest way to do it.
 
when i use onPaint or paint, it become as slow as if i was using a background image of the form.i guess the only way i can make it fast enough would be to repaint the picture but only when i use the scroll of the form. but i cant find that event. i guess il have to build a new scrollbar instead of the automatic scroll of the form
 
VolteFace, this did not work. Still flickers the same.
C#:
Bitmap b=new Bitmap(wbb.Bmp.Size.Width, wbb.Bmp.Size.Height);
Graphics g= Graphics.FromImage(b);
g.DrawImage(wbb.Bmp, new Rectangle(0,0, wbb.Bmp.Width, wbb.Bmp.Height));
Graphics gg=this.CreateGraphics();
gg.DrawImage(b, wbb.Bounds, wbb.ClipBounds, GraphicsUnit.Pixel);
g.Dispose();
gg.Dispose();
NeOBz, Code in OnPaint should not be slow if you do it right. It should only paint part of the picture and Form when you scroll.
 
Volteface...mutant, do you see anything wrong in that code above? I have in in OnPaint, does it need relocated to work?
 
I dont understand... when i use Onpaint its still slow. its only a 800*1212 picture. How can a computer play games like unreal smothly and not being able to show only one stupid picture. Well it can show it but when i click on control on the form, its too slow.
How could i use my graphics card power to print this picture so my processor stay fast enough for other jobs ?
 
Commercial games dont use GDI for games :).
If you want to use the graphics card you have to look into a graphics API like DirectX, which uses graphics hardware. GDI is not hardware accelerated.
 
Back
Top