Nasıl yapılır: Görüntü Kırpma ve Ölçeklendirme
GraphicsSınıfı DrawImage , bazılarının resimleri kırpmak ve ölçeklendirmek için kullanabileceğiniz kaynak ve hedef dikdörtgen parametreleri olan çeşitli yöntemler sunar.
Örnek
Aşağıdaki örnek, Image Apple.gif disk dosyasından bir nesne oluşturur. Kod tüm Apple görüntüsünü özgün boyutunda çizer. Kod daha sonra, DrawImageGraphics özgün Apple görüntüsünden daha büyük bir hedef dikdörtgende Apple görüntüsünün bir bölümünü çizmek için bir nesnenin yöntemini çağırır.
DrawImageYöntemi, Apple 'ın, üçüncü, dördüncü, beşinci ve altıncı bağımsız değişkenlerle belirtilen kaynak dikdörtgenine bakarak hangi bölümünün çizileceğini belirler. Bu durumda, Apple genişliğinin yüzde 75 ' i ve yüksekliğinin yüzde 75 ' e kırpılır.
DrawImageYöntemi, kırpılan Apple 'ın nereye çizileceğini ve ikinci bağımsız değişken tarafından belirtilen hedef dikdörtgenine bakarak kırpılan Apple 'ın ne kadar büyük olduğunu belirler. Bu durumda, hedef dikdörtgen orijinal görüntüden yüzde 30 daha geniş ve yüzde 30 ' dur.
Aşağıdaki çizimde orijinal Apple ve ölçeklendirilen kırpılan Apple görüntülenir.

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)
Kod Derleniyor
yukarıdaki örnek, Windows Forms kullanımı için tasarlanmıştır ve PaintEventArgse olay işleyicisinin bir parametresi olan gerektirir Paint . ' Apple.gif İ sisteminizde geçerli olan bir görüntü dosyası adı ve yolu ile değiştirdiğinizden emin olun.