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