Bitmap.Save Question

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

I am using the Save(stream, imageFormat) method of the Bitmap class with
ImageFormat.Bmp to save a bitmap.

If I then manually load the bitmap file header and bitmap info header
information I find that the biSizeImage is 0... Why doesn't the Save method
set this to the proper value? Is there a way to properly calculate the
number of bytes based off the width, height and bitcount of the image?
 
Hi Jeff,

Thanks for posting in the community!!

Can you tell me how you load the bitmap file header information and refer
biSizeImage ? Using C++ or C#?

Please show me some code snippet to reproduce your issue , Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
I am using C# to open a filestream and then use a binaryreader to read the
data. I use the following code to retrieve the biSizeImage:

BinaryReader binaryReader = new BinaryReader(stream);

// Bitmap File Header = 14 bytes
// Bitmap Info Header (up to biSizeImage) = 20 bytes

stream.Seek(34, SeekOrigin.Current);
int biSizeImage = binaryReader.ReadInt32();

This seems to work fine on files that I save from image editors, however,
when I try it on a file saved using Bitmap.Save() I was get 0 for
biSizeImage.

Here is a sample of how I am saving the file:

System.Drawing.Imaging.ImageFormat imageFormat =
System.Drawing.Imaging.ImageFormat.Bmp;
myBitmap.Save(@"C:\Image.bmp", imageFormat);
 
Hi Jeff,

Thanks very much for your feedback.

Actually, if you view the MSDN documentation of
BITMAPINFOHEADER.biSizeImage at below:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps
_1rw2.asp

You will see that biSizeImage may be 0 for BI_RGB bitmaps. BI_RGB is a
constant value for 0. So you may first check biCompression of
BITMAPINFOHEADER. If this field is 0, then biSizeImage is 0 is an expected
behavior.

In my computer, the biCompression field(offset 30) is really 0, so the
biSizeImage is 0.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Jeff,

Does the my reply make sense to you? Do you still have any concern on this
issue?

Please feel free to feedback, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Sort of... I guess I'm just not sure I can think of a scenerio where you
wouldn't want to know how many bytes made up the image... I guess a
workaround would be to calculate it yourself... but as long as the Save
method is saving the file, why wouldn't it make the calculations and save
them as well? There must be a reason...
 
Hi Jeff,

Thanks very much for your feedback.

When BITMAPINFOHEADER.biCompression is BI_RGB, it means "An uncompressed
format". That is, the bitmap was not compressed. So the bitmap's size can
be calculated yourself, so no need for the biSizeImage to specify the
bitmap size. That is why biSizeImage field is an optional for uncompressed
bitmap.

For more informatin, please refer to:
"Bitmap Storage"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps
_4v1h.asp

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Jeff,

Thanks for your feedback.

I am glad I can help you. :-) If you have any further question, please feel
free to post, I will help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top