Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How to save the image of Picture box of VB.Net 2003

Do you know how to save the image which is drawn in the picture box? Does [color=Blue]PictureBox1.Save[/color] procedure have been existed in VB.Net 2003? Please help to me.
JMH

Comments

  • DrMartenDrMarten Member Posts: 748
    Hi,

    With two buttons and one PictureBox on a FORM try this code please.

    Make sure to type .bmp or . jpg at the end of an NEW file name.

    This example shows saving in Jpeg or Bitmap file format only.

    Other

    System.Drawing.Imaging.ImageFormat.

    formats are available. See for yourself what is available after you type the last dot within VB.Net.

    :-)


    Regards,

    Dr M

    [code]
    Option Strict On
    Public Class Form1

    'This saves the BackgroundImage of PictureBox1.>>
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    If Not (PictureBox1.BackgroundImage) Is Nothing Then
    Dim sfd As New SaveFileDialog
    sfd.Title = "Save your BackgroundImage picture as...."
    sfd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyPictures
    sfd.Filter = "Jpeg files (*.jpg)|*.jpg|Bitmap files (*.bmp)|*.bmp"

    Dim result As DialogResult = sfd.ShowDialog
    If result = Windows.Forms.DialogResult.OK Then
    If sfd.FileName <> String.Empty Then
    'Set to Jpeg by default.
    Dim MyImageFormat As System.Drawing.Imaging.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg
    If sfd.FileName.ToString.ToUpper.EndsWith("JPG") Then
    MyImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg
    ElseIf sfd.FileName.ToString.ToUpper.EndsWith("BMP") Then
    MyImageFormat = System.Drawing.Imaging.ImageFormat.Bmp
    End If
    PictureBox1.BackgroundImage.Save(sfd.FileName, MyImageFormat)
    End If
    End If
    End If

    End Sub

    'This saves the Image of PictureBox1.>>
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    If Not (PictureBox1.Image) Is Nothing Then
    Dim sfd As New SaveFileDialog
    sfd.Title = "Save your Image picture as...."
    sfd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyPictures
    sfd.Filter = "Jpeg files (*.jpg)|*.jpg|Bitmap files (*.bmp)|*.bmp"

    Dim result As DialogResult = sfd.ShowDialog
    If result = Windows.Forms.DialogResult.OK Then
    If sfd.FileName <> String.Empty Then
    'Set to Jpeg by default.
    Dim MyImageFormat As System.Drawing.Imaging.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg
    If sfd.FileName.ToString.ToUpper.EndsWith("JPG") Then
    MyImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg
    ElseIf sfd.FileName.ToString.ToUpper.EndsWith("BMP") Then
    MyImageFormat = System.Drawing.Imaging.ImageFormat.Bmp
    End If
    PictureBox1.Image.Save(sfd.FileName, MyImageFormat)
    End If
    End If
    End If

    End Sub
    End Class
    [/code]
Sign In or Register to comment.