c#生成bmp的单色图且深度为1
时间: 2024-03-21 12:40:35 浏览: 31
在C#中,可以使用Bitmap类来创建和操作位图图像。以下是一个生成深度为1的单色位图的示例代码:
```csharp
// 创建一张 16x16 的黑白图像
Bitmap bmp = new Bitmap(16, 16, System.Drawing.Imaging.PixelFormat.Format1bppIndexed);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
// 将图像保存为 BMP 文件
string filename = "test.bmp";
bmp.Save(filename, System.Drawing.Imaging.ImageFormat.Bmp);
```
这段代码中,我们首先创建了一个16x16的黑白图像,然后使用Graphics类将图像填充为白色。接着,使用Bitmap类的Save方法将图像保存为BMP文件。在创建Bitmap对象时,我们需要指定像素格式为Format1bppIndexed,即深度为1的单色位图。
相关问题
c#bmp图片转换为jpg图片
你可以使用 System.Drawing 命名空间中的 Bitmap 类来打开 BMP 图片文件,然后使用 Save 方法将其另存为 JPEG 图片文件。以下是一个简单的示例代码:
```csharp
using System.Drawing;
using System.Drawing.Imaging;
// 打开 BMP 图片文件
Bitmap bmp = new Bitmap("input.bmp");
// 将 BMP 图片保存为 JPEG 图片文件
bmp.Save("output.jpg", ImageFormat.Jpeg);
```
注意,这个示例代码只适用于 BMP 图片文件中没有 alpha 通道的情况。如果 BMP 图片文件中包含 alpha 通道,保存为 JPEG 图片文件时会丢失 alpha 通道信息。
c# 压缩bmp图片的代码
以下是使用C#压缩BMP图片的代码示例:
```csharp
using System.Drawing;
using System.Drawing.Imaging;
public static void CompressBmp(string sourcePath, string targetPath, long quality)
{
using (Bitmap source = new Bitmap(sourcePath))
{
ImageCodecInfo codec = GetEncoderInfo("image/jpeg");
EncoderParameters parameters = new EncoderParameters(1);
parameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);
source.Save(targetPath, codec, parameters);
}
}
private static ImageCodecInfo GetEncoderInfo(string mimeType)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
for (int i = 0; i < codecs.Length; i++)
{
if (codecs[i].MimeType == mimeType)
{
return codecs[i];
}
}
return null;
}
```
使用示例:
```csharp
CompressBmp(@"C:\path\to\source.bmp", @"C:\path\to\target.jpg", 50L);
```
其中,`sourcePath`参数表示要压缩的BMP图片路径,`targetPath`参数表示压缩后的图片路径,`quality`参数表示压缩质量,取值范围为0~100。压缩质量越高,图片质量就越好,但文件大小也就越大。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)