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

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

问题描述

我需要编写一个程序,将产生的图标108 combinaisons(标准Windows .ico文件)基于地形设置图像。

我使用类System.Drawing.Bitmap构建每个combinaison,我救他们是这样的:

 位图IconBitmap =新位图(16,16);
//某些处理,编写源地形设置的不同部位
// ...
IconBitmap.Save(Path.Combine(TARGETPATH​​,图标+计数器+。ICO),
                ImageFormat.Icon);

不过,我发现,保存的文件实际上是一个PNG。无论是Windows资源管理器或Visual Studio中可以正确的显示,但GIMP可以了,如果我在十六进制查看器中打开它,这里是我所看到的:

  00000000 89 50 47 4E 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 .............. OY
00000020 61 00 00 00 01 73 52 47 42 00 AE CE 1C E9 00 00 ....sRGB.®Î.é..
00000030 00 04 67 41 41 4D 00 00 B1 8F 0B FC 61 05 00 00 ..gAMA ..±..üa...
00000040 00 09 70 48 59 73 00 00 0E 00 C3 00 0E C3 01 C7 ..pHYs ...一个...交流转换器
00000050 6F A8 64 00 00 00 15 49 44 41 54 38 63 4F 60 18 OD .... IDAT8Oc`。
00000060 05 21 A3 30 1A 02 21 A3 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资源管理器可以正常显示了。

我有这样的结果,即使我什么都不做的位图(我与构造它保存它直接,这给了我一个黑色的PNG)。

我在做什么错了?

我也试过这个,这给了我可怕的16色图标,但我会preFER避免这种情况的解决方案呢(使用句柄):

 图标NewIcon = Icon.FromHandle(IconBitmap.GetHicon());
的FileStream FS =新的FileStream(Path.Combine(目标,图标+计数器+。ICO),
        FileMode.Create);
NewIcon.Save(FS);


解决方案

这是真的,在 ImageFormat.Icon 不写,你会想,.NET工作根本不支持写入.ico文件,只是转储PNG数据。

有上的 $ C $的CProject (和之一)(和另一个),并且让你写一个 .ICO 文件,它实际上并不难。该文件格式是pretty直线前进,并支持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天全站免登陆