Graphics Drawlmage Methods selection

void DrawImage(Image image, int x, int y, int cx, int cy) void DrawImage(Image image, float x, float y, float cx, float cy) void DrawImage(Image image, Rectangle rect) void DrawImage(Image image, RectangleF rectf)

These methods scale the image to the size of the rectangle, either stretching or compressing it to fit. One common use of these methods is to display an image in its pixel size rather than its metrical size. If page units are pixels, simply call grfx.DrawImage(image, x, y, image.Width, image.Height);

The following program displays an image in its pixel dimensions centered within the client area (or printer page).

CenterPixelSizeImage.es

//---------------------------------------------------

// CenterPixelSizeImage.cs ® 2001 by Charles Petzold //---------------------------------------------------

using System;

using System.Drawing;

using System.Windows.Forms;

class CenterPixelSizelmage: PrintableForm {

Image image;

public new static void Main() {

Application.Run(new CenterPixelSizeImage());

public CenterPixelSizeImage() {

Text = "Center Pixel-Size Image";

image = Image.FromFile("..\\..\\..\\Apollo11FullColor.jpg");

protected override void DoPage(Graphics grfx, Color clr, int cx, int cy)

(cy - image.Height) / 2, image.Width, image.Height);

Because your video resolution is most likely greater than 72 dpi, this image is smaller than the one drawn by DrawImage:

On the printer, which has a default page transform that makes it appear to be a 100-dpi device, this version of the DrawImage method will render the 220 i 240 pixel bitmap as 2.2 i 2.4 inches. If you set page units to pixels in the DoPage method, the printed image will be much smaller, probably resulting in the postage stamp effect commonly encountered in less sophisticated graphics programming environments.

The following program loads an image and scales it to the entire size of the client area (or printable area of the printer page).

ImageScaleToRectangle.cs

//----------------------------------------------------

// ImageScaleToRectangle.cs ® 2001 by Charles Petzold //----------------------------------------------------

using System;

using System.Drawing;

using System.Windows.Forms;

class ImageScaleToRectangle: PrintableForm {

Image image;

public new static void Main()

Application.Run(new ImageScaleToRectangleO);

public ImageScaleToRectangleO {

Text = "Image Scale To Rectangle";

image = Image.FromFile("..\\..\\..\\ApollollFullColor.jpg");

protected override void DoPage(Graphics grfx, Color clr, int cx, int cy)

As you make the client area much wider than it is tall, or much taller than it is wide, the image is distorted accordingly:

If you really do need to scale an image to the size of a rectangle, this effect is probably not what you had in mind. You probably want to scale the image isotropically, which means equally in both directions. Here's a program that scales a rectangle more intelligently.

ImageScalelsotropic.cs

//--------------------------------------------------

// ImageScaleIsotropic.cs ® 2001 by Charles Petzold //--------------------------------------------------

using System;

using System.Drawing;

using System.Windows.Forms;

class ImageScaleIsotropic: PrintableForm {

Image image;

public new static void Main() {

Application.Run(new ImageScaleIsotropicO);

public ImageScaleIsotropicO {

Text = "Image Scale Isotropic";

image = Image.FromFile("..\\..\\..\\Apollo11FullColor.jpg");

protected override void DoPage(Graphics grfx, Color clr, int cx, int cy)

ScaleImageIsotropically(grfx, image, new Rectangle(0, 0, cx, cy));

void ScaleImageIsotropically(Graphics grfx, Image image, Rectangle rect)

SizeF sizef = new SizeF(image.Width / image.HorizontalResolution, image.Height /

image.VerticalResolution);

float fScale = Math.Min(rect.Width / sizef.Width, rect.Height / sizef.Height);

sizef.Width *= fScale; sizef.Height *= fScale;

grfx.DrawImage(image, rect.X + (rect.Width - sizef.Width ) / 2, rect.Y + (rect.Height - sizef.Height) / 2, sizef.Width, sizef.Height);

The ScaleImageIsotropically method will work in all cases except when the horizontal and vertical resolutions of the device are different (as is the case with some printers) and when the PageUnit is GraphicsUnit.Pixel (which is not the default case for printers).

The method begins by calculating a SizeF structure that indicates the size of the Image object in inches. (This step wouldn't be necessary if the horizontal and vertical resolution of the image were the same.) Then a factor is calculated that is the minimum of the destination rectangle width and height divided by the image width and height. This fScale number is the factor that must be applied to the image size to isotropically scale it to the size of the destination rectangle. The method then calculates the origin of this rectangle and passes all the information to DrawImage. Here's the image:

-fcllKl

■ ^Iff -

The rectangle-destination versions of the DrawImage method can do additional tricks beyond just stretching an image. If you specify a negative width, the image is flipped around the vertical axis—it's a mirror image. A negative height flips the image around the horizontal axis and shows it upside down. In all cases, the upper left corner of the original unflipped image is always positioned at the Point or PointF portion of the rectangle you specify in the drawing method.

Let's look at a program that draws four images, some with negative widths and heights. In all four cases, the second and third arguments to DrawImage indicate the center of the client area.

ImageRefleetion.es

// ImageRefleetion.es ® 2001 by Charles Petzold //----------------------------------------------

using System;

using System.Drawing;

using System.Windows.Forms;

elass ImageRefleetion: PrintableForm {

Image image;

public new static void Main()

Application.Run(new ImageReflectionO

public ImageReflectionO {

Text = "Image Reflection", image = Image.FromFile("..\\..\\..\\Apollo11FullColor.jpg");

proteeted override void DoPage(Graphies grfx, Color elr, int ex, int int cxImage = image.Width; int cyImage = image.Height;

grfx.DrawImage(image, cx / 2, cy / 2, cxImage, cylmage)

grfx.DrawImage(image, cx / 2, cy / 2, -cxImage, cyImage)

grfx.DrawImage(image, cx / 2, cy / 2, cxImage, -cyImage)

grfx.Draw!mage(image, cx / 2, cy / 2, -cxImage, -cyImage)

And here's the result showing the four images:

Notice that the program sizes the image based on its pixel dimension.

0 0

Post a comment

  • Receive news updates via email from this site