gdi and graphics

false74

Well-known member
Joined
Aug 4, 2010
Messages
76
Programming Experience
Beginner
I am completely confused onto using GDI/graphics; and I hope someone can help me out with this one. I have a form with 1 picturebox (picturebox1) and 1 button (button1)...

I am trying to create a simple program that drawstrings the current time, and keeps redrawing it; while having a button that lets you save it to a .png file.

this is the jumble of code i have made currently; but i do not know how to put it together and make it work; help, please and thankyou!
VB.NET:
        Dim graphics As Graphics = PictureBox1.CreateGraphics
        Dim font As New Font("Arial", 15, FontStyle.Bold)

        'draw time
        graphics.DrawString(Now, font, Brushes.Red, 0, 0)

        'draw graphics to bitmap
        Dim img As New Bitmap(720, 100, graphics)

        'save to file
        img.Save("C:\img.png", System.Drawing.Imaging.ImageFormat.Png)

        'dispose objects
        PictureBox1.CreateGraphics.Dispose()
        img.Dispose()
        graphics.Dispose()
        font.Dispose()
 
Ah, You are god. It works. However when I moved this code into another form with many other buttons and objects the picturebox it flickers like hell when it invalidates. I've heard using doublebuffers to solve this, but I enabled doublebuffer for the form, and has done nothing.
 
A PictureBox is optimised by default. If you're getting flickering then you must be either explicitly or implicitly calling Update or Refresh a lot. If you're displaying the current time then you shouldn't need to repaint more than once per second, so that won't cause any flickering. I suggest that you work out why the PictureBox is being repainted so often. Also, have you made sure that you're only invalidating the area in which you're drawing the text? There's no point repainting the whole thing if only a small part is changing.
 
Ok, I forgot to slow down the timer to 1000ms (1second). however it still flickers when it invalidates. How do I only invalidate a certain area? Also, what would double buffering do?
I am going to assume its something like this: (to make multiple bounds at different places)

VB.NET:
'Draw three rectangles
    Dim ThreeRects() As Rectangle = {New Rectangle(110, 10, 40, 40), _
                              New Rectangle(160, 10, 40, 40), _
                              New Rectangle(210, 10, 40, 40)}

 Me.Invalidate(ThreeRects)

Where should I draw the bounds, inside the sub that creates the graphics. or in the picturebox paint handler, or on load?
 
How do I only invalidate a certain area?
If you've read the code I provided then you already know the answer to that.
Also, what would double buffering do?
Always look first and ask questions later.

vb.net double buffering - Google Search
Where should I draw the bounds, inside the sub that creates the graphics. or in the picturebox paint handler, or on load?
You aren't drawing any bounds. I don't know what you're asking.
 
Sorry, I should have googled it before I posted. I added this

VB.NET:
        SetStyle(ControlStyles.UserPaint, True)
        SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        SetStyle(ControlStyles.DoubleBuffer, True)
        SetStyle(ControlStyles.UserPaint, True)
        SetStyle(ControlStyles.AllPaintingInWmPaint, True)

and I made multiple rectangles for invalidating only specific locations and it seems much less flickery, although it still has its moments from time to time.
 
If you made multiple Rectangles then that would imply that multiple areas on your PictureBox are changing but you haven't said anything in this thread about that. If that is the case then you haven't provided us with all the relevant information. If you want us to provide a solution then you need to give us all the relevant information.
 
As I stated previous, I will be adding more items to be draw in the graphics later on (that would require updates).
Everything works great now. So I shouldn't be needing anymore assistance; thank you so much for your help, I understand gdi much more than I had previous to making this thread. Much appreciation! You sure know your stuff mate!
 
Back
Top