PictureBox in Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

This lesson describes the PictureBox control


This control displays images, and also provides a Graphics object that you can use to draw various shapes at runtime.

Its Image property determines the picture that the control displays, while its SizeMode property determines how the image is sized to fit into the control.


Its Size, Width, and Height properties give info about the control size, including its border if it has one. The ClientRectangle, ClientSize, and DisplayRectangle properties give info about the area inside the control, not including its border.

There are two ways to draw on a Bitmap, and then set the control's Image property to the Bitmap.

One way is to place the drawing commands in the PictureBox control's Paint event, and another to make a Bitmap that fits the PictureBox, draw on the Bitmap and finally display in the PictureBox control.

    Private Sub WatermarkButton_Click(sender As Object, e As EventArgs) _
     Handles WatermarkButton.Click
        Dim bmp As New Bitmap(PictureBox1.Width, PictureBox1.Height)
        'create a new graphics from the bmp
        Dim grf As Graphics = Graphics.FromImage(bmp)

        ' set the rendering quality
        grf.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
        grf.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
        grf.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        grf.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit

        Dim font As Font = New Font("Arial", 12)
        grf.FillRectangle(Brushes.White, 0, 0, PictureBox1.Width, _
        grf.DrawImage(PictureBox1.Image, 0, 0)
        ' draw the string at the bottom of the image
        grf.DrawString("www.visual-basic-tutorials.com", font, _
         Brushes.Black, 200, 300)

        ' set the image property 
        PictureBox1.Image = bmp
    End Sub

    Private Sub SaveButton_Click(sender As Object, e As EventArgs) _
     Handles SaveButton.Click
        ' save the image to the desktop
        PictureBox1.Image.Save(IO.Path.Combine( _
            My.Computer.FileSystem.SpecialDirectories.Desktop, _
             "watermarked.jpg"), Imaging.ImageFormat.Jpeg)
    End Sub

example before

example after

Previous Lesson | Next Lesson

Share This