Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GDI+ Drawing an image to a PictureBox

Status
Not open for further replies.

ehaack

Programmer
Dec 11, 2002
2
US
Using VB.NET... Following an example from from Francesco Balena's 'Programming Microsoft Visual Basic.NET'... Can draw an image on the form, but need to put it inside a picturebox (because, when the user drags the image, it needs to move around the form.)

Here's the code to draw to the form from a numericupdown control, just for the X dimension...(PictureBox2 already contains the image to be skew'ed)

---BEGIN---
Private Sub NumericUpDown4_ValueChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles NumericUpDown4.ValueChanged
Dim gr As Graphics = Me.CreateGraphics
Dim bmp As New Bitmap(PictureBox2.Image)
gr.Clear(Color.White)
DrawSkewImage(gr, bmp, 0, 0, NumericUpDown4.Value, 0)
bmp.Dispose()
gr.Dispose()
End Sub

Sub DrawSkewImage(ByVal gr As Graphics, ByVal bmp As Bitmap, ByVal x As
Single, ByVal y As Single, ByVal dx As Single, ByVal dy As Single)
' Find the position of (x1,y1) and (x2,y2).
Dim x1 As Single = x + bmp.Width
Dim y1 As Single = y + dy
Dim x2 As Single = x + dx
Dim y2 As Single = y + bmp.Height

' Create the points array.
Dim points() As Point = {New Point(x, y), New Point(x1, y1), New
Point(x2, y2)}
' Draw the skewed image
gr.DrawImage(bmp, points)
End Sub

---END---

The above code will read the picturebox.image into the bitmap. The Points Array is created, and the graphic draws the image, with the points array onto the background of the application.

My goal: redirect the drawing into the picturebox, replacing the content of the picturebox with the newly drawn graphic...

Any recommendations?

- e


 
At the moment you are declaring your graphics object on the form. You need to declare it in the picture box

Change
Dim gr As Graphics = Me.CreateGraphics

To
Dim gr As Graphics = Picturebox.CreateGraphics



Just remember that your x y co-ordinates are now measured from the top left of the picture box not the form. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Actually, I solved the problem this way:

Dim currentBitmap As New Bitmap(pbOriginalBitmap.Image)
Dim newBitmap As New Bitmap(newWidth, newHeight)
Dim g As Graphics = Graphics.FromImage(newBitmap)

Then, after the g.drawimage, put newBitmap into the picturebox...

Now... on to bigger fish to fry...

Thanks for the reply!

- Ed
 
I have a set of x,y values in a file to draw on a picturebox and calculate the frequency of the points. How do I plot these files fromthe file?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top