Operations on Bitmaps: Copying a Picture
Copying a picture is the process of getting each pixel of a picture from one document, the source, and reproducing it on another document, the target:
1 min read62 words1 code sample
Description#
To copy a picture, you must retrieve every pixel from the initial source document and recreate it exactly on a new target document:
This task is straightforward: select the (x, y) coordinates for each pixel in the source and assign those values to the same coordinates in the destination. The following code demonstrates this:
C#
private void btnCopy_Click(object sender, EventArgs e)
{
Graphics graph = pbxSource.CreateGraphics();
Bitmap bmpSource = (Bitmap)pbxSource.Image;
Bitmap bmpDestination =
new Bitmap(pbxDestination.ClientSize.Width,
pbxDestination.ClientSize.Height);
int width = bmpSource.Width;
int height = bmpSource.Height;
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Color clr = bmpSource.GetPixel(x, y);
bmpDestination.SetPixel(x, y, clr);
}
}
pbxDestination.Image = bmpDestination;
}Home | Copyright © 2010-2011 C# Key

