本文介绍了从一个图像复制的投资回报率,并复制到另一个WPF图像上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要开发具有以下签名的函数:
I want to develop a function with the following signature:
CopyImage(ImageSource inputImage, Point inTopLeft, Point InBottomRight, ImageSource outputImage, Point outTopLeft);
这输入图像(ROI由inTopLeft和inBottomRight定义)和功能拷贝一部分它复制到outputImage在outTopLeft。
This function copy part of input image (ROI defined by inTopLeft and inBottomRight) and copy it to outputImage at outTopLeft.
我可以通过操纵像素为此在WPF中,但我要寻找一个解决方案,可以做到这一点要快得多。
I can do this in WPF by manipulating pixels, but I am looking for a solution that can do it much faster.
什么是WPF做到这一点的最快方法?
What is the fastest way to do this in WPF?
推荐答案
您的方法可能是这样的:
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;
}
这篇关于从一个图像复制的投资回报率,并复制到另一个WPF图像上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!