Share via


FAQ: How do I draw an image respectively on the PictureBox control and Image object?

Question

Friday, April 10, 2009 2:40 PM

How do I draw an image respectively on the PictureBox control and Image object?

 


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

All replies (1)

Friday, April 10, 2009 2:41 PM âś…Answered

The key is to find the correct Graphics object.

*Prerequisites: Drag&drop PictureBox1, Button1 and Button2 onto Form1.

*

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        PictureBox1.Image = Image.FromFile("D:\VBproject\2.jpg")
    End Sub

    'Draw image on PictureBox control
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim newImage As Image = Image.FromFile("D:\VBproject\1.jpg")
        Dim g As Graphics = Me.PictureBox1.CreateGraphics
        g.DrawImage(newImage, New Rectangle(10, 10, 200, 200))
    End Sub

    'Draw image on PictureBox.Image
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim newImage As Image = Image.FromFile("D:\VBproject\1.jpg")
        Dim g As Graphics = Graphics.FromImage(PictureBox1.Image)
        g.DrawImage(newImage, New Rectangle(10, 10, 200, 200))
        PictureBox1.Refresh()
    End Sub
End Class

 

Related thread:

http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/7c7ee412-703e-46d8-84c7-b3f91dfefdc7/

** **

For more FAQ about Visual Basic .NET General, please see Visual Basic .NET General FAQ

 


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.