Question

I would like to generate a two dimensional array of pixels and then save it in .bmp file. I've read about Bitmaps, but I don't fully understand how to convert 2D arrays of Integer to Bitmap image.

That's what I've already found and tried to make

        // Create array of integers
        int width = 1024;
        int height = 768;
        int[] integers = new int[width * height];

        // Fill array with random values
        Random random = new Random();
        for (int i = 0; i < integers.Length; ++i)
        {
            integers[i] = random.Next(Int32.MaxValue);
        }

        // Copy into bitmap
        Bitmap bitmap;
        unsafe
        {
            fixed (int* intPtr = &integers[0])
            {

            bitmap = new Bitmap(width, height, width, PixelFormat.Format32bppRgb, new IntPtr(&integers[0]));
            }
        }

However I still don't understand this part

        // Copy into bitmap
        Bitmap bitmap;
        unsafe
        {
            fixed (int* intPtr = &integers[0])
            {

            bitmap = new Bitmap(width, height, width, PixelFormat.Format32bppRgb, new IntPtr(&integers[0]));
            }
        }
  1. The compiler says The type or namespace name 'Bitmap' could not be found (are you missing a using directive or an assembly reference?)
  2. How can I put it into the bmp file?
Was it helpful?

Solution

Good old compiler ... did you try his suggestion ?

Add this line in the top of your file :

using System.Drawing.Bitmap;

If you want more : check msdn.

By the way, you can right-click on Bitmap and select Resolve to do it. This is the easy way.

To save your image into a file, there is differents ways to do it. It depends of which platform you are. I think you should look in WriteableImageEx or here or here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top