Saving Images & Image Format Properties in GDI+

In this article I will explain about Saving Images & Image Format Properties in GDI+.


This article has been excerpted from book "Graphics Programming with GDI+".

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

Specifies Tiff 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 void SaveFileMenu_Click (object sender System.EventArgs e)
{
            //If image is created
            if (curImage == null)
                return;

            //Call SaveFileDialog
            SaveFileDialog saveDlg = 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)
            {
                //Get the user-selected file name
                string fileName = saveDlg.FileName;

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

                //Save file
                switch (strFilExtn)
                {
                    case "bmp":
                         curImage.Save(fileName, ImageFormat.Bmp);
                        break;
                    case "jpg":
                         curImage.Save(fileName, ImageFormat.Jpeg);
                        break;
                    case "gif":
                         curImage.Save(fileName, ImageFormat.Gif);
                        break;
                    case "tif":
                         curImage.Save(fileName, ImageFormat.Tiff);
                        break;
                    case "png":
                         curImage.Save(fileName, ImageFormat.Png);
                        break;
                    default:
                        break;
                }
            }
}

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 void ExitMenu_Click (object sender, System.EventArgs e)
{
             this.Close();
}

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 void PropertiesMenu_Click (object sender, System.EventArgs e)
{
        if (curImage!=null)
        {
        //Viewing image properties
        string imageProperties = "Size:" + curImage.Size;
        imageProperties +=",\n RawFormat:"+ curImage.RawFormat.ToString();
        imageProperties +=",\n Vertical Resolution:"+ curImage.VerticalResolution.ToString();
        imageProperties +=",\n Horizontal Resolution:"+ curImage.HorizontalResolution.ToString();
        imageProperties +=",\n PixelFormat:"+ curImage.PixelFormat.ToString();
         MessageBox.Show(imageProperties);
        }
}

Figure 7.6 shows the properties of an image.

Figure 7.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.

bookGDI.jpg
This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.