< C++ .NET Drawing & Printing 4 | Main | C++ .NET Drawing & Printing 6 >


 

 

Graphical Output: Simple Drawing and Printing 5

 

 

What we have in this page?

  1. Handling Images

  2. Printing

 

 

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 class encapsulates a GDI+ bitmap, which consists of the pixel data for a graphics image and its attributes. A Bitmap is an object used to work with images defined by pixel data. The following tables list the members exposed by the Bitmap type.

 

Public Constructors

Symbol

Public method

Name

Description

Bitmap

Overloaded. Initializes a new instance of the Bitmap class.

 

Table 4

 

 

Public Properties

 

Symbol

Public property

Name

Description

Flags

Gets attribute flags for the pixel data of this Image. (Inherited from Image.)

FrameDimensionsList

Gets an array of GUIDs that represent the dimensions of frames within this Image. (Inherited from Image.)

Height

Gets the height, in pixels, of this Image. (Inherited from Image.)

HorizontalResolution

Gets the horizontal resolution, in pixels per inch, of this Image. (Inherited from Image.)

Palette

Gets or sets the color palette used for this Image. (Inherited from Image.)

PhysicalDimension

Gets the width and height of this image. (Inherited from Image.)

PixelFormat

Gets the pixel format for this Image. (Inherited from Image.)

PropertyIdList

Gets IDs of the property items stored in this Image. (Inherited from Image.)

PropertyItems

Gets all the property items (pieces of metadata) stored in this Image. (Inherited from Image.)

RawFormat

Gets the format of this Image. (Inherited from Image.)

Size

Gets the width and height, in pixels, of this image. (Inherited from Image.)

Tag

Gets or sets an object that provides additional data about the image. (Inherited from Image.)

VerticalResolution

Gets the vertical resolution, in pixels per inch, of this Image. (Inherited from Image.)

Width

Gets the width, in pixels, of this Image. (Inherited from Image.)

 

Table 5

 

 

 

 

 

Public Methods

Symbol

Public method

Name

Description

Clone

Overloaded. Creates a copy of the section of this Bitmap defined with a specified PixelFormat.

CreateObjRef

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)

Dispose

Releases all resources used by this Image. (Inherited from Image.)

Equals

Overloaded. Determines whether two Object instances are equal. (Inherited from Object.)

FromFile

Overloaded. Creates an Image from the specified file. (Inherited from Image.)

FromHbitmap

Overloaded. Creates a Bitmap from a Windows handle. (Inherited from Image.)

FromHicon

Creates a Bitmap from a Windows handle to an icon.

FromResource

Creates a Bitmap from the specified Windows resource.

FromStream

Overloaded. Creates an Image from the specified data stream. (Inherited from Image.)

GetBounds

Gets the bounds of the image in the specified unit. (Inherited from Image.)

GetEncoderParameterList

Returns information about the parameters supported by the specified image encoder. (Inherited from Image.)

GetFrameCount

Returns the number of frames of the specified dimension. (Inherited from Image.)

GetHashCode

Serves as a hash function for a particular type. GetHashCode is suitable for use in hashing algorithms and data structures like a hash table. (Inherited from Object.)

GetHbitmap

Overloaded. Creates a GDI bitmap object from a GDI+ Bitmap.

GetHicon

Returns the handle to an icon.

GetHowFromImage

(Inherited from Image.)

GetLifetimeService

Retrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)

GetPixel

Gets the color of the specified pixel in this Bitmap.

GetPixelFormatSize

Returns the color depth, in number of bits per pixel, of the specified pixel format. (Inherited from Image.)

GetPropertyItem

Gets the specified property item from this Image. (Inherited from Image.)

GetThumbnailImage

Returns a thumbnail for this Image. (Inherited from Image.)

GetType

Gets the Type of the current instance. (Inherited from Object.)

InitializeLifetimeService

Obtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)

IsAlphaPixelFormat

Returns a value that indicates whether the pixel format for this Image contains alpha information. (Inherited from Image.)

IsCanonicalPixelFormat

Returns a value that indicates whether the pixel format is 32 bits per pixel. (Inherited from Image.)

IsExtendedPixelFormat

Returns a value that indicates whether the pixel format is 64 bits per pixel. (Inherited from Image.)

LockBits

Overloaded. Locks a Bitmap into system memory.

MakeTransparent

Overloaded. Makes the default transparent color transparent for this Bitmap.

ReferenceEquals

Determines whether the specified Object instances are the same instance. (Inherited from Object.)

RemovePropertyItem

Removes the specified property item from this Image. (Inherited from Image.)

RotateFlip

This method rotates, flips, or rotates and flips the Image. (Inherited from Image.)

Save

Overloaded. Saves this image to the specified stream in the specified format. (Inherited from Image.)

SaveAdd

Overloaded. Adds the information in the specified Image to this Image. The specified EncoderParameters determine how the new information is incorporated into the existing image. (Inherited from Image.)

SelectActiveFrame

Selects the frame specified by the dimension and index. (Inherited from Image.)

SetPixel

Sets the color of the specified pixel in this Bitmap.

SetPropertyItem

Stores a property item (piece of metadata) in this Image. (Inherited from Image.)

SetResolution

Sets the resolution for this Bitmap.

ToString

Returns a String that represents the current Object. (Inherited from Object.)

UnlockBits

Unlocks this Bitmap from system memory.

 

Table 6

 

Protected Methods

Symbol

Protected method

Name

Description

Finalize

Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)

MemberwiseClone

Overloaded. (Inherited from MarshalByRefObject.)

 

Table 7

 

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.

 

27.     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 = gcnew Bitmap(L"ramp1.gif");

gr->DrawImage(bmp, 10,10);

 

Adding codes to display the bitmap in the Paint event handler

 

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 used forms a vertical stripe down the left side of the form. You’ll find the ramp1.gif file in the CppDraw project directory. Copy it into your CppDraw\CppDraw project directory to use it in this code.

 

28.     Build and run the application. You’ll see a bitmap displayed on the form as shown below.

 

Bitmap displayed on the form

 

Printing

 

The System::Drawing::Printing namespace contains the classes that implement the printing functionality within GDI+. Printing isn’t particularly hard, but it can be a rather involved process because of the number of classes involved. The first class you’ll meet when printing is PrintDocument. A PrintDocument object represents your link with a printer and can be used for more than one print job. PrintDocument has four main properties:

  1. DefaultPageSettings, which gets or sets a PageSettings object that represents the default page settings.

  2. DocumentName, which represents the document name.

  3. PrintController, which gets or sets a PrintController object that controls the printing process

  4. PrinterSettings, which gets or sets a PrinterSettings object that controls where and how the document is printed.

The PrinterSettings and PageSettings classes hold data about the printer to be used such as whether different paper trays are available and PageSettings also holds data about the setup of the document, such as page orientation and number of copies.

 

System.Drawing.Printing Namespace

 

The System.Drawing.Printing namespace provides print-related services. Typically, you create a new instance of the PrintDocument class, set the properties that describe what to print, and call the Print method to actually print the document. Use the Graphics property to specify the output to print. If you are printing a text file, use StreamReader in the following manner: read one line at a time from the stream and call the DrawString method to draw the line in the graphics object. The DrawText methods of the TextRenderer class are not supported for printing. Instead, use the DrawString methods of the Graphics class. When implemented in a derived class, the PrintController controls how a PrintDocument is printed. The PrintDocument.Print method invokes the print controller's OnStartPrint, OnEndPrint, OnStartPage, and OnEndPage methods, which in turn tell the printer how to print the document. The print-preview process uses a specialized print controller, dialog box, and control. Classes within the System.Drawing.Printing namespace are not supported for use within a Windows service or ASP.NET application or service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions.

 

 

 

 

Classes

Symbol

Public class

Class

Description

InvalidPrinterException

Represents the exception that is thrown when you try to access a printer using printer settings that are not valid.

Margins

Specifies the dimensions of the margins of a printed page.

MarginsConverter

Provides a MarginsConverter for Margins.

PageSettings

Specifies settings that apply to a single, printed page.

PaperSize

Specifies the size of a piece of paper.

PaperSource

Specifies the paper tray from which the printer gets paper.

PreviewPageInfo

Specifies print preview information for a single page. This class cannot be inherited.

PreviewPrintController

Specifies a print controller that displays a document on a screen as a series of images.

PrintController

Controls how a document is printed.

PrintDocument

Defines a reusable object that sends output to a printer.

PrinterResolution

Represents the resolution supported by a printer.

PrinterSettings

Specifies information about how a document is printed, including the printer that prints it.

PrinterSettings.PaperSizeCollection

Contains a collection of PaperSize objects.

PrinterSettings.PaperSourceCollection

Contains a collection of PaperSource objects.

PrinterSettings.PrinterResolutionCollection

Contains a collection of PrinterResolution objects.

PrinterSettings.StringCollection

Contains a collection of String objects.

PrinterUnitConvert

Specifies a series of conversion methods that are useful when interoperating with the Win32 printing API. This class cannot be inherited.

PrintEventArgs

Provides data for the BeginPrint and EndPrint events.

PrintingPermission

Controls access to printers. This class cannot be inherited.

PrintingPermissionAttribute

Allows declarative printing permission checks.

PrintPageEventArgs

Provides data for the PrintPage event.

QueryPageSettingsEventArgs

Provides data for the QueryPageSettings event.

StandardPrintController

Specifies a print controller that sends information to a printer.

 

Table 8

 

 

Delegates

Symbol

Public delegate

Delegate

Description

PrintEventHandler

Represents the method that will handle the BeginPrint or EndPrint event of a PrintDocument.

PrintPageEventHandler

Represents the method that will handle the PrintPage event of a PrintDocument.

QueryPageSettingsEventHandler

Represents the method that handles the QueryPageSettings event of a PrintDocument.

 

Table 9

 

 

Enumerations

Symbol

Public enumeration

Enumeration

Description

Duplex

Specifies the printer's duplex setting.

PaperKind

Specifies the standard paper sizes.

PaperSourceKind

Standard paper sources.

PrintAction

Specifies the type of print operation occurring.

PrinterResolutionKind

Specifies a printer resolution.

PrinterUnit

Specifies several of the units of measure used for printing.

PrintingPermissionLevel

Specifies the type of printing that code is allowed to do.

PrintRange

Specifies the part of the document to print.

 

Table 10

 

 

Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6

 

 


< C++ .NET Drawing & Printing 4 | Main | C++ .NET Drawing & Printing 6 >