1

I have a bitmap that I am performing a colorizing transformation on. I have the new array of pixels but I'm not sure how to then save them back to disk as an image

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
    }

How can I then save the new pixels as a bitmap on disk?

jimmyjambles
  • 1,631
  • 2
  • 20
  • 34
  • 1
    Remember to dispose of your bitmaps properly. http://stackoverflow.com/questions/5838608/net-and-bitmap-not-automatically-disposed-by-gc-when-there-is-no-memory-left – geedubb Jan 08 '14 at 21:56

2 Answers2

2

I do believe you can use the Bitmap.Save() method after you have loaded the bytes back into a Bitmap object. This post may give you some insight on how to do it.

According to this MSDN document, if you only specify a path while using Bitmap.Save(),

If no encoder exists for the file format of the image, the Portable Network Graphics (PNG) encoder is used.

Community
  • 1
  • 1
OnoSendai
  • 3,960
  • 2
  • 22
  • 46
1

You can convert a byte array to a bitmap using a MemoryStream, and then feeding it into the Image.FromStream method. Your example with this in place would be..

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 - Seems to be a simpler approach than the one on the post I mentioned. – OnoSendai Jan 08 '14 at 22:25
  • I'm not too sure that is necessarily a simpler approach, but achieves a different purpose. This answer simply creates a bitmap object from a byte array, whereas Bitmap.Save() takes a bitmap, and saves it either to a file, or a stream. – Scott Gulliver Jan 08 '14 at 22:28
  • Don't get me wrong, I think simpler is better. And I mean this one - http://stackoverflow.com/questions/6782489/create-bitmap-from-a-byte-array-of-pixel-data – OnoSendai Jan 08 '14 at 22:31
  • Oh, haha - I missed that link :) – Scott Gulliver Jan 08 '14 at 22:33
  • It was hiding in plain sight! ;) – OnoSendai Jan 08 '14 at 22:35
  • I receive an error saying parameter is not valid from FromStream, I should also mention that I really only have the pixel data in the byte[], not the bitmap file itself – jimmyjambles Jan 08 '14 at 22:43
  • Ah, OK. When you say pixel data, is this an ARGB value? If so, you could iterate through the pixels array, and set the relevant pixel on the output bitmap using `bitmap.SetPixel(x,y,Color.FromArgb(pixels[i]));` – Scott Gulliver Jan 08 '14 at 22:53