Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不复制而裁剪图像_C#_Performance_Crop_Bitmapimage - Fatal编程技术网

C# 不复制而裁剪图像

C# 不复制而裁剪图像,c#,performance,crop,bitmapimage,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, tileWid

我正在编写一个应用程序,它要求我将一个大图像分割成小图像块,每个图像块本质上都是原始图像的裁剪版本

目前,我的拆分操作看起来像这样

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();
直觉上,我认为这将基本上创建一个对原始图像的“引用”,并将显示为图像的一个子矩形。然而,我的分割操作执行的速度很慢,这让我相信这实际上是在复制原始图像的源rect,这对于大图像来说非常慢(分割大小合适的图像时有明显的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;

您是否尝试过创建一个新的
位图
并在其中进行快速切换?我认为这可能比尝试内联裁剪图像要快。您的
平铺
类可以存储对原始图像的引用和绘制自身时要复制的矩形?Graphics.DrawImage有重载来绘制位图的一部分:我承认不是图像处理方面的专家,但我不确定您要求的内容是否合理。虽然可能存在隐藏副本的机制,但我怀疑图像数据需要在内存中以任何方式“重新对齐”(复制)。澄清我的最后一条评论,我只是在考虑内部存储格式——它可能只是一个字节数组。简化了很多,例如,4x4图像在内存中是ABCD EFGH IJKL MNOP。例如,如果希望2x2位于左上角,则需要像ABEF这样的新字节数组。这不是子数组。我已经编辑了你的标题。请参阅“”,其中的共识是“不,他们不应该”。早些时候我评论说,这并没有显著提高性能,但我只是再次尝试了旧方法,事实上,这确实产生了显著的性能提升(从一次拆分接近6秒到更接近1秒)。