将图标文件保存到硬盘

发布于 2024-11-06 08:52:32 字数 594 浏览 1 评论 0原文

我知道这一定非常简单 - 令人难以置信的是,基于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾挽 2024-11-13 08:52:32

如果您保存图标而不先转换为位图,您将获得更好的结果。这是因为“图标”可以包含多个尺寸,而位图是在转换过程中选择的单个尺寸。

Icon 类没有保存到文件的方法,但它有保存到 FileStream 的方法,因此您可以像这样保存它:

        string s = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\IconData.ico";
        using (FileStream fs = new FileStream(s, FileMode.Create))
            ico.Save(fs);

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:

        string s = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\IconData.ico";
        using (FileStream fs = new FileStream(s, FileMode.Create))
            ico.Save(fs);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文