The things I draw don't stay!

Hi all,

my application involves retreiving information of pixels to form an image. I created an access database with 3 field, PixelColumn, Pixel Row and Colour.

I'd then draw rectangles as pixels and fill these pixels with the corresponding colour from the database.

A screenshot is available here.http://i5.tinypic.com/120mipz.jpg

But if I minimize the form and maximize it again, the rectangles disappear!

Is there any way I to make them stay

This is a code sample of my DrawImage subroutine.

------------------------------------------------------------

Private Sub DrawImage()

Dim myBrush As New System.Drawing.SolidBrush (System.Drawing.Color.Aqua)

Dim formGraphics As System.Drawing.Graphics

formGraphics = Me.CreateGraphics()

Dim x As Integer = 10 'Pixel Coordinate

Dim y As Integer = 10

Dim z As Integer = 10 'Pixel Size

Dim MoveNextRowFlag As Integer = 1

While Inc <> MaxRow - 1

Call MoveNext()

If PixelDBCol <> MoveNextRowFlag Then

x = x + z

y = 10

End If

Select Case PixelIntensity

Case 1

myBrush.Color = Color.White

Case 2 : myBrush.Color = Color.LightGray

Case 3 : myBrush.Color = Color.Silver

Case 4 : myBrush.Color = Color.LightSlateGray

Case 5 : myBrush.Color = Color.Gray

Case 6 : myBrush.Color = Color.DimGray

Case 7 : myBrush.Color = Color.DarkSlateGray

Case 8 : myBrush.Color = Color.Black

End Select

formGraphics.FillRectangle(myBrush, New Rectangle(x, y, z, z))

y = y + z

MoveNextRowFlag = PixelDBCol

End While

myBrush.Dispose()

formGraphics.Dispose()

End Sub




Answer this question

The things I draw don't stay!

  • Chris the badger

    New problem..

    Now the image get repainted but the images are cascaded horizontally!

    Is there a command to "wipe clean " the form before repainting

    Thanks.



  • SoftwareAndWhy

     

    drawimage has no arguments which is perfect

    Create a member variable called bDrawImage as a boolean

    inside the form paint routine put:

    If bDrawImage then

      execute the contents of drawimage routine

    endif

    To call your routine the first time...

    bDrawImage =true

    me.refresh

     

    to erase what youve done

    bDrawImage =false

    me.refresh

     



  • ShaneBattrick

    Thanks a lot,

    but when should I call my DrawImage subroutine again

    There isn't a refresh event in Forms.

    I apologise in advance if I asked really the obvious. I hadn't touched VB for ages. I learnt VB4 ages ago.



  • Mike Hull

    What you are seeing is occurring when the form is being displayed again, the content on it is likely out of date and must be redrawn... and in the process of doing so, everything on the form that you drew is also thrown out and must itself be redrawn.

    One of the easiest ways to do this is to simply call your DrawImage method a Paint event handler on your form ala:

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

    DrawImage()

    End Sub

    Just for note though, the PaintEventArgs being passed in contain a Graphics object of its own so you could simplify things by passing that into DrawImage so that you wouldn’t have to create your own.



  • BahKoo



  • suppechasper

    Thanks, it worked.

    I think like you... Using a flag to decide when to repaint.

    Still figuring out.

    Thanks everyone!



  • qdewolf

    "but when should I call my DrawImage subroutine again

    There isn't a refresh event in Forms.

    I apologise in advance if I asked really the obvious. I hadn't touched VB for ages. I learnt VB4 ages ago."

    The paint event is what is called when a refresh is done. In other words... a paint event should call your Drawimage routine.... or your drawimage routine should be put in the forms paint event.



  • Andrey Arkhipov

    Yanno,

    I sory of figured that you aren't bill Gates.

    Of course your bitmaps don't stay.

    when you maximize the form, it's repainted.

    You're going to have repaint the form with your code.

    Here is an example of a form paint routine. You are going to have to create a paint routine that will call your code if a refresh is needed.

    Public Sub FrmToolBar_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

    Dim clr1 As Color = System.Drawing.Color.FromArgb(Settings.Settings.iARGBColor1)

    Dim clr2 As Color = System.Drawing.Color.FromArgb(Settings.Settings.iARGBColor2)

    Dim ColorDisplayLinearGradientBrush As Drawing2D.LinearGradientBrush

    ' If blUseGradientFill Then ' to be put under smp control

    ColorDisplayLinearGradientBrush = New Drawing2D.LinearGradientBrush _

    (Me.ClientRectangle, clr1, clr2, Drawing2D.LinearGradientMode.Horizontal)

    e.Graphics.FillRectangle(ColorDisplayLinearGradientBrush, Me.ClientRectangle)

    'End If

    End Sub



  • JHB

    If you want to clear out the entire form (or drawing surface), on your graphics reference, call the Clear() method and pass in the color of your choice, from your original example it would look like (in this case using the BackColor of your form):

    formGraphics.Clear(Me.BackColor)



  • The things I draw don't stay!