0

I have successfully generated a BMP file. (I am still tweaking some changes in format I am required to do though). I created a winform with a PictureBox. Following my last question, I now can save the file, but now the bitmap cannot be displayed in the PictureBox. This happened after I changed the palette following this answer to have the alpha of the colors set to 0

My bmp is monochrome as taught in this answer (I wonder whether there could be a simpler way)

My code is

 private void btnNameUsage_Click(object sender, EventArgs e)
  {
   Bitmap bmp = new Bitmap(width, height);
   // Bitmap bmp = new Bitmap(width, height, PixelFormat.Format1bppIndexed); //This does not work

     bmp.SetResolution(300.0F, 300.0F);

    string name = "Hello how are you";
    string date = DateTime.Now.Date.ToString();      
    using (Graphics thegraphics = Graphics.FromImage(bmp))
      {

      string complete = date + "\n" + name ;


      thegraphics.FillRectangle(Brushes.White, 0, 0, bmp.Width, bmp.Height);

      using (Font font1 = new Font("Arial", 24, FontStyle.Regular, GraphicsUnit.Pixel))
      using (var sf = new StringFormat()
          {
            Alignment = StringAlignment.Center,
            LineAlignment = StringAlignment.Center,
          })
          {
          thegraphics.DrawString(complete, font1, Brushes.Black, new Rectangle(0, 0, bmp.Width, bmp.Height), sf);
           }
      }

    //add
    BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format1bppIndexed);

    Bitmap newBitmap = new Bitmap(width, height, bmpData.Stride, PixelFormat.Format1bppIndexed, bmpData.Scan0);
    newBitmap.SetResolution(300.0F, 300.0F);

    //we modify the palette THIS MAKES THE BMP NOT SHOWN IN PIT BOX

     ColorPalette palette = newBitmap.Palette;
     palette.Entries[0] = black;  //black is a type Color
     palette.Entries[1] = white;  //white is a type Color
     newBitmap.Palette = palette;


     picBoxImage.Image = newBitmap;  //THIS fails
     newBitmap.Save(@"theImage.bmp", ImageFormat.Bmp); //This works!

     }

I have to clarify that the generated pallete by default colors are (RGBA) black:000000FF and white: FFFFFFFF (with this palette I can see the bmp in the picbox) but I am changing to black :00000000 and white: FFFFFF00 (as you can see only the A component is changed)

the variables black and white are

Color black = new Color();
Color white = new Color();
white = Color.FromArgb(0, 255, 255, 255); //the 0 is alpha zero
black = Color.FromArgb(0, 0, 0, 0);  //the first 0 is alpha zero

I wonder why it is not showing.

As a side question, how can I change the "number of important colors" in the DIB header?

EDIT: I experimented with the alpha setting (for example 128) and I could see the picture only slightly less bright colors.(as in grey) This only happens when displaying the bmp. The saved file is correctly black and white

What is the relation between picturebox and alpha...

KansaiRobot
  • 7,564
  • 11
  • 71
  • 150

1 Answers1

0

Alpha denotes the opacity of the color. 0 means the color is totally transparent, i.e. you will only see the canvas/background, 128 means the color is 50% transparent meaning you will see the color and background in the same amount, 255 means full opacity/zero transparency meaning you will only see the color.

In short white is "FFFFFFFF" and black is "FF000000".

If you want to specify colors by their RGB components use the FromArgb method with the three parameters which implicitly sets the alpha channel to 255.

ckuri
  • 3,784
  • 2
  • 15
  • 17
  • Yes, I am wondering why when alpha is 0 (white: 00FFFFFF black: 00000000) I can see the BMP when saved to a file but cannot see it when assigned to a PictureBox... – KansaiRobot Jul 05 '18 at 00:39
  • 1
    I think the reason for this is that the color table in bitmap files has no alpha channel. So when you save it as a Windows bitmap file the previous alpha component is disregarded, but as long as the file is still in memory the palette is represented by the Color C# type which has an alpha component. What happens when you load the bitmap file into the PictureBox? – ckuri Jul 05 '18 at 04:52
  • When the alpha is 0, I can only see the pictureBox with the background color – KansaiRobot Jul 05 '18 at 05:12
  • 1
    With loading the bitmap file, I meant `picBoxImage.Image = Image.FromFile("theImage.bmp");`. If its still not visible, means the viewer you use to display the file ignores the alpha channel. – ckuri Jul 05 '18 at 05:46