C#怎么将生成的bmp图 变为单色黑白图且深色位1
请问一下有没有人知道呀 bmp图我已经生成了 但格式不对 要怎么变呢
核心关键词“PixelFormat.Format1bppIndexed”
然后我们可以去问chargpt了“C# 将图像转换成PixelFormat.Format1bppIndexed的位图并保存”
下面是chartgpt的回复:
您可以使用 C# 中的 Bitmap 类、Graphics 类和 ImageLockMode 枚举来将图像转换成 1 位索引格式,并将其保存为文件。具体步骤如下:
创建一个新的 Bitmap 对象,将其大小设置为原始图像的大小。
csharp
Bitmap bmp = new Bitmap("original_image.jpg");
Bitmap indexedBmp = new Bitmap(bmp.Width, bmp.Height, PixelFormat.Format1bppIndexed);
创建一个用于将原始图像绘制到新位图上的 Graphics 对象。
csharp
using (Graphics g = Graphics.FromImage(indexedBmp))
{
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
// 绘制原始图像到位图上
g.DrawImage(bmp, 0, 0);
}
锁定位图以便进行像素级修改,并准备一个缓冲区来保存像素值。在这个例子中,我们创建了一个 byte 类型的数组,每个元素代表一行位图数据。
csharp
Rectangle rect = new Rectangle(0, 0, indexedBmp.Width, indexedBmp.Height);
BitmapData imageData =
indexedBmp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);
int bytesPerRow = imageData.Stride;
byte[] buffer = new byte[bytesPerRow * indexedBmp.Height];
处理每一行像素,并将数据保存到缓冲区中。
csharp
unsafe
{
byte* p = (byte*)(void*)imageData.Scan0;
int offset = imageData.Stride - ((indexedBmp.Width + 7) / 8);
for (int y = 0; y < indexedBmp.Height; y++)
{
byte value = 0;
int position = 0;
for (int x = 0; x < indexedBmp.Width; x++)
{
Color color = indexedBmp.GetPixel(x, y);
if (color.GetBrightness() > 0.5)
{
value |= (byte)(128 >> position);
}
position++;
if (position == 8 || x == indexedBmp.Width - 1)
{
buffer[y * bytesPerRow + (x >> 3)] = value;
value = 0;
position = 0;
}
}
p += imageData.Stride;
}
}
在缓冲区中生成位图数据后,解锁位图,并将图像保存为文件。
csharp
Marshal.Copy(buffer, 0, imageData.Scan0, buffer.Length);
indexedBmp.UnlockBits(imageData);
indexedBmp.Save("indexed_image.bmp", ImageFormat.Bmp);
你可以使用 C# 中的 Bitmap
和 Graphics
类来将生成的 BMP 图像转换为黑白图像。以下是一个实现这个功能的示例代码:
using System.Drawing;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
// 读入 BMP 文件,注意要替换成你自己的路径
Bitmap bmp = new Bitmap(@"C:\test.bmp");
// 创建新的 Bitmap,并指定宽和高
Bitmap newBmp = new Bitmap(bmp.Width, bmp.Height);
// 遍历每个像素
for (int y = 0; y < bmp.Height; y++)
{
for (int x = 0; x < bmp.Width; x++)
{
// 获取当前像素的颜色
Color color = bmp.GetPixel(x, y);
// 计算灰度值
int gray = (color.R + color.G + color.B) / 3;
// 将灰度值小于 128 的像素变为黑色,大于等于 128 的像素变为白色
if (gray < 128)
{
newBmp.SetPixel(x, y, Color.Black);
}
else
{
newBmp.SetPixel(x, y, Color.White);
}
}
}
// 保存新的 BMP 文件,注意要替换成你自己的路径
newBmp.Save(@"C:\test_bw.bmp");
}
}
}
在上面的代码中,我们首先使用 Bitmap
类读取原始的 BMP 文件,并创建一个新的 Bitmap
对象来存储黑白图像。然后我们遍历原始图像的每个像素,并计算它的灰度值。根据灰度值的大小,我们将像素变为黑色或者白色,并在新的 Bitmap
对象中设置像素的颜色。最后我们使用 Save
方法将新的 BMP 文件保存到磁盘上。
在C#中,可以使用Bitmap
类创建单色(黑白)位图并将深色位设置为1。下面是一个简单的示例代码:
using System.Drawing;
// 创建位图对象并设置宽度和高度
Bitmap bitmap = new Bitmap(100, 100);
// 将位图像素格式设置为 Format1bppIndexed
bitmap = bitmap.Clone(new Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.PixelFormat.Format1bppIndexed);
// 将位图调色板的第一项颜色设置为深色
ColorPalette palette = bitmap.Palette;
palette.Entries[0] = Color.Black;
bitmap.Palette = palette;
// 在这里执行对位图进行绘制的操作
// ...
// 保存位图文件
bitmap.Save("output.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
在上面的代码中,Bitmap
类用于创建位图对象。首先,我们将位图像素格式设置为Format1bppIndexed
,以便将其转换为单色位图形式。然后,通过使用调色板将深色设置为颜色 0(也就是第一项),从而将 1 填入其他非零像素。 您还可以在绘制图像之前在bitmap
变量中进行设置。最后,将位图保存到文件中。
在 C# 中,可以通过使用 Bitmap 类和 Graphics 类来生成单色位图且深色位为1的黑白图。
以下是一个示例代码,生成一个100x100像素的单色位图,并将其保存为'bmp'格式:
Bitmap bmp = new Bitmap(100, 100, System.Drawing.Imaging.PixelFormat.Format1bppIndexed);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
g.Dispose();
bmp.Save("filename.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
该代码首先创建了一个100x100像素的单色位图,像素格式设置为PixlFormat.Format1bppIndexed。接着,使用Graphics类的Clear方法填充整个位图为白色。最后,将位图保存到指定的文件名。
注意,由于单色位图只有黑白两种颜色,因此深色位(bit0)被设置为1,而浅色位(bit1)被设置为0。因此,所有黑色像素都将被表示为0,白色像素都将被表示为1。
希望这可以帮助您生成所需的单色位图。
你代码看看lo