Handling Images


The Image class provides the base class for all the image classes in System:: Drawing, especially the Bitmap class, which is used to represent and manipulate images in a number of formats.

Bitmap has a number of useful properties and methods, which are summarized in the following two tables.

Property

Description

Height

Gets the height of the bitmap in pixels

HorizontalResolution

Gets the horizontal resolution, in pixels per inch

Palette

Gets or sets the color palette used by this image

PixelFormat

Gets or sets the PixelFormat value that describes the color format of the pixels

Size

Gets the width and height of this bitmap as a Size object

VerticalResolution

Gets the vertical resolution, in pixels per inch

Width

Gets the width of the bitmap in pixels

Method

Description

FromFile

Creates a bitmap from data in a file

FromHbitmap

Creates a bitmap from a Windows HBITMAP

FromStream

Creates a bitmap from data from a stream

RotateFlip

Rotates and/or flips the image

Save

Saves the image in a given format

You can see the formats that are supported by looking at the ImageFormat enumeration.

The following exercise illustrates how to display a bitmap on a form.

  1. Find the Form1_Paint function that handles Paint events. You need to display the bitmap in the Paint event handler so that it gets redisplayed when the form needs to refresh itself. Add the following code to Form_Paint:

    // Draw the image Bitmap* bmp = new Bitmap(S"ramp1.gif"); gr->DrawImage(bmp, 10,10);

    The code creates a Bitmap object that reads data from a GIF file. The call to DrawImage then draws the image at position (10,10) on the form. The image I’ve used forms a vertical stripe down the left side of the form.

    Note

    You’ll find the ramp1.gif file in the CppDraw project directory of the sample code that accompanies this book. Copy it into your project directory to use it in this code.

  2. Build and run the application. You’ll see a bitmap displayed on the form.




Microsoft Visual C++  .NET(c) Step by Step
Microsoft Visual C++ .NET(c) Step by Step
ISBN: 735615675
EAN: N/A
Year: 2003
Pages: 208

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