pho*_*oer 5 c# performance crop bitmapimage
我正在编写一个应用程序,要求我将大图像拆分成小图块,其中每个图块基本上是原始图像的裁剪版本.
目前我的拆分操作看起来像这样
tile.Image = new BitmapImage();
tile.Image.BeginInit();
tile.Image.UriSource = OriginalImage.UriSource;
tile.Image.SourceRect = new Int32Rect(x * tileWidth + x, y * tileHeight, tileWidth, tileHeight);
tile.Image.EndInit();
Run Code Online (Sandbox Code Playgroud)
直觉上,我认为这将基本上创建原始图像的"参考",并且只显示为图像的子矩形.然而,我的分割操作执行速度慢导致我认为这实际上是复制原始图像的源矩形,对于大图像来说这是非常慢的(当分割一个体面时有一个明显的3-4秒暂停大小图片).
我环顾四周但却找不到一种方法来绘制位图作为大图像的子矩形,而无需复制任何数据.有什么建议?
使用 System.Windows.Media.Imaging.CroppedBitmap 类:
// Create a CroppedBitmap from the original image.
Int32Rect rect = new Int32Rect(x * tileWidth + x, y * tileHeight, tileWidth, tileHeight);
CroppedBitmap croppedBitmap = new CroppedBitmap(originalImage, rect);
// Create an Image element.
Image tileImage = new Image();
tileImage.Width = tileWidth;
tileImage.Height = tileHeight;
tileImage.Source = croppedBitmap;
Run Code Online (Sandbox Code Playgroud)