我有一个位图,我正在对其执行着色转换。 我有新的像素数组,但我不确定如何将它们作为图像保存回磁盘
public static void TestProcessBitmap(string inputFile, string outputFile)
{
Bitmap bitmap = new Bitmap(inputFile);
Bitmap formatted = bitmap.Clone(new Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
byte[] pixels = BitmapToPixelArray(formatted);
pixels = Process8Bits(pixels, System.Windows.Media.Colors.Red);
Bitmap output = new Bitmap(pixels); //something like this
}
然后如何将新像素保存为磁盘上的位图?
我相信您可以在将字节加载回位图对象后使用 Bitmap.Save()
方法。这篇文章可能会给你一些关于如何做到这一点的见解。
根据此 MSDN 文档,如果在使用 Bitmap.Save()
时仅指定路径,
如果图像的文件格式不存在编码器,则便携式 使用网络图形 (PNG) 编码器。
可以使用 MemoryStream 将字节数组转换为位图,然后将其馈送到 Image.FromStream 方法中。你的例子将是..
public static void TestProcessBitmap(string inputFile, string outputFile)
{
Bitmap bitmap = new Bitmap(inputFile);
Bitmap formatted = bitmap.Clone(new Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
byte[] pixels = BitmapToPixelArray(formatted);
pixels = Process8Bits(pixels, System.Windows.Media.Colors.Red);
using (MemoryStream ms = new MemoryStream(pixels))
{
Bitmap output = (Bitmap)Image.FromStream(ms);
}
}