|
|
Title | Load an image and save it in a different image format (GIF, JPG, TIFF, etc.) in VB .NET |
Keywords | image, VB.NET, type, BMP, bitmap, GIF, JPG, JPEG, TIF, TIFF, PNG, EMF, WMF, ICO, icon, Exif |
Categories | Graphics, VB.NET, Utilities |
|
|
To load an image, set the PictureBox's Image property to a new Bitmap object initialized by the image file's name.
|
|
' Load a file.
Private Sub mnuFileOpen_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
mnuFileOpen.Click
If dlgOpen.ShowDialog() = DialogResult.OK Then
picImage.Image = New Bitmap(dlgOpen.FileName)
Me.Width = picImage.Width + Me.Width - _
Me.ClientSize.Width
Me.Height = picImage.Height + Me.Height - _
Me.ClientSize.Height
Dim file_name As String = dlgOpen.FileName
file_name = _
file_name.Substring(file_name.LastIndexOf("\") _
+ 1)
Me.Text = "[" & file_name & "]"
dlgSave.FileName = dlgOpen.FileName
End If
End Sub
|
|
To save the image with a particular file type, call the Image object's Save method passing it the file name and image type.
|
|
' Save the file.
Private Sub mnuFileSave_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
mnuFileSave.Click
If dlgSave.ShowDialog() = DialogResult.OK Then
Dim extension As String = dlgSave.FileName
extension = _
extension.Substring(extension.LastIndexOf(".") _
+ 1).ToLower
Select Case extension
Case "bmp"
picImage.Image.Save(dlgSave.FileName, _
ImageFormat.Bmp)
Case "jpg", "jpeg"
picImage.Image.Save(dlgSave.FileName, _
ImageFormat.Jpeg)
Case "gif"
picImage.Image.Save(dlgSave.FileName, _
ImageFormat.Gif)
Case "ico"
picImage.Image.Save(dlgSave.FileName, _
ImageFormat.Icon)
Case "emf"
picImage.Image.Save(dlgSave.FileName, _
ImageFormat.Emf)
Case "wmf"
picImage.Image.Save(dlgSave.FileName, _
ImageFormat.Wmf)
Case "png"
picImage.Image.Save(dlgSave.FileName, _
ImageFormat.Png)
Case "tif", "tiff"
picImage.Image.Save(dlgSave.FileName, _
ImageFormat.Tiff)
Case "exif"
picImage.Image.Save(dlgSave.FileName, _
ImageFormat.Exif)
End Select
End If
End Sub
|
|
|
|
|
|