简体   繁体   中英

Exceptions when working with big images

I working on a maze generator in C# which generates big mazes and then saves them to a png file.

Currently I'm running against some errors when I'm trying to save the image.

When I use this:

System.Drawing.Bitmap tempBitmap = new System.Drawing.Bitmap(16000, 16000);

I get an argument exception. (It does work with 8000*8000)

When I use the WPF way of doing it:

RenderTargetBitmap rtb = 
              new RenderTargetBitmap(16000, 16000, 96, 96, PixelFormats.Pbgra32);

I get a memory overflow exception.

Does anyone have another way to save big images in c#?

I've looked at some libraries too without success.

Someone also said that I should first save the image in parts and then combine them, but how can I do this?

Edit:

Edit2: I'm now trying PixelFormat.Format1bppIndexed like this:

private Bitmap CreateBitmapImage2(BitArray[] map, List<Point> path)
{
    Bitmap objBmpImage = new Bitmap(cursize * (map[0].Length - 1), cursize * (map.Length - 1), PixelFormat.Format1bppIndexed);

    Rectangle rect = new Rectangle(0, 0, objBmpImage.Width, objBmpImage.Height);
    System.Drawing.Imaging.BitmapData bmpData =
        objBmpImage.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
        objBmpImage.PixelFormat);

    IntPtr ptr = bmpData.Scan0;

    int bytes = Math.Abs(bmpData.Stride) * objBmpImage.Height;
    byte[] rgbValues = new byte[bytes];

    System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

    int counter = 0;
    int counterdeluxe = 0;
    BitArray bitarray = new BitArray(8);

    Random r = new Random();

    Boolean last = true;
    for (int i = 0; i < 225 / 8; i++)
    {
        rgbValues[i] = (byte)r.Next(255);
    }

    System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);


    objBmpImage.UnlockBits(bmpData);

    return (objBmpImage);
}

Though for some reason it only creates half an image: http://dl.dropbox.com/u/1814002/maze.png

Edit4: Ah it seems that the bits in the width are always rounded up to a 4 byte number. Now it starts to make sense to me :).

Edit5: Ok it doesn't make sense anymore lol, I'll just stay with the graphics for now I guess

Edit6: I couldn't let it go so I looked at it again and found out the following: At the point where I use a BitArray to convert my seperate bits to a byte which I then write to the ImageData something was going wrong, apparently the bitarray had to be written to in reverse. Finally everything works correctly now :).

All problems solved and my maze generator is up and running. Thanks everyone :)

If your mazes are just black/white, you might want to try:

Bitmap bitmap = new Bitmap(16000, 16000, PixelFormat.Format1bppIndexed);

Assuming it keeps the image in that format in memory, that should be considerably more efficient than the default pixel format.

On my box (which has 12GB of memory, admittedly), 16000x16000 works by default, but 32000x32000 doesn't. Even 32000x32000 works with Format1bppIndexed though.

EDIT: As noted in comments, you should use using statements for bitmaps:

using (Bitmap bitmap = ...)
{
}

... so that they can be cleaned up appropriately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM