Saving drawing graphics

S

Steve Marshall

Hi all,

I am converting an app which used a picturebox to draw graphs etc onto, then
saved them to a file. I can certainly draw things onto a picturbox in
VB.NET, but how do I save them to a file? I've looked at Bitmap objects,
which support saving, but can't see a way to grab what I have drawn and make
a bitmap from it, or whatever. Anybody done this?

Thanks for any suggestions

Remove numerals from e-mail address if e-mailing.
 
L

Larry Lard

Steve said:
Hi all,

I am converting an app which used a picturebox to draw graphs etc onto, then
saved them to a file. I can certainly draw things onto a picturbox in
VB.NET, but how do I save them to a file? I've looked at Bitmap objects,
which support saving, but can't see a way to grab what I have drawn and make
a bitmap from it, or whatever. Anybody done this?

<http://www.bobpowell.net/gdiplus_faq.htm>

Read and be enlightened :)
 
S

Steve Marshall

Thanks for that Larry. I had actually found that page myself. But I'm
still not completely enlightened...

OK, I get that a PictureBox is not the "right" way to draw things in VB.
But it's still not clear to me what IS the right way! All I need to do is
draw a grid and a few lines, plus a bit of text, then save it to a file. It
ain't rocket science, which is why I was using a simple PictureBox in VB6,
which of course worked beautifully. I haven't looked at every article on
the page, but thus far have not ofund a "recipe" for simple drawing and
saving to file.
 
R

Robin Tucker

It's quite easy really:

Public Sub CreateAndSave()

Dim theBitmap As New Bitmap(400, 400)
Dim theGraphics As Graphics

theGraphics = Graphics.FromImage(theBitmap)
theGraphics.FillEllipse(System.Drawing.Brushes.Blue, New Rectangle(0, 0,
100, 100))

theBitmap.Save("c:\theFile.bmp", System.Drawing.Imaging.ImageFormat.Bmp)

End Sub

Dim theGraphics as Graphics = PictureBox1.
Steve Marshall said:
Thanks for that Larry. I had actually found that page myself. But I'm
still not completely enlightened...

OK, I get that a PictureBox is not the "right" way to draw things in VB.
But it's still not clear to me what IS the right way! All I need to do is
draw a grid and a few lines, plus a bit of text, then save it to a file.
It ain't rocket science, which is why I was using a simple PictureBox in
VB6, which of course worked beautifully. I haven't looked at every
article on the page, but thus far have not ofund a "recipe" for simple
drawing and saving to file.
 
L

Larry Lard

Steve said:
Thanks for that Larry. I had actually found that page myself.

So much for my easy way out :)
But I'm
still not completely enlightened...

OK, I get that a PictureBox is not the "right" way to draw things in VB.
But it's still not clear to me what IS the right way! All I need to do is
draw a grid and a few lines, plus a bit of text, then save it to a file.

If by this you mean that you are doing things entirely
programmatically, with no user interaction, then yes, a PictureBox is
inappropriate. VB6 needed to do things this way because it had no way
of manipulating a conceptual 'image' - the only thing if could draw on
was a PictureBox. However, with .NET, we have a formal Image and a
Graphics, and all a PictureBox is is a way of *displaying to the user*
an Image. If we don't need to involve the user, we don't need a
PictureBox at all.

Once you have these concepts straight it's just a matter of finding the
framework functionality that does what you want.

It
ain't rocket science, which is why I was using a simple PictureBox in VB6,
which of course worked beautifully. I haven't looked at every article on
the page, but thus far have not ofund a "recipe" for simple drawing and
saving to file.

Imports System.Drawing
Imports System.Drawing.Imaging

' in a procedure now:
Dim bmp As Bitmap = New Bitmap(128, 128,
Imaging.PixelFormat.Format24bppRgb)

Dim g As Graphics = Graphics.FromImage(bmp)

g.Clear(Color.AliceBlue)

g.DrawRectangle(Pens.Bisque, 30, 60, 40, 50)
g.FillRectangle(Brushes.LightGreen, 100, 40, 10, 60)
g.DrawString("some text", New Font(FontFamily.GenericSansSerif,
12, FontStyle.Bold, GraphicsUnit.Point), Brushes.Red, 50, 50)

bmp.Save("c:\temp\mynewimage.bmp", ImageFormat.Bmp)

Things to note:

- Bitmap is a subclass of Image, so look in both classes' docs for
interesting stuff
- Graphics.FromImage is obviously the key method for obtaining a
drawing surface (a Graphics) for a given Image object
- Note that there is no Form in sight here - this code would work just
as well in a Class Library as in a Windows Forms app
 
S

Steve Marshall

Many thanks for the responses. I'm sure they will set me on the path of
drawing righteousness!

Cheers

Steve Marshall said:
Thanks for that Larry. I had actually found that page myself. But I'm
still not completely enlightened...

OK, I get that a PictureBox is not the "right" way to draw things in VB.
But it's still not clear to me what IS the right way! All I need to do is
draw a grid and a few lines, plus a bit of text, then save it to a file.
It ain't rocket science, which is why I was using a simple PictureBox in
VB6, which of course worked beautifully. I haven't looked at every
article on the page, but thus far have not ofund a "recipe" for simple
drawing and saving to file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top