【发布时间】: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