locked
Why do I get a blue mark instead of red? Color question

    Question

  •                Bitmap symbolBmp;
                      using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(defaultData)))
                      {
                          symbolBmp = new Bitmap(ms);
                      }
                      Bitmap newsymbolBmp = new Bitmap(symbolBmp.Width, symbolBmp.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                      System.Drawing.Imaging.BitmapData symbolBmp_data = symbolBmp.LockBits(new Rectangle(0, 0, symbolBmp.Width, symbolBmp.Height),
                      System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                      System.Drawing.Imaging.BitmapData newsymbolBmp_data = newsymbolBmp.LockBits(new Rectangle(0, 0, symbolBmp.Width, symbolBmp.Height),
                      System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                      IntPtr p0 = symbolBmp_data.Scan0;
                      IntPtr p1 = newsymbolBmp_data.Scan0;
                      for (int ii = 0; ii < symbolBmp_data.Height * symbolBmp_data.Height ; ii++)
                      {
                              byte b = System.Runtime.InteropServices.Marshal.ReadByte(p0,  4 * ii + 3);
                              if (b == 0) continue;
                              /*System.Runtime.InteropServices.Marshal.WriteByte(p1, 4 * ii, loadedContactsColor.R);
                              System.Runtime.InteropServices.Marshal.WriteByte(p1, 4 * ii + 1, loadedContactsColor.G);
                              System.Runtime.InteropServices.Marshal.WriteByte(p1, 4 * ii + 2, loadedContactsColor.B);
                              System.Runtime.InteropServices.Marshal.WriteByte(p1, 4 * ii + 3, 255);*/
                              System.Runtime.InteropServices.Marshal.WriteByte(p1, 4 * ii, 255);
                              System.Runtime.InteropServices.Marshal.WriteByte(p1, 4 * ii + 1, 0);
                              System.Runtime.InteropServices.Marshal.WriteByte(p1, 4 * ii + 2, 0);
                              System.Runtime.InteropServices.Marshal.WriteByte(p1, 4 * ii + 3, 255);
                          
                      }
                      symbolBmp.UnlockBits(symbolBmp_data);
                      newsymbolBmp.UnlockBits(newsymbolBmp_data);
                      //now make newsymbolbmp back to base64string
                      // Convert the image to byte[] 
                      System.IO.MemoryStream stream = new System.IO.MemoryStream();
                      newsymbolBmp.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                      byte[] imageBytes = stream.ToArray();
                      // Convert byte[] to Base64 String 
                      newColorData = Convert.ToBase64String(imageBytes); 

    It's a transparent png so I test the Alpha and if it is 0, I continue, but if it is 255,  I fill in my own color -- intended to be red, into the Bitmap. I'm getting a blue symbol appearing on my glControl, not Red!

    Thanks for reading and the help!

    sb

    Wednesday, October 03, 2012 9:20 PM

Answers

  • Just a wild guess, but perhaps you're looking at the bytes in the wrong order.

                 System.Runtime.InteropServices.Marshal.WriteByte(p1, 4 * ii, 0); // Blue
                 System.Runtime.InteropServices.Marshal.WriteByte(p1, 4 * ii + 1, 0); // Green
                 System.Runtime.InteropServices.Marshal.WriteByte(p1, 4 * ii + 2, 255); // Red
                 System.Runtime.InteropServices.Marshal.WriteByte(p1, 4 * ii + 3, 255);  // Alpha

    Thursday, October 04, 2012 4:49 AM

All replies

  • Just a wild guess, but perhaps you're looking at the bytes in the wrong order.

                 System.Runtime.InteropServices.Marshal.WriteByte(p1, 4 * ii, 0); // Blue
                 System.Runtime.InteropServices.Marshal.WriteByte(p1, 4 * ii + 1, 0); // Green
                 System.Runtime.InteropServices.Marshal.WriteByte(p1, 4 * ii + 2, 255); // Red
                 System.Runtime.InteropServices.Marshal.WriteByte(p1, 4 * ii + 3, 255);  // Alpha

    Thursday, October 04, 2012 4:49 AM
  • Try this as well:

    Marshal.WriteInt32( p1, 4 * ii, Color.Red.ToArgb()).


    • Edited by Viorel_MVP Thursday, October 04, 2012 6:31 AM
    Thursday, October 04, 2012 6:29 AM