Bitmap format 8bpp on Compact Framework

I've been messing around with Game of Life - http://en.wikipedia.org/wiki/Conway 's_Game_of_Life

I started coding algorithms for winforms and now I want to port my work to Windows Mobile 6.1 (compact framework). I came across an article by John Skeet where I compared several different algorithms for calculating the next generations in a game. He used a byte array to store the state of the cells (live or dead) and then he copied that array into an 8bpp bitmap. For each new generation, it processes the state of each byte, then copies the array to a bitmap, then draws that bitmap into the image box.

        void CreateInitialImage()
    {
        bitmap = new Bitmap(Width, Height, PixelFormat.Format8bppIndexed);
        ColorPalette palette = bitmap.Palette;
        palette.Entries[0] = Color.Black;
        palette.Entries[1] = Color.White;
        bitmap.Palette = palette;
    }

    public Image Render()
    {
        Rectangle rect = new Rectangle(0, 0, Width, Height);
        BitmapData bmpData = bitmap.LockBits(rect, ImageLockMode.ReadWrite, bitmap.PixelFormat);
        Marshal.Copy(Data, 0, bmpData.Scan0, Data.Length);
        bitmap.UnlockBits(bmpData);
        return bitmap;
    }

      

His code above is nicely simple and very fast to render. John is using Windows Forms, but now I want to port my own version to Windows Mobile 6.1 (Compact Framework), but.,. There is no way to format the bitmap to 8bpp in cf.

Can anyone suggest a way to render a byte array for a drawn image in CF. This array is created on the fly in code (it is NOT loaded from an image file on disk). I basically need to store an array of cells represented by bytes, they are either live or dead, and then I need to draw that array as an image. The game is especially slow on CF, so I need to implement smart optimized algorithms, but also need to be done as quickly as possible and the above solution would be pretty tight if available on a compact basis.

Thanks a lot for any help

Any suggestions?

+2


source to share


3 answers


You can look at GDI + for CF. It is basically a wrapper around most of the GDIs implemented in WinCE. Here's a link to the source code and entry: http://community.opennetcf.com/articles/cf/archive/2007/10/31/using-gdi-on-windows-mobile.aspx



I think ImagingFactoryClass.CreateBitmapFromBuffer () looks like a good place to start.

0


source


Ok, how about this:

  • use Bitmap.Save () method to save to MemoryStream instead of file;
  • when you save to a MemoryStream you can name the ImageFormat as "GIF" (this is equivalent to 8bpp in .Net, according to this: http://support.microsoft.com/kb/318343 )
  • use MemoryStream.Write () to change whatever data you want in the image, or copy the data using MemoryStream.ToArray () if that's better.


After modifying the MemoryStream, you may have to copy it back to the Bitmap or create a new Bitmap. If you are making a new bitmap, make sure you have () the old one to avoid memory leaks.

0


source


Hi Rojo and thanks again for the help, I tried the following

        Image bmp = new Bitmap(10, 10);
        byte[] array = ImageToByteArray(bmp);


        public byte[] ImageToByteArray(Image img)
    {
        MemoryStream ms = new MemoryStream();
        img.Save(ms, System.Drawing.Imaging.ImageFormat.Gif );
        return ms.ToArray();
    }

      

The returned array has more than 870 bytes in it. It seems to contain all sorts of header, padding, and whatever information you have. so again it won't work ...

0


source







All Articles