Aracılığıyla paylaş


Nasıl yapılır: Görüntüleri Kırpma ve Ölçeklendirme

sınıfı Graphics , bazıları resimleri kırpmak ve ölçeklendirmek için kullanabileceğiniz kaynak ve hedef dikdörtgen parametrelerine sahip olan çeşitli DrawImage yöntemler sağlar.

Örnek

Aşağıdaki örnek, Apple.gif disk dosyasından bir Image nesne oluşturur. Kod, apple görüntüsünün tamamını özgün boyutunda çizer. Kod daha sonra bir nesnenin yöntemini çağırarak DrawImage apple görüntüsünün Graphics bir kısmını özgün apple görüntüsünden daha büyük bir hedef dikdörtgene çizer.

DrawImage yöntemi, üçüncü, dördüncü, beşinci ve altıncı bağımsız değişkenlerle belirtilen kaynak dikdörtgene bakarak elmanın hangi kısmının çizileceğini belirler. Bu durumda, elma genişliğinin yüzde 75'ine ve yüksekliğinin yüzde 75'ine kırpılır.

yöntemi, DrawImage kırpılan elmayı nereye çizeceğini ve ikinci bağımsız değişken tarafından belirtilen hedef dikdörtgene bakarak kırpılan elmayı ne kadar büyük hale getireceğini belirler. Bu durumda hedef dikdörtgen, özgün görüntüden yüzde 30 daha geniş ve yüzde 30 daha uzundur.

Aşağıdaki çizimde özgün elma ve ölçeklendirilmiş, kırpılmış elma gösterilmektedir.

Özgün bir resmin ve aynı resmin kırpılmış olarak ekran görüntüsü.

Image image = new Bitmap("Apple.gif");

// Draw the image unaltered with its upper-left corner at (0, 0).
e.Graphics.DrawImage(image, 0, 0);

// Make the destination rectangle 30 percent wider and
// 30 percent taller than the original image.
// Put the upper-left corner of the destination
// rectangle at (150, 20).
int width = image.Width;
int height = image.Height;
RectangleF destinationRect = new RectangleF(
    150,
    20,
    1.3f * width,
    1.3f * height);

// Draw a portion of the image. Scale that portion of the image
// so that it fills the destination rectangle.
RectangleF sourceRect = new RectangleF(0, 0, .75f * width, .75f * height);
e.Graphics.DrawImage(
    image,
    destinationRect,
    sourceRect,
    GraphicsUnit.Pixel);
Dim image As New Bitmap("Apple.gif")

' Draw the image unaltered with its upper-left corner at (0, 0).
e.Graphics.DrawImage(image, 0, 0)

' Make the destination rectangle 30 percent wider and
' 30 percent taller than the original image.
' Put the upper-left corner of the destination
' rectangle at (150, 20).
Dim width As Integer = image.Width
Dim height As Integer = image.Height
Dim destinationRect As New RectangleF( _
    150, _
    20, _
    1.3F * width, _
    1.3F * height)

' Draw a portion of the image. Scale that portion of the image
' so that it fills the destination rectangle.
Dim sourceRect As New RectangleF(0, 0, 0.75F * width, 0.75F * height)
e.Graphics.DrawImage( _
    image, _
    destinationRect, _
    sourceRect, _
    GraphicsUnit.Pixel)

Kodu Derleme

Yukarıdaki örnek, Windows Forms ile kullanılmak üzere tasarlanmıştır ve PaintEventArgs olay işleyicisinin bir parametresi olan ePaintgerektirir. değerini sisteminizde geçerli olan bir görüntü dosyası adı ve yoluyla değiştirdiğinden Apple.gif emin olun.

Ayrıca bakınız