Bitmap.Save 保存一个图标实际上保存了一个 .png [英] Bitmap.Save to save an icon actually saves a .png

查看:42
本文介绍了Bitmap.Save 保存一个图标实际上保存了一个 .png的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个程序,该程序将根据图块集图像生成 108 个图标组合(标准 Windows .ico 文件).

我使用 System.Drawing.Bitmap 类来构建每个组合,并像这样保存它们:

Bitmap IconBitmap = new Bitmap(16, 16);//一些处理,写入源tileset的不同部分//...IconBitmap.Save(Path.Combine(TargetPath, "Icon" + Counter + ".ico"),ImageFormat.Icon);

但是我发现保存的文件实际上是PNG.Windows 资源管理器和 Visual Studio 都不能正确显示它,但 GIMP 可以,如果我在十六进制查看器中打开它,我看到的是:

00000000 89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52‰PNG........IHDR00000010 00 00 00 10 00 00 00 10 08 06 00 00 00 1F F3 FF .....................óÿ00000020 61 00 00 00 01 73 52 47 42 00 AE CE 1C E9 00 00 a....sRGB.®Î.é..00000030 00 04 67 41 4D 41 00 00 B1 8F 0B FC 61 05 00 00 ..gAMA..±..üa...00000040 00 09 70 48 59 73 00 00 0E C3 00 00 0E C3 01 C7 ..pHYs...Ã...Ã.Ç00000050 6F A8 64 00 00 00 15 49 44 41 54 38 4F 63 60 18 o¨d....IDAT8Oc`.00000060 05 A3 21 30 1A 02 A3 21 00 09 01 00 04 10 00 01 .£!0..£!........00000070 72 A5 13 76 00 00 00 00 49 45 4E 44 AE 42 60 82 r¥.v....IEND®B`‚

另外,如果我将 .ico 重命名为 .png Windows Explorer 可以正确显示它.

即使我在位图上什么也没做,我也会得到这个结果(我用 new 构建它并直接Save 它,这给了我一个黑色的 png).

我做错了什么?

我也试过这个,它给了我可怕的 16 色图标,但我还是宁愿避免这个解决方案(使用句柄):

Icon NewIcon = Icon.FromHandle(IconBitmap.GetHicon());FileStream FS = new FileStream(Path.Combine(Target, "Icon" + Counter + ".ico"),FileMode.Create);NewIcon.Save(FS);

解决方案

ImageFormat.Icon 确实不能像您想象的那样用于写作,.NET 根本不支持写作.ico 文件并简单地转储 PNG 数据.

上有几个项目CodeProject(和这个)(和另一个),让你写一个 .ico 文件,其实没那么难.文件格式非常简单,支持 BMP 和 PNG 数据.

I need to write a program that will generate 108 combinaisons of icons (standard windows .ico files) based on a tileset image.

I use the class System.Drawing.Bitmap to build each combinaison, and I save them like this:

Bitmap IconBitmap = new Bitmap(16, 16);
// Some processing, writing different parts of the source tileset
// ...
IconBitmap.Save(Path.Combine(TargetPath, "Icon" + Counter + ".ico"),
                ImageFormat.Icon);

But I found out that the file saved is actually a PNG. Neither Windows Explorer nor Visual Studio can display it correctly, but GIMP can, and if I open it in an Hex viewer, here is what i see:

00000000  89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52  ‰PNG........IHDR
00000010  00 00 00 10 00 00 00 10 08 06 00 00 00 1F F3 FF  ..............óÿ
00000020  61 00 00 00 01 73 52 47 42 00 AE CE 1C E9 00 00  a....sRGB.®Î.é..
00000030  00 04 67 41 4D 41 00 00 B1 8F 0B FC 61 05 00 00  ..gAMA..±..üa...
00000040  00 09 70 48 59 73 00 00 0E C3 00 00 0E C3 01 C7  ..pHYs...Ã...Ã.Ç
00000050  6F A8 64 00 00 00 15 49 44 41 54 38 4F 63 60 18  o¨d....IDAT8Oc`.
00000060  05 A3 21 30 1A 02 A3 21 00 09 01 00 04 10 00 01  .£!0..£!........
00000070  72 A5 13 76 00 00 00 00 49 45 4E 44 AE 42 60 82  r¥.v....IEND®B`‚

Also if I rename the .ico to .png Windows Explorer can display it properly.

I have this result even if I do NOTHING on the bitmap (I construct it with new and Save it directly, that gives me a black png).

What am I doing wrong?

I also tried this, which gave me awful 16 color icons, but I would prefer to avoid this solution anyway (using handles) :

Icon NewIcon = Icon.FromHandle(IconBitmap.GetHicon());
FileStream FS = new FileStream(Path.Combine(Target, "Icon" + Counter + ".ico"),
        FileMode.Create);
NewIcon.Save(FS);

解决方案

It's true that the ImageFormat.Icon does not work for writing as you'd suppose, .NET simply does not support writing .ico files and simply dumps the PNG data.

There are a few projects on CodeProject (and this one) (and another one) and that let you write an .ico file, it's actually not that hard. The file format is pretty straight-forward, and supports BMP and PNG data.

这篇关于Bitmap.Save 保存一个图标实际上保存了一个 .png的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆