C# 调色板alpha为零时无法将单色BMP文件显示到pictureBox
我已成功生成BMP文件。(我仍在调整一些格式的变化,但我必须这样做)。我用一个PictureBox创建了一个winform。根据我的最后一个问题,我现在可以保存文件,但现在位图无法显示在PictureBox中。这是在我更改了之后的调色板以将颜色的alpha设置为0后发生的 我的bmp是单色的(我想知道是否有更简单的方法) 我的代码是C# 调色板alpha为零时无法将单色BMP文件显示到pictureBox,c#,bitmap,image-manipulation,bmp,C#,Bitmap,Image Manipulation,Bmp,我已成功生成BMP文件。(我仍在调整一些格式的变化,但我必须这样做)。我用一个PictureBox创建了一个winform。根据我的最后一个问题,我现在可以保存文件,但现在位图无法显示在PictureBox中。这是在我更改了之后的调色板以将颜色的alpha设置为0后发生的 我的bmp是单色的(我想知道是否有更简单的方法) 我的代码是 private void btnNameUsage_Click(object sender, EventArgs e) { Bitmap bmp = n
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和白色:FFFFFF(使用此调色板,我可以在picbox中看到bmp),但我正在更改为黑色:00000000和白色:FFFFFF 00(因为您只能看到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之间的关系是什么…alpha表示颜色的不透明度。0表示颜色完全透明,即您只能看到画布/背景,128表示颜色为50%透明,表示您可以看到相同数量的颜色和背景,255表示完全不透明度/零透明度,表示您只能看到颜色 简而言之,白色是“FFFFFFFF”,黑色是“FF000000”
如果要通过RGB组件指定颜色,请使用FromArgb方法和三个参数,这三个参数隐式地将alpha通道设置为255。是的,我想知道alpha为0(白色:00FFFFFF黑色:00000000)时的原因我可以在保存到文件时看到BMP,但在分配到PictureBox时看不到它…我认为这是因为位图文件中的颜色表没有alpha通道。因此,当您将其保存为Windows位图文件时,先前的alpha组件将被忽略,但只要该文件仍在内存中,调色板将由具有alpha组件的颜色C#type表示。将位图文件加载到PictureBox时会发生什么情况?当alpha为0时,加载位图文件时,我只能看到背景色的PictureBox,我的意思是
picBoxImage.Image=Image.FromFile(“theImage.bmp”)代码>。如果仍然不可见,则表示用于显示文件的查看器忽略alpha通道。