none
convert graphic to bitmap RRS feed

  • Question

  • I have dawn a graph on a graphic and is need to convert this to a bitmap.  How do I do this?

    Next i need to disply the bitmap and draw more graphs on it.  How is it done?

    Any advise would be appreciated. Thanks in advance.

     

    Wednesday, March 1, 2006 8:14 PM

Answers

  • Dim b as Bitmap = new Bitmap(400, 400, PixelFormat.whatever)

    Dim plot as Graphics = Graphics.FromBitmap(b)

    Now do your drawing as before

    b.Save("myFile.bmp")

    Now you can load it with

    Dim b as Bitmap = Bitmap.FromFile("myFile.bmp")

    and you can load that into a graphics object as before, and draw onto it.

    Now, to draw it, handle the Paint event of the form.  You do that by clicking on the form, then the lightning bolt in the properties dialog, then find 'Paint' and type 'OnPaint' in the edit box next to it.

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

    e.Graphics.DrawImageUnscaled(b, 0, 0)

    End Sub

    That's my code with adding the event, and drawing the image unscaled at hte top left.  There is also a DrawImage method that allows scaling.  Note that b is NOT a good name for your bitmap, and that the Bitmap needs to be a member variable ( visible to the whole class ).

     

    Wednesday, March 1, 2006 10:13 PM
    Moderator

All replies

  • Do you mean you've drawn onto a Graphics class, presumably in OnPaint ? Instead, create a Bitmap, create a Graphics instance from Graphics.FromBitmap, and then when you've drawn on your bitmap, draw that to the screen.

    To draw it to the screen, use the Graphics object passed to OnPaint, and one of the DrawImage overloads it has.  If you make this bitmap a member variable, you can draw on it where-ever you like and call Invalidate() to make a paint message occur, drawing it to the screen ( because you wrote code there to draw it to the screen )

    Wednesday, March 1, 2006 8:44 PM
    Moderator
  • Sorry, you lost me, i'm quite a novice!

    I was given a PlotArea as Panel object and created a graphics; 

    Dim Plot As Graphics = PlotArea.CreateGraphics()

     I then drew on Plot.  Now  I want to save  Plot as a Bitamp and later display it again. Preferably i would like to draw on the bitmap lter on.

    Wednesday, March 1, 2006 10:02 PM
  • Dim b as Bitmap = new Bitmap(400, 400, PixelFormat.whatever)

    Dim plot as Graphics = Graphics.FromBitmap(b)

    Now do your drawing as before

    b.Save("myFile.bmp")

    Now you can load it with

    Dim b as Bitmap = Bitmap.FromFile("myFile.bmp")

    and you can load that into a graphics object as before, and draw onto it.

    Now, to draw it, handle the Paint event of the form.  You do that by clicking on the form, then the lightning bolt in the properties dialog, then find 'Paint' and type 'OnPaint' in the edit box next to it.

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

    e.Graphics.DrawImageUnscaled(b, 0, 0)

    End Sub

    That's my code with adding the event, and drawing the image unscaled at hte top left.  There is also a DrawImage method that allows scaling.  Note that b is NOT a good name for your bitmap, and that the Bitmap needs to be a member variable ( visible to the whole class ).

     

    Wednesday, March 1, 2006 10:13 PM
    Moderator
  • Thanks you very much, your detailed explanation was very helpfull.  Two issues though;

    a) The compiler did not like the second line

    Dim plot as Graphics = Graphics.FromBitmap(b)

    It clomplained about 'FromBitMap' not being a member of Graphics.  I later used 'FromImage'.  That seems to work.  Was it a typo of is it something else?

    b) The bitmap appears to be behind a TabPage control, I can just see a piece of it wher the tabs end.  So far I've not had succes to send this to the back and cant figure out how to bring the bitmap to the front.  I'm still trying though.  

    Thursday, March 2, 2006 1:30 PM
  • Thank you, I got it working.  The Last problem was because the paint event handler was declared for the form and not the TabPage Control.
    Thursday, March 2, 2006 3:10 PM