开发者

Creating a bitmap file line by line

Is it possible to write a bitmap in C# line by line? I am worried that using the Bitmap.Save() function will require too much memory when dealing with LARGE files..

To clarify, I want to do this:

  1. Open the file and write the bitmap header (if any)开发者_开发问答
  2. Put 1 scanline worth of image data into a buffer (byte[])
  3. Write the contents of the buffer into the file
  4. Repeat from 2 unless there are no more scan lines
  5. Close the file

How is this done in C#?

Thanks,

kreb


@Mikael

It looks like this:

int bufferSize = ((width +31) >> 5) << 2); // the width is in pixels, 1bpp,
// so I had to round up to the nearest 32 bits

byte[] buffer = new byte[bufferSize];
byte[] buffer = new byte[height * bufferSize];

for (int i = 0; i < height; i++)
{
    getLine(buffer,bufferSize);
    Buffer.BlockCopy(bufffer,0,bigBuffer, i*bufferSize, bufferSize);
}

unsafe
{
    fixed (byte* ptr = bigBuffer)
    {
        using (Bitmap image = new Bitmap(width,height,bufferSize,System.Drawing.Imaging.PixelFormat.Format1bbIndexed, new IntPtr(ptr)))
        {
            image.SetResolution(xres,yres);
            image.Save(filename,System.Drawing.Imaging.ImageFormat.Tiff);
        }
    }
}

I am worried that for really big images, bigBuffer might become too big.. I would like to change this to write to file line by line, instead of writing it in one go..

Thanks.. :)


I was once writing some code that saved a 32kx32k pixels JPEG file. I had tons of problems with creating it, loading it, viewing it and manipulating it (Photoshop can handle files up to 30kx30k). After trying allot I dropped the whole deal and decided on saving parts of the big image to small images and write some manager program that knows how to "stitch" these files when needed.

I'm not sure when you are in to but if it is for display then you might check out DeepZoom

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜