none
Forcing a Bitmap.Save to save uncompressed

    Question

  • I load an uncompressed bitmap into a Bitmap object from a .bmp on the local harddrive, do some maniuplaction of it (cropping a black border around it, and then resizing it), and then I want to save it in a different location.  All works except that the version that gets saved is compressed even though I didn't specify anywhere that it should be compressed.  I also couldn't see where I could specify that when the file is saved it should be saved uncompressed.
    Thursday, October 22, 2009 10:55 PM

Answers

  • That's probably because the didn't specify the 2nd argument to the Save() method.  That saves the image in the PNG format, regardless of what filename extension you use.  If you want it uncompressed, you'll need to specify ImageFormat.Bmp

    Hans Passant.
    Thursday, October 22, 2009 11:20 PM
  • You saved TestCopy.bmp in the default ImageFormat, Portable Network Graphics (PNG), which is a compressed format.  If you want to save it uncompressed, save it as a Bitmap (BMP) as demonstrated in the code I posted earlier in this thread.  You forgot to read the posts by Nobugz and myself earlier in this thread.
    Tuesday, October 27, 2009 5:30 AM

All replies

  • That's probably because the didn't specify the 2nd argument to the Save() method.  That saves the image in the PNG format, regardless of what filename extension you use.  If you want it uncompressed, you'll need to specify ImageFormat.Bmp

    Hans Passant.
    Thursday, October 22, 2009 11:20 PM
  • I did use that.

    the code is something like:

    Bitmap bmp = Bitmap("Input.bmp");

    bmp = MyCrop(bmp);
    bmp = MyResize(bmp);

    bmp.Save("Output.bmp", ImageFormat.bmp);



    I had posted this same question a little while ago and had a "workaround" whereby I had to copy my bitmap to a second bitmap whose dimensions where the same but was created with PixelFormat.Format24bppRgb and then used a Graphics to copy the bitmap and the the copied was saved.  And that seemd to work for the source of bitmaps that I had at the time but now for the source I have now it doesn't work.
    Thursday, October 22, 2009 11:37 PM
  • Do you have code that reproduces your result.  This code doesn't:

        private void button1_Click(object sender, EventArgs e)
        {      
          Bitmap bmp = new Bitmap("C:\\Users\\Public\\Pictures\\Sample Pictures\\Creek.jpg");
          //1024x768, 24bpp
          Bitmap bmp1 = new Bitmap(bmp);
          //1024x768, 32bpp
          bmp.Save("Output.bmp", ImageFormat.Bmp);
          Byte[] b = File.ReadAllBytes("Output.bmp");
          int b_length = b.Length; //= 2359350
          int should_be = 54 + 4*768*(1024*24 + 31)/32;  // = 2359350
        }
    Friday, October 23, 2009 3:01 AM
  • I'm going to guess that you have an unusual definition of the term "compressed bitmap".  How do you know the file contains compressed data after you save?
    Hans Passant.
    Friday, October 23, 2009 9:08 AM
  • I know it's "compressed" because even through each bitmap has the same dimensions the file sizes varry and they are smaller than the height * width * 3 bytes, and that doesn't include the header.  I know they are a bitmap because the bitmap header information contains the obligatory "BM".

    On a side note I was able to get it to work consistantly now thanks to an idea that JohnWein gave me, and that was to stream it into memory and then stream it to a file.  I just wish a parameter while saving allowed you to specify non-compression.
    Friday, October 23, 2009 10:30 PM
  • Hmm, I'm not buying.  However, you already got that workaround in your last thread.  Why did you start another one?
    Hans Passant.
    Friday, October 23, 2009 11:31 PM
  • Sorry I had to get a work around because I was headed for a deadline.  I started another thread because I had closed the old one and the "trick" that I used didn't seem to be working.  So I figured that there must be something else that was overlooked.  I finally tracked down the to an initialization that was done differently because of the source from where the original bitmap came from in my code.  So I am still using a trick.

    Try this out if you're not buying what's happening.  Use paint and create a bitmap all white 640x480 and save it as a 24bit color bitmap.  You will see that it is 901kb which is what you would expect given 640 * 480 * 3 = 921600.  Now write the following code:

    Bitmap bmp = new Bitmap("C:\\Test.bmp");
    bmp.RotateFlip(RotateFlipType.Rotate90FlipNone);
    bmp.Save("C:\\TestCopy.bmp");

    You will see that TestCopy.bmp is only 6kb and this is because TestCopy.bmp.

    Let me know what happend.
    Tuesday, October 27, 2009 3:42 AM
  • You saved TestCopy.bmp in the default ImageFormat, Portable Network Graphics (PNG), which is a compressed format.  If you want to save it uncompressed, save it as a Bitmap (BMP) as demonstrated in the code I posted earlier in this thread.  You forgot to read the posts by Nobugz and myself earlier in this thread.
    Tuesday, October 27, 2009 5:30 AM
  • In going back and forth in the code I think there was a time even when I specifed ImageFormat.Bmp in the save I was still getting a compressed version, and maybe that's because I was doing a lot more things to the bitmap and makeing copies where only the bits were copied.

    In any case the whole point is why should I have to specify ImageFormat.Bmp.  How dare the library change the format without explict instructions to do so.  It's like me putting a regular soda into a fridge and then getting a diet back because it's better for me.  I didn't ask for it, so don't give it to me, if I wanted it I'd ask for it.

    Tuesday, October 27, 2009 4:22 PM
  • In going back and forth in the code I think there was a time even when I specifed ImageFormat.Bmp in the save I was still getting a compressed version, and maybe that's because I was doing a lot more things to the bitmap and makeing copies where only the bits were copied.

    In any case the whole point is why should I have to specify ImageFormat.Bmp.  How dare the library change the format without explict instructions to do so.  It's like me putting a regular soda into a fridge and then getting a diet back because it's better for me.  I didn't ask for it, so don't give it to me, if I wanted it I'd ask for it.


    There you go!  You tell whoever is listening.  Microsoft!  I want you to write all of your programs exclusively for me.
    Tuesday, October 27, 2009 5:08 PM
  • Ok,

    So someone explain to me how:

    Bitmap bmp = new Bitmap("C:\\Test.bmp");
    bmp.RotateFlip(RotateFlipType.Rotate90FlipNone);
    bmp.Save("C:\\TestCopy.bmp");

    can yield a different result from:

    Bitmap bmp = new Bitmap("C:\\Test.bmp");
    bmp.Save("C:\\TestCopy.bmp");


    Am I the only one who thinks that the result from both should be the same?

    Wednesday, October 28, 2009 12:34 AM
  • Wow, no response from the Microsoft guys, wonder what that means?
    Friday, October 30, 2009 5:16 AM