【问题标题】:Save bitmap pixel array as a new bitmap将位图像素数组保存为新位图
【发布时间】:2014-01-08 21:54:15
【问题描述】:

我有一个正在执行着色转换的位图。我有新的像素数组,但我不确定如何将它们作为图像保存回磁盘

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
    }

然后如何将新像素保存为磁盘上的位图?

【问题讨论】:

标签: c# bitmap


【解决方案1】:

我相信您可以在将字节加载回位图对象后使用Bitmap.Save() 方法。 This post 可能会让您了解如何操作。

According to this MSDN document,如果在使用Bitmap.Save()时只指定路径,

如果图像的文件格式不存在编码器,则便携式 使用了网络图形 (PNG) 编码器。

【讨论】:

  • 在这种情况下,我在内存中的流是位图中的实际像素,而不是文件本身
【解决方案2】:

您可以使用 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);
    }
}

【讨论】:

  • +1 - 似乎比我提到的帖子中的方法更简单。
  • 我不太确定这一定是一种更简单的方法,但可以达到不同的目的。这个答案只是从一个字节数组创建一个位图对象,而 Bitmap.Save() 需要一个位图,并将其保存到文件或流中。
  • 别误会,我认为越简单越好。我的意思是这个 - stackoverflow.com/questions/6782489/…
  • 哦,哈哈 - 我错过了那个链接 :)
  • 它藏在了众目睽睽之下! ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-18
  • 2015-11-03
  • 1970-01-01
相关资源
最近更新 更多