我想开发一个具有以下签名的函数:
CopyImage(ImageSource inputImage, Point inTopLeft, Point InBottomRight, ImageSource outputImage, Point outTopLeft);
该函数复制输入图像的一部分(由inTopLeft和inBottomRight定义的ROI)并将其复制到outTopLeft处的outputImage。
我可以在 WPF 中通过操作像素来完成此操作,但我正在寻找一种可以更快完成此操作的解决方案。
在 WPF 中执行此操作最快的方法是什么?
您的方法可能如下所示:
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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)