working with large images

Started by
1 comment, last by Ohforf sake 13 years, 6 months ago
hi friends

I'm working with some large images, I want to tile them into small parts, so I use following lines in order to break my large image (a JPEG or BMP which may be up to 30000 x 30000 pixels in size), when my image is somehow smaller (almost smaller than 10000 x 10000) there is no problem, but when I have a bigger image I get an error in the first line

can you suggest my anything better? is there a good and secure way for tiling very large images using C#?



Bitmap bmpImage = new Bitmap(fname);// I get error here
int tile_width = bmpImage.Width / tile_x;
int tile_height = bmpImage.Height / tile_y;

//loop through all tiles
int counter = 0;
for (int y = 0; y < tile_y; y++)
{
for (int x = 0; x < tile_x; x++)
{

if (!File.Exists(_filename + (counter).ToString() + ".JPG"))//create only if file doesn't exist
{
Rectangle cropArea = new Rectangle(x * tile_width, y * tile_height, tile_width, tile_height);

Image img_cropped;
Bitmap bmpCrop = bmpImage.Clone(cropArea,
bmpImage.PixelFormat);
img_cropped = (Image)(bmpCrop);

EncoderParameter qualityParam =
new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L);

// Jpeg image codec
ImageCodecInfo jpegCodec = getEncoderInfo("image/jpeg");

if (jpegCodec == null)
return;

EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;

//img_cropped.Save(_filename + (counter).ToString() + ".BMP", ImageFormat.Bmp);
img_cropped.Save(_filename + (counter).ToString() + ".JPG", jpegCodec, encoderParams);
counter++;
MDIParent1.layerstatus = "Processing Layer...";
}
}
}


thanks
Visit galaxyroad.com which soon will have english contents too
Advertisement
What is your Bitmap class, does it load the image when you call the constructor?

A 30000x30000 bmp is going to be something like 3-4GB, you probably just don't have enough memory to load the whole file at once.

Bitmap is a fairly easy filetype to parse so you could load the image in chunks yourself and then tile it out piece by piece, but jpg is quite a bit more difficult and I don't think any existing loader would be able to do a partial load like that (or maybe, I don't know).

Alternatively, you could just get a 64bit OS and 8-16GB of ram to be able to load it all at once.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
I agree that you should roll your own file loader.
If you want to avoid that, you might want to look into the exr format. It's an image format that is actually for hdr images, however it supports tile based storing, so whatever C# libs exist for it might also support tile-based loading.

This topic is closed to new replies.

Advertisement