I want to develop a function with the following signature:
CopyImage(ImageSource inputImage, Point inTopLeft, Point InBottomRight, ImageSource outputImage, Poin
Your method could look like this:
private BitmapSource CopyRegion(
BitmapSource sourceBitmap, Int32Rect sourceRect,
BitmapSource targetBitmap, int targetX, int targetY)
{
if (sourceBitmap.Format != targetBitmap.Format)
{
throw new ArgumentException(
"Source and target bitmap must have the same PixelFormat.");
}
var bytesPerPixel = (sourceBitmap.Format.BitsPerPixel + 7) / 8;
var stride = bytesPerPixel * sourceRect.Width;
var pixelBuffer = new byte[stride * sourceRect.Height];
sourceBitmap.CopyPixels(sourceRect, pixelBuffer, stride, 0);
var outputBitmap = new WriteableBitmap(targetBitmap);
sourceRect.X = targetX;
sourceRect.Y = targetY;
outputBitmap.WritePixels(sourceRect, pixelBuffer, stride, 0);
return outputBitmap;
}