Chapter 23: Image Processing


The Graphics class represents a drawing surface at a logical level. Below that level, a Graphics object is attached to a Bitmap or Metafile object. Those objects understand the slightly lower-level needs of managing more physical data structures. For example, a Bitmap object maps abstract drawing commands such as DrawLine and DrawEllipse to colored pixels that can be displayed on a PictureBox or saved into a file. Similarly, a Metafile maps the Graphics object’s abstract commands into metafile records that you can play back on a drawing surface, or save in a graphical metafile.

This chapter describes the more down-to-earth Bitmap and Metafile classes. It explains methods for building, modifying, and manipulating these objects. It shows how to load and save them from graphics files and, in the case of Bitmap classes, how to work with files saved in a variety of graphic formats such as BMP, GIF, JPEG, TIFF, and PNG.

Image

An Image object represents some sort of picture that you can draw on, copy, transform, and display. Image is an abstract (MustInherit) class, so you cannot create instances of this class directly. Instead you must make instances of its derived classes Bitmap and Metafile.

Tip 

You can also derive your own class from Image if you want, although that’s a fairly advanced technique, so it isn’t covered here.

The Image class provides useful graphical methods that the Bitmap and Metafile classes inherit. Many other objects can work with any type of Image object, so you can pass them either a Bitmap or a Metafile. For example, the Graphics object’s FromImage method takes an Image object as a parameter and returns a Graphics object attached to that Image. This parameter can be either a Bitmap or a Metafile. The following code creates a new Bitmap object, attaches a Graphics object to it, and then uses the Graphics object to draw a rectangle on the Bitmap:

  Dim bm As New Bitmap(100, 100) Using gr As Graphics = Graphics.FromImage(bm)     gr.DrawRectangle(Pens.Black, 10, 10, 80, 80) End Using 

The Image class itself provides several useful methods, particularly Load and Save. The following table describes these and some of the class’s other useful properties and methods.

Open table as spreadsheet

Property or Method

Purpose

Dispose

Frees the resources associated with this image. See the sections “Loading Bitmaps” and “Saving Bitmaps” later in this chapter for more information.

Flags

Returns attribute flags for the image. These provide information such as whether the pixel data contains alpha values and whether the image is a gray scale. For more information, look in the online help for the ImageFlags enumeration.

FromFile

This shared function loads an image from a file as in bm = Bitmap .FromFile(file-name).

FromHbitmap

This shared function loads a Bitmap image from a Windows bitmap handle. (A bitmap handle is a 32-bit integer that gives a value associated with the bitmap in the GDI environment. Windows uses the handle to refer to the bitmap when it needs to manipulate it. In the .NET environment, you generally work with Bitmap and Image objects and don’t need to worry about bitmap handles. It’s useful to know about this method, however, in case you need to manipulate a bitmap loaded using older GDI routines.)

FromStream

This shared function loads an image from a data stream.

GetBounds

Returns a RectangleF structure representing the rectangle’s bounds.

GetPixelFormatSize

Returns the color resolution (bits per pixel) for a specified PixelFormat.

GetThumbnailImage

Returns a thumbnail representation of the image.

Height

Returns the image’s height.

HorizontalResolution

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

IsAlphaPixelFormat

Returns True if the specified PixelFormat contains alpha information.

Palette

Determines the ColorPalette object used by the image.

PhysicalDimension

Returns a SizeF structure giving the image’s dimensions in pixels for Bitmaps and 0.01 millimeter units for Metafiles.

PixelFormat

Returns the image’s pixel format. This property can take such values as Format24bppRgb (24-bit red/green/blue data), Format32bppArgb (32-bit alpha/red/green/blue data), and Format8bppIndexed (8-bit index into a 256-color table). For more information, see the online help for the PixelFormat property and the PixelFormat enumeration.

RawFormat

Returns an ImageFormat object representing the image’s raw format. The ImageFormat class has shared members for each of the standard image types. For example, the following code checks whether the Bitmap bm was loaded from a JPEG file.

If bm.RawFormat.Equals(ImageFormat.Jpeg) Then ...

RotateFlip

Rotates, flips, or rotates and flips the image. The parameter indicates which combination of flips (vertical, horizontal, or both) and rotation (0, 90, 180, or 270 degrees) to use.

Save

Saves the image in a file or stream with a given data format (BMP, GIF, JPEG, and so on). See the sections “Loading Bitmaps” and “Saving Bitmaps” later in this chapter for more information.

Size

Returns a Size structure containing the image’s width and height in pixels.

VerticalResolution

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

Width

Returns the image’s width.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net