Sign in to follow this  
vcGamer

working with large images

Recommended Posts

vcGamer    100
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

Share this post


Link to post
Share on other sites
karwosts    840
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.

Share this post


Link to post
Share on other sites
Ohforf sake    2052
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this