views:

407

answers:

3

Hi friends.

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

I started out coding algorithmns for winforms and now want to port my work onto windows mobile 6.1 (compact framework). I came across an article by Jon Skeet where he compared several different algorithmns for calculating next generations in the game. He used an array of bytes to store a cells state (alive or dead) and then he would copy this array to an 8bpp bitmap. For each new generation, he works out the state of each byte, then copies the array to a bitmap, then draws that bitmap to a picturebox.

        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 beautifully simple and very fast to render. Jon is using Windows Forms but now I want to port my own version of this onto Windows Mobile 6.1 (Compact Framework) but . . . .there is no way to format a bitmap to 8bpp in the cf.

Can anyone suggest a way of rendering an array of bytes to a drawable image in the CF. This array is created in code on the fly (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 alive or dead and I then need to draw that array as an image. The game is particularly slow on the CF so I need to implement clever optimised algoritmns but also need to render as fast as possible and the above solution would be pretty dam perfect if only it was available on the compact framework.

Many thanks for any help

Any suggestions?

A: 

You could have a look at GDI+ for CF. It's basically a wrapper for most of the GDI implemented in WinCE. Here's a link to the source code and a writeup: 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.

Rocjoe
I have downloaded but I cant seem to find the "ImageFactory" class you refer to
ahh, got it now, had to download the OpenNetCF smart device framework. I think this is a dead end because they use the same System.Drawing.Imaging.PixelFormat enum so I still cannot get at an 8bpp image via their framework.
A: 

Ok, how about this:

  • use the Bitmap.Save() method to save to a MemoryStream instead of a file;
  • when you save to the MemoryStream, you get to 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 jives better.

After you change the MemoryStream, you'll probably have to copy it back into the Bitmap, or make a new Bitmap. If you do make a new Bitmap, be sure to Dispose() the old one, to avoid memory leaks.

Rocjoe
A: 

Hi Rocjoe and thanks again for the help, I have 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 array coming back has over 870 bytes in it, It seems to hold all sorts of header info, padding and what have you. so again it does not work...