By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 883 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

Saving drawing graphics

P: n/a
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.
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

Steve Marshall wrote:
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 :)

--
Larry Lard
Replies to group please

Nov 21 '05 #2

P: n/a
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.

"Larry Lard" <la*******@hotmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...

Steve Marshall wrote:
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 :)

--
Larry Lard
Replies to group please

Nov 21 '05 #3

P: n/a
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.Blu e, 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" <st****@westnet.net.au> wrote in message
news:11***************@quartz.westnet.net.au...
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.

"Larry Lard" <la*******@hotmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...

Steve Marshall wrote:
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 :)

--
Larry Lard
Replies to group please


Nov 21 '05 #4

P: n/a

Steve Marshall wrote:
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

"Larry Lard" <la*******@hotmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...

Steve Marshall wrote:
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 :)

--
Larry Lard
Replies to group please


Nov 21 '05 #5

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

Cheers

"Steve Marshall" <st****@westnet.net.au> wrote in message
news:11***************@quartz.westnet.net.au...
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.

"Larry Lard" <la*******@hotmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...

Steve Marshall wrote:
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 :)

--
Larry Lard
Replies to group please


Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.