【问题标题】:Failure to display a monochrome BMP file to a pictureBox when Palette alpha is zero当调色板 alpha 为零时,无法将单色 BMP 文件显示到图片框
【发布时间】:2018-07-04 03:13:06
【问题描述】:

我已成功生成 BMP 文件。 (我仍在调整一些我需要做的格式更改)。我用 PictureBox 创建了一个 winform。在我的最后一个问题之后,我现在可以保存文件,但是现在位图无法显示在 PictureBox 中。这发生在我更改了调色板之后,this answer 将颜色的 alpha 设置为 0

我的bmp是this answer中所教的单色(我想知道是否有更简单的方法)

我的代码是

 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!

     }

我必须澄清生成的调色板默认颜色是(RGBA)黑色:000000FF 和白色:FFFFFFFF(使用此调色板我可以在 picbox 中看到 bmp),但我正在更改为黑色:00000000 和白色:FFFFFF00 (如您所见,只有 A 组件发生了变化)

黑白变量分别是

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

我想知道为什么它没有显示。

作为一个附带问题,如何更改 DIB 标头中的“重要颜色数量”?

编辑: 我尝试了 alpha 设置(例如 128),我可以看到图片的颜色稍微不那么亮。(如灰色) 这仅在显示 bmp 时发生。保存的文件是正确的黑白

picturebox和alpha有什么关系...

【问题讨论】:

    标签: c# bitmap image-manipulation bmp


    【解决方案1】:

    Alpha 表示颜色的不透明度。 0 表示颜色完全透明,即您只会看到画布/背景,128 表示颜色为 50% 透明,表示您将看到相同数量的颜色和背景,255 表示完全不透明度/零透明度表示您只会看到看颜色。

    总之白色是“FFFFFFFF”,黑色是“FF000000”。

    如果您想通过其 RGB 分量指定颜色,请使用 FromArgb 方法和三个参数,该方法将 alpha 通道隐式设置为 255。

    【讨论】:

    • 是的,我想知道为什么当 alpha 为 0 时(白色:00FFFFFF 黑色:00000000)我可以在保存到文件时看到 BMP,但在分配给 PictureBox 时却看不到它...跨度>
    • 我认为原因是位图文件中的颜色表没有alpha通道。因此,当您将其保存为 Windows 位图文件时,之前的 alpha 组件将被忽略,但只要文件仍在内存中,调色板就由具有 alpha 组件的 Color C# 类型表示。将位图文件加载到 PictureBox 时会发生什么?
    • 当alpha为0时,只能看到带有背景色的pictureBox
    • 加载位图文件时,我的意思是picBoxImage.Image = Image.FromFile("theImage.bmp");。如果它仍然不可见,则意味着您用于显示文件的查看器忽略了 Alpha 通道。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    • 2019-02-08
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    • 2021-08-19
    相关资源
    最近更新 更多