Image class methods and properties in GDI+


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

Before we write any imaging code, let's explore the .NET Framework library and see what kind of imaging support it offers. The Bitmap class provides functionality to work with raster images, and the Metafile class provides functionality to work with vector images. Both classes are inherited from the Image class. In this article we will discuss the Image and Bitmap classes and their members.

We'll start this discussion with the Image class, which is defined in the System.Drawing namespace. Understanding this class is important because we will be using its members in our samples throughout this article.

The Image class is an abstract base class for the Bitmap, Metafile, and Icon classes. Some common Image class properties (all read-only) are described in Table 7.2.

The Pixel Format

The pixel format (also known as color depth) defines the number of bits within each pixel. The format also defines the order of color components within a single pixel of data. In the .NET Framework library, the PixelFormat enumeration represents the pixel format.

Beside the properties discussed in Table 7.2, the Image class provides methods, which are described in Table 7.3.

An Image Viewer Application

Now we will write an application that will use some of the properties and methods of the Image class. You will learn to open, view, manipulate, and save images. The application is a simple image viewer.

TABLE 7.2: Image class properties

Property

Description

Flags

Gets or sets attribute flags for an image.

FrameDimensionsList

Returns an array of GUIDs that represent the dimensions of frames within an image.

Height, Width

Return the height and width of an image.

HorizontalResolution

Returns the horizontal resolution, in pixels per inch, of an image.

Palette

Gets or sets the color palette used for an image.

PhysicalDimension

Returns the width and height of an image.

PixelFormat

Returns the pixel format for an image.

PropertyIdList

Returns an array of the property IDs stored in an image.

PropertyItems

Returns an array of PropertyItem objects for an image.

RawFormat

Returns the format of an image.

Size

Returns the width and height of an image.

VerticalResolution

Returns the vertical resolution, in pixels per inch, of an image.

To begin:

  1. Use Visual Studio .NET to create a Windows application project called ImageViewer.
  2. Add a MainMenu control and some menu item to the form.
  3. Change the text of the menu items to File, Open File, Save File and Exit, and the name of these menu items to FileMenu, OpenFileMenu, SaveFileMenu, and ExitMenu, respectively. The final form looks like Figure 7.3.
  4. Write menu click event handlers for the OpenFileMenu, SaveFileMenu, and ExitMenu items by simply double-clicking on them.

The OpenFileMenu click event handler will allow us to browse and select one image and display it, the SaveFileMenu click event handler will save the image as a new file name, and the ExitMenu click event handler will simply close the application.

Before we write code for these menu event handlers, let's see how to create an Image object from a file and how to display it using the DrawImage method of the Graphics class.

TABLE 7.3: Image class methods

MethodDescription

FromFile, FromHbitmap,
FromStream

Creates an Image object from a file, a window handle, and a stream, respectively.

GetBounds

Returns the bounding rectangle for an image.

GetEncoderParameterList

Returns parameters supported by an image encoder.

GetFrameCount

Returns the total number of frames available in an image. Some images include multiple frames. Each frames is a separate layer with different properties. For example, and animated GIF can have multiple frames with different text and other properties.

GetPixelFormatSize

Returns the color depth.

GetPropertyItem

Returns the property item.

GetThumbnailImage

Returns the thumbnail for an image.

IsAlphaPixelFormat

Returns true if the pixel format for an Image object contains alpha information.

IsCanonicalPixelFormat

Returns true if the pixel format is canonical. This is a reserved format.

IsExtendedPixelFormat

Returns true if the pixel format is extended. This is a reserved format.

RemovePropertyItem

Removes the property item.

RotateFlip

Rotate and / or flips an image.

Save

Saves an image in a specified format.

SaveAdd

Takes one parameter of type EncoderParameters that defines parameters required by the image encoder that is used by the save-add operation.

SelectActiveFrame

Selects a frame specified by the dimensions and index. The first parameter of this method is the frame dimensions, which can be used to identify an image by its time, resolution, or page number. The second parameter is the frame index of the active frame. Calling this method causes all changes made to the previous frame to be discarded.

SetPropertyItem

Sets the value of a property item.

Figure 7.3.gif

FIGURE 7.3: A simple image viewer application

Conclusion

Hope the article would have helped you in understanding image class methods and 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.

Up Next
    Ebook Download
    View all
    Learn
    View all