How to crop huge image

前端 未结 5 2011
清酒与你
清酒与你 2020-12-10 07:39

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

5条回答
  •  醉梦人生
    2020-12-10 08:13

    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.

提交回复
热议问题