2

I'm kind of confused as to the way C# saves bitmaps. I currently use this statement to save a bitmap to (virtually) any extension (.ico, .dds, etc.).

Bitmap.Save(FileName);

This actually works for any extension I choose, but I'm having doubts it's saving in correct format. Reason for said doubts is because I found another article here, How to save Bitmap as icon?, which does so like:

// Create a Bitmap object from an image file.
Bitmap bmp = new Bitmap(sFn);

// Get an Hicon for myBitmap. 
IntPtr Hicon = bmp.GetHicon();

// Create a new icon from the handle. 
 Icon newIcon = Icon.FromHandle(Hicon);

//Write Icon to File Stream
System.IO.FileStream fs = new System.IO.FileStream(destFileName, System.IO.FileMode.OpenOrCreate);
newIcon.Save(fs);

Which is obviously way different. So two questions:

  1. How can/does the first approach work if the second is the "correct" way?
  2. Would I need to customize save properties for each individual extension I wish to support or is the first method sufficient for any extension?
Community
  • 1
  • 1
  • You didn't specify the image format in the Save() call. So it is saved as a PNG. With the wrong filename extension, no doubt. That doesn't trip up too many programs, they look in the file to know the real image format, their header identifies the real format. PNG is a very good format and one you should favor. There isn't any way to save a .ico or .dds file, gdiplus does not have encoders for such formats. ImageFormat.Icon is defined but not implemented. – Hans Passant Dec 03 '15 at 14:21
  • So really, the .ico I think is an .ico is just a .png (or other type, for all I know) with the .ico extension. And to support files such as .dds (for which GDI+ does not have encoders for), I have to implement my own encoder. That makes more sense. Thanks for the reply! –  Dec 03 '15 at 14:30

1 Answers1

0

The proper way would be to use the original Bitmap.Save(Filename) but you are missing the ImageFormat variable which will basically do the actualy Conversion from an image obejct ot the specified format.

Currently the way you are using it results in a file named "somefile.somextension" but does not have the actual contents of an icon object for example.

best way would be to use :

Bitmap.Save(String, ImageFormat)

for example, to save a .ico file :

Bitmap.Save(String, System.Drawing.Imaging.ImageFormat.Icon)

Hope this helps, good luck.

Noxymon
  • 201
  • 4
  • 15
  • Just try the code you recommend yourself first. Pretty easy to see that your snippet can never work. – Hans Passant Dec 03 '15 at 14:22
  • I have, it works. have used it several times before check the documentation as well. https://msdn.microsoft.com/en-us/library/system.drawing.imaging.imageformat(v=vs.110).aspx – Noxymon Dec 03 '15 at 14:25
  • If you try using the file as an icon, it won't work. I did a simple test by trying to set a Windows folder icon to it and it said it does not contain any icon files. –  Dec 03 '15 at 14:37
  • This might be due to different versions of Windows supporting different pixel ratio for .ico files. – Noxymon Dec 03 '15 at 14:45