如何裁剪大图像

发布于 2024-07-24 07:21:19 字数 193 浏览 6 评论 0原文

我需要在 C# 中处理大图像(20,000x20,000 像素)。 由于内存限制,直接打开这些图像并不是可行的方法,但我想做的是将图像分割成更小的块(裁剪)。 我一直在寻找可以解决这个问题的第三方库,但到目前为止还没有结果。 我尝试了 FreeImage 和 ImageMagick,但它们无法打开 20,000x20x000 像素图像。 我怎样才能实现这个目标?

I need to process large images (20,000x20,000pixels) in C#. Opening these images directly isn't the way to go because of memory limitations, but what I want to do is split the image into smaller pieces (cropping). I was looking for a 3rd party library that could the trick, but so far no result. I tried FreeImage and ImageMagick, but they cannot open an 20,000x20x000 pixel image. How can I achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

唔猫 2024-07-31 07:21:19

我当前的工作项目包括一个图像查看器/分析程序,能够加载超过 16 GB 的图像。 您必须使用手动文件 IO,提取标头信息并按需创建子图像(或者如果您正在处理图像,则可以创建单个图块并就地处理它)。 很少有库能够为您提供加载/修改 20k x 20k 图像(1.2gb,24bpp)的能力,并且能够实现这一点的库很少会在性能方面做到这一点(如果这是一个问题的话)。

My current project at work consists of an image viewer/analyzing program capable of loading images in excess of 16 gb. You have to use manual file IO, pull the header information out and create your subimages on-demand (or if you're processing the image, you can create a single tile and process it in-place). Very few libraries are going to give you the capability to load/modify a 20k by 20k image (1.2gb at 24bpp) and the ones that do will rarely do so with anything resembling performance (if that is a concern).

苦行僧 2024-07-31 07:21:19

不知道这是否有帮助,但这是一篇关于 使用 C# lambda 表达式进行图像处理

Don't know if this would help, but here is an article on image processing with C# lambda expressions.

我的影子我的梦 2024-07-31 07:21:19

我不知道有任何现有的图书馆可以做到这一点。

您可能必须破解图像文件流,寻找颜色和像素数据存在的位置,并将像素数据的一部分读入数组,然后从中创建图像。

例如,对于 BMP 文件格式,您需要查找颜色表,加载颜色表,然后查找像素数组部分,将所需数量的像素加载到数组中,然后仅使用这些像素创建一个新的位图。

I don't know of any existing library to do this.

You're probably going to have to crack open the image file stream, seek to location where the color and pixel data exists, and read a section of the pixel data into an array, and create your image from that.

For example, for the BMP file format, you'll want to seek into the color table, load the color table, then seek to the pixel array section, load however many pixes you wish into an array, then make a new bitmap with just those pixels.

一萌ing 2024-07-31 07:21:19

您可以使用内置的.Net 库。

使用

sourceBitmap = (Bitmap)Image.FromStream(sourceFileStream, false, false);

这将阻止图像数据缓存在内存中。 您可以创建一个新的目标位图来将该大图像的一部分绘制到:

var output = new Bitmap(outputWidth, outputHeight);
var outputGraphics = Graphics.FromImage(output);

outputGraphics.DrawImage(sourceBitmap, destRect, srcRect, GraphicsUnit.Pixel);

using (var fs = File.OpenWrite(outputFilePath))
{
    output.Save(fs, System.Drawing.Imaging.ImageFormat.Png);
}

其中 destRect 可以是输出图像的整个大小,或更小的区域。

You can use the built-in .Net libraries.

Use

sourceBitmap = (Bitmap)Image.FromStream(sourceFileStream, false, false);

That will stop the image data from being cached in memory. You can create a new destination bitmap to draw a subsection of that massive image to:

var output = new Bitmap(outputWidth, outputHeight);
var outputGraphics = Graphics.FromImage(output);

outputGraphics.DrawImage(sourceBitmap, destRect, srcRect, GraphicsUnit.Pixel);

using (var fs = File.OpenWrite(outputFilePath))
{
    output.Save(fs, System.Drawing.Imaging.ImageFormat.Png);
}

Where destRect can be the whole size of the outputImage, or a smaller area.

命硬 2024-07-31 07:21:19

我会用 ImageMagick 来完成。 有一个非常可靠的 .NET API 可用,它通常是进行此类图像处理的最佳方法。

查看页面下方的 .NET 部分。

http://www.imagemagick.org/script/api.php

这是信息关于 ImageMagick 中的裁剪内容如何在命令行版本中工作。

http://www.imagemagick.org/Usage/crop/#crop

I'd do it with ImageMagick. There is a pretty solid .NET API available, and it is typically the best way to do image processing like this.

Look under .NET, part of the way down the page.

http://www.imagemagick.org/script/api.php

Here is the info on how the crop stuff in ImageMagick works, for the command line version.

http://www.imagemagick.org/Usage/crop/#crop

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文