Drawing to persistent bitmap

TechnoTone

Well-known member
Joined
Jan 20, 2003
Messages
224
Location
UK - London
I havent had very much experience with graphics so please bear with me.


Heres what Im trying to do. I have two TrackBar controls, trackStart and trackStop. These represent Start time and Stop time. They are horizontal TrackBars, one directly above the other.

Between these two controls I want to draw a coloured bar indicating the running time (time between the Start and Stop times). How should I do this?

I thought that I should place a PictureBox between the TrackBars which displays a Bitmap object. Im confident that I can figure out the coordinates that I need to get the results I want but Im having difficulty working out how to paint onto the Bitmap object and then display it.

Any pointers?
 
How about creating a Graphics object from a Bitmap?
GFX = System.Drawing.Graphics.FromImage(bmp)
Use this Graphics object to draw on your bitmap...

Then, with another Graphics object from your form, you can draw the new image like this:
GFXForm.DrawImage(bmp) Same image as above.
 
Excellent. Got it working. Thanks.

Heres my final code, in case anyones interested:

Code:
        myTimeRangeBitmap = New Bitmap(picTimeRange.ClientSize.Width, picTimeRange.ClientSize.Height)

        Dim ForeBrush As New System.Drawing.SolidBrush(SystemColors.Highlight)
        Dim BackBrush As New System.Drawing.SolidBrush(SystemColors.Control)
        Dim x As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(myTimeRangeBitmap)
        Dim StartPos As Integer = (trackStart.Value * picTimeRange.ClientSize.Width) \ trackStart.Maximum
        Dim StopPos As Integer = (trackStop.Value * picTimeRange.ClientSize.Width) \ trackStop.Maximum

        If StartPos < StopPos Then
            x.FillRectangle(BackBrush, 0, 0, StartPos, picTimeRange.ClientSize.Height)
            x.FillRectangle(BackBrush, StopPos, 0, picTimeRange.ClientSize.Width - StopPos, picTimeRange.ClientSize.Height)
            x.FillRectangle(ForeBrush, StartPos, 0, StopPos - StartPos, picTimeRange.ClientSize.Height)
        ElseIf StartPos > StopPos Then
            x.FillRectangle(BackBrush, StopPos, 0, StartPos - StopPos, picTimeRange.ClientSize.Height)
            x.FillRectangle(ForeBrush, 0, 0, StopPos, picTimeRange.ClientSize.Height)
            x.FillRectangle(ForeBrush, StartPos, 0, picTimeRange.ClientSize.Width - StartPos, picTimeRange.ClientSize.Height)
        Else
            x.FillRectangle(BackBrush, 0, 0, picTimeRange.ClientSize.Width, picTimeRange.ClientSize.Height)
        End If

        picTimeRange.Image = myTimeRangeBitmap
 
Back
Top