2014-01-08 100 views
1

我有一个位图正在执行着色转换。我有像素的新阵列,但我不知道那么如何将其保存到磁盘图像将位图像素阵列保存为新的位图

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 
    } 

我怎样才能然后保存新的像素作为磁盘上的一个位图?

+1

记得妥善处理你的位图。 http://stackoverflow.com/questions/5838608/net-and-bitmap-not-automatically-disposed-by-gc-when-there-is-no-memory-left – geedubb

回答

2

我相信你可以使用Bitmap.Save()方法,你已经将字节加载回Bitmap对象。 This post可能会给你一些关于如何做到这一点的见解。

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

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

+0

在这种情况下,我在内存中的流是的位图中的实际像素,而不是文件本身 – jimmyjambles

1

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

+1 - 似乎是比我提到的文章更简单的方法。 – OnoSendai

+0

我不太确定这必然是一个更简单的方法,但实现了不同的目的。这个答案只是从一个字节数组创建一个位图对象,而Bitmap.Save()需要一个位图,并将其保存到文件或流中。 –

+0

不要误解我的意思,我认为简单更好。我的意思是这一个 - http://stackoverflow.com/questions/6782489/create-bitmap-from-a-byte-array-of-pixel-data – OnoSendai