Cut and zoom image in .NET.

xiaoxiao2021-03-06  77

From: msdn http://msdn.microsoft.com/library/chs/cpguide/html/_gdiplus_cropping_and_scaling_images_about.asp?frame=true

You can use the Graphics class DrawImage method to draw and locate vector images and grating images. DrawImage is an overload method, so you have several ways to provide parameters. A variant of the DrawImage method receives Bitmap objects and Rectangle objects. This rectangle specifies the target of the drawing operation, that is, it specifies the rectangle that will be drawn within it. If the size of the target rectangle is different from the size of the original image, the original image will be scaled to accommodate the target rectangle. The following example draws the same image three times: no zoom in time, extension once, use compression at a time:

[Visual Basic]

DIM MyBitmap as new bitmap ("spiral.png")

DIM ExpansionRectangle As New Rectangle (135, 10, _

Mybitmap.width, mybitmap.height)

DIM CompressionRectangle As New Rectangle (300, 10, _

Mybitmap.width / 2, mybitmap.height / 2)

MyGraphics.drawImage (Mybitmap, 10, 10)

MyGraphics.drawImage (MyBitmap, ExpansionRectangle)

MyGraphics.drawImage (MyBitmap, CompressionRectangle)

[C #]

Bitmap mybitmap = new bitmap ("spiral.png");

Rectangle ExpanSionRectangle = New Rectangle (135, 10,

Mybitmap.width, mybitmap.height;

Rectangle compressionRectangle = New Rectangle (300, 10,

Mybitmap.width / 2, mybitmap.height / 2);

MyGraphics.drawImage (MyBitmap, 10, 10);

MyGraphics.drawImage (MyBitmap, ExpansionRectangle);

MyGraphics.drawImage (MyBitmap, CompressionRectangle);

The following illustration shows these three pictures.

Some variants of the DrawImage method with source rectangular parameters and target rectangular parameters. The source rectangular parameter specifies the part to be drawn by the original image. The target rectangular parameter specifies the rectangle of the image specified portion to be drawn to it. If the size of the target rectangle is different from the source rectangle, the picture will be scaled to accommodate the target rectangle.

The following example constructs a BitMap object from the file runner.jpg. The entire image is drawn in (0, 0) is not zoomed. A small portion of the image is then drawn twice: use compression once, use extensions at a time.

[Visual Basic]

DIM MyBitmap As New Bitmap ("runner.jpg")

'One Hand of the Runner

DIM SourceRectangle As New Rectangle (80, 70, 80, 45)

'Compressed hand

DIM DESTRECTANGLE1 AS New Rectangle (200, 10, 20, 16)

'Expanded Hand

DIM DESTRECTANGLE2 AS New Rectangle (200, 40, 200, 160) 'DRAW The Original Image At (0, 0).

MyGraphics.drawImage (mybitmap, 0, 0)

'Draw the compressed hand.

MyGraphics.drawImage (_

Mybitmap, DestRectangle1, SourceRectangle, GraphicsUnit.pixel

'Draw the expanded hand.

MyGraphics.drawImage (_

Mybitmap, Destructangle2, SourceRectangle, GraphicsUnit.pixel

[C #]

Bitmap mybitmap = new bitmap ("runner.jpg");

// one hand of the runner

Rectangle SourceRectangle = New Rectangle (80, 70, 80, 45);

// Compressed Hand

Rectangle destRectangle1 = New Rectangle (200, 10, 20, 16);

// Expanded Hand

Rectangle destRectangle2 = New Rectangle (200, 40, 200, 160);

// Draw the Original Image At (0, 0).

MyGraphics.drawImage (mybitmap, 0, 0);

// Draw the compressed hand.

MYGRAPHICS.DRAWIMAGE

Mybitmap, DestRectangle1, SourceRectangle, GraphicsUnit.pixel;

// Draw the expanded hand.

MYGRAPHICS.DRAWIMAGE

Mybitmap, DesTRectangle2, SourceRectangle, GraphicsUnit.pixel;

The following illustration shows unzurked images, as well as compressed and extended image portions. How to automatically add watermark in the picture in the picture? If you know the friend, please tell me :-)

转载请注明原文地址:https://www.9cbs.com/read-93881.html

New Post(0)