How to crop huge image

人走茶凉 提交于 2019-11-28 10:20:41

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).

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

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.

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

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!