Custom color array into Bitmap

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Oscee
    New Member
    • Feb 2010
    • 3

    Custom color array into Bitmap

    Hi everyone!

    I've read several Bitmap-related threads but haven't find a solution.

    I am working on a raytracer program. I have an array of "custom color" objects - the color class basically contains only the RGBA values and a couple of overloaded operators.

    I would like to display this array on a PictureBox - what would be the easiest way to convert my structure to a Bitmap?

    (I know, this array-thingy doesn't look like to be an optimal solution, the whole thing is based on an old C-OpenGl code of mine, later I would like tho replace everything with generic lists)
  • ThatThatGuy
    Recognized Expert Contributor
    • Jul 2009
    • 453

    #2
    PictureBox.Back Color =Color.FromArgb (100,125,152);


    the backcolor applies to picturebox if there's no image over it

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Ok, assuming that there is some logical way your array is tied to coordinates, it's fairly easy:

      Make sure to include a reference to System.Drawing.

      Create a new Bitmap object, and provide it dimensions:
      Code:
      Bitmap bmp = new Bitmap(100, 100);
      Then, depending on how your array is mapped to the coordinates, loop through it and assign each pixel. This example assumes a single dimensional array, starting from 0,0 (top left), proceeding to fill first down, then to the left.

      Code:
      for (int i = 0; i < 100; i++)
          for (int j = 0; j < 100; j++)
              bmp.SetPixel(i, j, Color.FromArgb(a, r, g, b));
      a, r, g, and b, should all be ints that map to the alpha, red, green, and blue values.

      Once you have constructed your bitmap, assign it as the picturebox's image property:

      Code:
      pictureBox1.Image = bmp;
      That should do it.

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        Just as a little p.s.:

        I only learned how to do this bitmap stuff for a neat little project I heard about that is to design an image containing all possible RGB values, with no two colors repeated. Here's what I came up with:

        Code:
        Bitmap bmp = new Bitmap(4096, 4096);
        int r = 0, g = 0, b = 0;
        bool rRev = false, gRev = false, bRev = false;
        for (int i = 0; i < 4096; i++)
        {
            for (int j = 0; j < 4096; j++)
            {
                bmp.SetPixel(i, j, Color.FromArgb(r, g, b));
                r = rRev ? r - 1 : r + 1;
                if (r >= 255)
                {
                    rRev = true;
                    g = gRev ? g - 1 : g + 1;
                    if (g >= 255)
                    {
                        gRev = true;
                        b = bRev ? b - 1 : b + 1;
                        if (b >= 255)
                            bRev = true;
                        if (b <= 0)
                            bRev = false;
                    }
                    if (g <= 0)
                    {
                        gRev = false;
                        b = bRev ? b - 1 : b + 1;
                        if (b >= 255)
                            bRev = true;
                        if (b <= 0)
                            bRev = false;
                    }
        
                }
                if (r <= 0)
                {
                    rRev = false;
                    g = gRev ? g - 1 : g + 1;
                    if (g >= 255)
                    {
                        gRev = true;
                        b = bRev ? b - 1 : b + 1;
                        if (b >= 255)
                            bRev = true;
                        if (b <= 0)
                            bRev = false;
                    }
                    if (g <= 0)
                    {
                        gRev = false;
                        b = bRev ? b - 1 : b + 1;
                        if (b >= 255)
                            bRev = true;
                        if (b <= 0)
                            bRev = false;
                    }
                }
        
            }
        }
        FileStream fs = new FileStream(@"c:\dev\temp.png", FileMode.Create);
        Console.WriteLine("Writing File");
        bmp.Save(fs, ImageFormat.Png);
        fs.Flush();
        fs.Close();
        Console.WriteLine("Done");

        Comment

        • Oscee
          New Member
          • Feb 2010
          • 3

          #5
          Thank you!
          It is easier than I thought it would be. It seems that I had problems with the raytracing itself than the displaying after all :)

          Comment

          • Curtis Rutland
            Recognized Expert Specialist
            • Apr 2008
            • 3264

            #6
            Glad I could help.

            Comment

            Working...