' Draws the bitmaps on the form.
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Dim arialFont As Font
Dim blackBrush As Brush
arialFont = New Font("Arial", 10, FontStyle.Regular)
blackBrush = New SolidBrush(Color.Black)
' Set the size of the sides of the bitmap,
' and get one-third of it for the center bitmap.
Dim sidesize As Integer = 75
Dim third As Integer = CInt(sidesize / 3)
' Create bitmap.
Dim source As Bitmap = CreateBitmap(sidesize)
' Copy entirely as a clone.
Dim clone As Bitmap = CopyBitmap(source)
' Copy the center part of the bitmap.
Dim center As Bitmap = _
CopyBitmap(source, New Rectangle(third, third, third, third))
' Save the bitmap to a file.
clone.Save("newbitmap.bmp", ImageFormat.Bmp)
' Draw the source, clone, and partial
' bitmaps vertically down the screen.
Dim y As Integer = 10
e.Graphics.DrawString("source bitmap:", ArialFont, BlackBrush, 10, y)
y += 20
e.Graphics.DrawImage(source, 10, y)
y += source.Height + 10
e.Graphics.DrawString("clone bitmap:", ArialFont, BlackBrush, 10, y)
y += 20
e.Graphics.DrawImage(clone, 10, y)
y += clone.Height + 10
e.Graphics.DrawString("center part of bitmap:", ArialFont, BlackBrush, 10, y)
y += 20
e.Graphics.DrawImage(center, 10, y)
y += center.Height + 10
' Dispose graphic objects.
arialFont.Dispose()
blackBrush.Dispose()
End Sub
// Draws the bitmaps on the form.
protected override void OnPaint(PaintEventArgs e)
{
Font arialFont;
Brush blackBrush;
arialFont = new Font("Arial", 10, FontStyle.Regular);
blackBrush = new SolidBrush(Color.Black);
// Set the size of the sides of the bitmap,
// and get one-third of it for the center bitmap.
int sidesize = 75;
int third = (int) sidesize/3;
// Create bitmap.
source = CreateBitmap(sidesize);
// Copy entirely as a clone.
clone = CopyBitmap(source);
// Copy the center part of the bitmap.
center = CopyBitmap(source, new Rectangle(third, third, third, third));
// Save the bitmap to a file.
clone.Save("newbitmap.bmp", ImageFormat.Bmp);
// Draw the source, clone, and partial
// bitmaps vertically down the screen.
int y = 10;
e.Graphics.DrawString("source bitmap:", arialFont, blackBrush, 10, y);
y += 20;
e.Graphics.DrawImage(source, 10, y);
y += source.Height + 10;
e.Graphics.DrawString("clone bitmap:", arialFont, blackBrush, 10, y);
y += 20;
e.Graphics.DrawImage(clone, 10, y);
y += clone.Height + 10;
e.Graphics.DrawString("center part of bitmap:", arialFont, blackBrush, 10, y);
y += 20;
e.Graphics.DrawImage(center, 10, y);
y += center.Height + 10;
// Dispose graphic objects.
arialFont.Dispose();
blackBrush.Dispose();
}