Making graphics on form load?

bonedoc

Well-known member
Joined
May 4, 2006
Messages
112
Programming Experience
Beginner
I have a procedure that allows me to draw an ellipse. I works great when I cause this to happen with the even of clicking a button. But, when I insert the exact same code in the form_load Sub, nothing happens. Is there a reason for this?

Here is what I am trying to do:

dim mypen as new pen = pen(color.red)
dim design1 as graphics = picturebox1.creategraphics
design1.drawellipse(mypen, 10, 10, 10,10)


works fine with a button click, but not on the form load...wierd. It acutally goes through the code, but nothing is drawn. It does get drawn in a button click event.
 
Last edited:
I havent done much painting with the picturebox control but htis may have something to do with order things are carried out. When you use ...CreateGraphics you create a new graphics surface to draw on. The picture box like all objects that inherit from control raises it's own paint event. Try doing your painting there using the graphics object passed inside the subs arguments..normally 'e'
 
How do I save a pic AND graphics?

I have a picturebox that I am saving its image to a file. It works fine, but if I draw any graphics on the picturebox, they are not saved, just the picture is. Is there a way to save both? Basicaly, I use this to create graphics at different times:

Dim GFX as graphics = picturebox1.CreateGraphics
gfx.drawEllipse(MyPen, 10,10,10,10)

then later, I want to save the image and graphics together. I can save, but no graphics appear. If I use this, i get a blank image:

dim bmp as new bitmap(Picturebox1.width, picturebox1.height, imaging.pixelformat.formay32bbpaRGB)
Dim g as Graphics = Graphics.FromImage(bmp)
bmp.Save(DestinationFile)
 
Last edited:
Trouble with pencil drawing tool

I am trying to make a pencil tool that draw where the mouse is when it is held down. It draws, but not where the pointer is. In fact, the further down and right I go, the furth the pointer is away from the line being draw. If I move the posiotn of the window the picturebox is in, to the upper left, the values get closer together, but still move apart as I go to the lower right. I can't figure out what the deal is. Can someone explain this?

if flag_draw then
dim GFX as Graphics = Graphics.FromImage(PictureBox1.Image)
GFX.DrawEllipse(MyPen, MousePosition.X, MousePosition.Y, 1, 1)
PictureBox1.Refresh
GFX.Dispose
end if
 
I am having trouble for some reason. This code works great:

dim GFX as Graphics = PictureBox1.CreateGraphics
GFX.DrawEllipse(MyPen, e.X, e.Y, 1, 1)

The only problm, is that I need to save the graphics on the image, so I use this:

dim GFX as graphics = graphics.fromimage(picturebox1.image)
GFX.DrawEllipse(MyPen, e.X, e.Y, 1, 1)

GFX.Refresh
GFX.dispose


This does work, but again, the area the drawing occurs is not where the mouse is. I dont understand.
 
Last edited:
Going Crazy with Graphics Problems....

:eek:
Ok, I do not get this. I can draw an graphics onto an image like this:

dim i as image = picturebox1.image
dim g as graphics = graphics.fromimage(picturebox1.image)
draw ellipse(mypen, 20, 20,10,10)
picturebox1.refresh
''''''''''''
I can draw like a paintbrush like this (but not saved on image):

dim g as graphics = picturebox1.creategraphics
draw ellipse(mypen, e.X, e.Y,10,10)
''''''''''''''''''
But, when I try to do the above paintbrush and save it to the image like the first case, I get problems:

dim i as image = picturebox1.image
dim g as graphics = graphics.fromimage(picturebox1.image)
draw ellipse(mypen, e.X, e.Y,10,10)
picturebox1.refresh
'''''''''''''''
When I do this, it will draw, but the drawing does not occure anywhere near the mouse. Why would case 1 save the graphics in the correct position? Why would case 2 draw the graphics in the correct location, but case 3 draw in the incorrect location? Sorry, I know this is kind of a repost. I accidentally said I had it figured out in the last post, so I don't think anyone helped after that(or maybe they just didn't want to).
 
There are some basic graphics concepts you don't understand, here is a site with much info and small samples, Bob Powells GDI FAQ http://www.bobpowell.net/faqmain.htm (esp #1)

Your code questions above is incomplete and out of context, hard to tell where you're going wrong. What is "draw ellipse(...)"? Should that be "g.DrawEllipse(...)"? What events are you working with when you use e.X/e.Y?
 
I have been through that entire site, but no luck. That is suppost to be g.DrawEllipse(..). What I am trying to do is draw an ellipse on the screen where the mouse is located. I can do it this way:
VB.NET:
dim g as graphics = picturebox1.creategraphics
g.DrawEllipse(mypen, e.X, e.Y,10,10)
But, when I try to draw the graphics to the image using g = graphics.fromimage(picturebox1.image), the graphics drawn is nowhere near the mouse. I am basically trying to make a paintbrush tool on the mouse move event, so I can draw ONTO the image in the picturebox.
 
Last edited by a moderator:
Here's an example, see if it helps:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Canvas [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Bitmap
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] md [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Boolean[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] mdptdown, mdptup [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Point[/SIZE]
 
[/SIZE][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Form2_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) _
[/SIZE][SIZE=2][COLOR=#0000ff]Handles [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load[/SIZE]
[SIZE=2] Canvas = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Bitmap([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].PictureBox1.Width, [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].PictureBox1.Height, _
 System.Drawing.Imaging.PixelFormat.Format32bppArgb)
 PictureBox1.Image = Canvas
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] PictureBox1_MouseDown([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.MouseEventArgs) _
[/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] PictureBox1.MouseDown
 md = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2] mdptdown = e.Location
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] PictureBox1_MouseMove([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.MouseEventArgs) _
[/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] PictureBox1.MouseMove
[/SIZE][SIZE=2][COLOR=#0000ff] If[/COLOR][/SIZE][SIZE=2] md = [/SIZE][SIZE=2][COLOR=#0000ff]True [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]  mdptup = e.Location
  PictureBox1.Refresh() [/SIZE][SIZE=2][COLOR=#008000]'causes paint event
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] PictureBox1_MouseUp([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.MouseEventArgs) _
[/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] PictureBox1.MouseUp
 md = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2] mdptup = e.Location
[/SIZE][SIZE=2][COLOR=#008000] 'draw permanent
[/COLOR][/SIZE][SIZE=2] myDrawEllipse(Graphics.FromImage(Canvas))
 PictureBox1.Image = Canvas
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] myDrawEllipse([/SIZE][SIZE=2][COLOR=#0000ff]ByRef[/COLOR][/SIZE][SIZE=2] g [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Graphics)
[/SIZE][SIZE=2][COLOR=#0000ff] Dim[/COLOR][/SIZE][SIZE=2] x [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = Math.Min(mdptdown.X, mdptup.X)
[/SIZE][SIZE=2][COLOR=#0000ff] Dim[/COLOR][/SIZE][SIZE=2] y [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = Math.Min(mdptdown.Y, mdptup.Y)
[/SIZE][SIZE=2][COLOR=#0000ff] Dim[/COLOR][/SIZE][SIZE=2] wid [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = Math.Abs(mdptdown.X - mdptup.X)
[/SIZE][SIZE=2][COLOR=#0000ff] Dim[/COLOR][/SIZE][SIZE=2] hgt [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = Math.Abs(mdptdown.Y - mdptup.Y)
[/SIZE][SIZE=2][COLOR=#0000ff] Dim[/COLOR][/SIZE][SIZE=2] rct [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Rectangle(x, y, wid, hgt)
 g.DrawEllipse(Pens.Black, rct)
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] PictureBox1_Paint([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.PaintEventArgs) _
[/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] PictureBox1.Paint
[/SIZE][SIZE=2][COLOR=#008000]'draw not permanent
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] md = [/SIZE][SIZE=2][COLOR=#0000ff]True [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2] myDrawEllipse(e.Graphics)
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
Canvas/PictureBox1.Image may be saved as it is displayed.
 
Top help. Cheers John. Used the graphicspath instead of drawellipse to create the signature. Tiniest little bug left but I'll sort it. Thanks a shed load fella.
 
Back
Top