保存图标文件到硬盘驱动器 [英] Save Icon File To Hard Drive
问题描述
我知道,这一定是非常容易 - 这是令人难以置信我已搜查多长时间回答这个问题,基于它在VB6多么简单。我只是想提取使用Icon.ExtractAssociatedIcon一个EXE文件的图标,然后该图标文件保存到我的硬盘。
I know that this must be incredibly easy - It's unbelievable how long I have searched for an answer to this question based on how simple it is in VB6. I simply want to extract an Icon from an EXE File using Icon.ExtractAssociatedIcon, and then save this icon file to my hard drive.
所以,这里是我的,我也会告诉你我已经试过这样你就不会认为我是懒惰。
So, here is what I have, and I will also show you what I have tried so you don't think I'm being lazy.
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
string s = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\IconData.ico";
Icon ico = Icon.ExtractAssociatedIcon(ofd.FileName);
Bitmap bmp = ico.ToBitmap();
bmp.Save(s, System.Drawing.Imaging.ImageFormat.Icon);
上面的代码只是让一个名为IconData.ico我的桌面上的文件,该文件是0字节长度。同样,我相信这一定是非常容易的事,但对我的生活,我不能明白。
The above code just makes a file called "IconData.ico" on my desktop which is 0 bytes in length. Again, I am sure this must be incredibly easy to do, but for the life of my I can't figure it out.
感谢您!
推荐答案
如果您保存图标不先转换为位图,你会得到更好的效果。这是因为图标可以包含多个大小而一个位图是在转换过程中选择了单一尺寸。
You will get better results if you save the icon without first converting to a bitmap. This is because an "Icon" can contain multiple sizes whereas a bitmap is a single size chosen during the conversion.
Icon类没有保存到文件的方法,但它确实有一个保存的FileStream方法,这样可以节省这样的:
The Icon class does not have a save to file method, but it does have a save to FileStream method, so you can save it like this:
string s = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\IconData.ico";
using (FileStream fs = new FileStream(s, FileMode.Create))
ico.Save(fs);
这篇关于保存图标文件到硬盘驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!