Saving Images & Image Format Properties in GDI+ using VB.NET

In this article I will explain about Saving Images & Image Format Properties in GDI+.
  • 4686
Now we move to the Save File menu item. It allows you to save images in different file formats.
 
The Image class provides the Save method, which is used to save images to a specified format. The Save method takes a fine name (as string type) or a stream (a stream object), and a specified format of type ImageFormat class. Table 7.4 describes the properties of the ImageFormat class.
 
Note: The Emf and Wmf properties in the ImageFormat enumeration do not save a real metafile, but save the bitmap as one metafile record. It will still be a bitmap.
 
TABLE 7.4: ImageFormat properties
 

Property

Description

Bmp

Specifies BMP format.

Emf

Specifies EMF (Enhanced Metafile Format).

Exif

Specifies EXIF format.

Gif

Specifies GIF format.

Guid

Specifies a GUID structure that represents the ImageFormat object.

Icon

Specifies Windows icon format.

Jpeg

Specifies JPEG format.

MemoryBmp

Specifies memory bitmap format.

Png

Specifies PNG format.

Tiff

SpecifiesTiff format.

Wmf

Specifies WMF (Windows Metafile Format).


Now we add code for the SaveFileMenu click event handler, as shown in Listing 7.3. We use SaveFileDialog, which lets us specify the file name and saves an image using the format specified in the dialog. We read the extension of the file name entered by the user, and on that basis we pass the ImageFormat property in the Save method.

 
Note: The ImageFormat enumeration is defined in the System.Drawing.Imaging namespace. Don't forget to add a reference to this namespace in your application.
 
LISTING 7.3: Using the Save method to save images

    Private Sub SaveFileMenu_Click(ByVal sender As Object)
        'If image is created
        If curImage Is Nothing Then
            Return
        End If

        'Call SaveFileDialog
        Dim saveDlg As SaveFileDialog = New SaveFileDialog()
        saveDlg.Title = "Save Image As"
        saveDlg.OverwritePrompt = True
        saveDlg.CheckPathExists = True
saveDlg.Filter =
"Bitmap File (*.bmp) | *.bmp |" +
"Gif File (*.gif) | *.gif | " +
"JPEG File (*.jpg) | *.jpg" +
"PNG File (*.png) | *.png"
        saveDlg.ShowHelp = True

        'If selected, save
        If saveDlg.ShowDialog() = DialogResult.OK Then
            'Get the user-selected file name
            Dim fileName As String = saveDlg.FileName

            'Get the extension
string strFilExtn =
            fileName.Remove(0, fileName.Length - 3)

            'Save file
            Select Case strFilExtn
                Case "bmp"
                    curImage.Save(fileName, ImageFormat.Bmp)
                    Exit Sub
                Case "jpg"
                    curImage.Save(fileName, ImageFormat.Jpeg)
                    Exit Sub
                Case "gif"
                    curImage.Save(fileName, ImageFormat.Gif)
                    Exit Sub
                Case "tif"
                    curImage.Save(fileName, ImageFormat.Tiff)
                    Exit Sub
                Case "png"
                    curImage.Save(fileName, ImageFormat.Png)
                    Exit Sub
                Case Else
                    Exit Sub
            End Select
        End If
    End Sub

Now we write code for the ExitMenu click event handler. This menu simply closes the application. Hence we call the Form.Close method on this event handler, as shown in Listing 7.4.
 
LISTING 7.4: The ExitMenu click event handler

    Private Sub ExitMenu_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

Retrieving Image Properties
 
Table 7.2 listed the Image class properties. Now we will read and display the properties of an image. We add a Properties menu item to the main menu and write the code in Listing 7.5 as this menu click event handler. We read the size, format, resolution, and pixel format of an image.
 
LISTING 7.5: Getting image properties

   Private Sub PropertiesMenu_Click(ByVal senderAs Object,ByVal e As System.EventArgs)
        If curImage IsNot Nothing Then
            'Viewing image properties
            Dim imagePropertiesAs String ="Size:" & curImage.Size
            imageProperties += "," & vbLf & " RawFormat:" & curImage.RawFormat.ToString()
            imageProperties += "," & vbLf & " Vertical Resolution:" & curImage.VerticalResolution.ToString()
            imageProperties += "," & vbLf & " Horizontal Resolution:" & curImage.HorizontalResolution.ToString()
            imageProperties += "," & vbLf & " PixelFormat:" & curImage.PixelFormat.ToString()
            MessageBox.Show(imageProperties)
        End If
   End Sub
 
Figure 7.6 shows the properties of an image.
 
Figure207_6.gif
 
FIGURE 7.6 Reading the properties of an image
 
Conclusion
 
Hope the article would have helped you in understanding Saving Images & Image Format Properties in GDI+. Read other articles on GDI+ on the website.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.