将图标文件保存到硬盘
我知道这一定非常简单 - 令人难以置信的是,基于 VB6 中这个问题的简单性,我花了这么长时间来寻找这个问题的答案。我只想使用 Icon.ExtractAssociatedIcon 从 EXE 文件中提取图标,然后将此图标文件保存到我的硬盘上。
所以,这就是我所拥有的,我还将向您展示我尝试过的内容,这样您就不会认为我很懒。
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 字节。再说一次,我确信这一定非常容易做到,但我一生都无法弄清楚。
谢谢你!
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);
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.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您保存图标而不先转换为位图,您将获得更好的结果。这是因为“图标”可以包含多个尺寸,而位图是在转换过程中选择的单个尺寸。
Icon 类没有保存到文件的方法,但它有保存到 FileStream 的方法,因此您可以像这样保存它:
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.
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: