如何将位图另存为图标?
我需要保存从图像文件(.png、.jpeg、.bmp)加载的位图对象并将其作为图标(.ico)保存到单独的文件中。
首先,我尝试使用 Icon ImageFormat 将 Bitmap 对象保存到文件中:
using System.Drawing;
Bitmap bmp = (Bitmap)pictureBox1.Image;
bmp.Save(@"C:\icon.ico", Imaging.ImageFormat.Icon);
此操作失败,因为生成的图标格式不正确,并且无法用作图标。
下一个是从 Bitmap 获取 HIcon 并将其保存到文件中:
using System.Drawing;
using System.IO;
StreamWriter iconWriter = new StreamWriter(@"C:\icon.ico");
Icon ico = Icon.FromHandle(((Bitmap)pictureBox1.Image).GetHicon())
ico.Save(iconWriter.BaseStream);
iconWriter.Close();
iconWriter.Dispose();
这个也不能完成这项工作。尽管图标文件编写正确,但它只有 16 种颜色,并且宽度和高度有限。
我希望能够编写具有自定义宽度和高度的图标,以保留原始图像的颜色。这可以在.NET 中实现吗?
提前致谢。
I need to save Bitmap object loaded from image file (.png, .jpeg, .bmp) and save it as an icon (.ico) to a separate file.
First I tried saving Bitmap object to a file with Icon ImageFormat:
using System.Drawing;
Bitmap bmp = (Bitmap)pictureBox1.Image;
bmp.Save(@"C:\icon.ico", Imaging.ImageFormat.Icon);
This one fails, as the icon produced is not in a proper format and it cannot be used as an icon.
Next one was to get HIcon from Bitmap and save it to a file:
using System.Drawing;
using System.IO;
StreamWriter iconWriter = new StreamWriter(@"C:\icon.ico");
Icon ico = Icon.FromHandle(((Bitmap)pictureBox1.Image).GetHicon())
ico.Save(iconWriter.BaseStream);
iconWriter.Close();
iconWriter.Dispose();
This one does not do the job too. Although icon file is properly written, it has only 16 colors and a limited width and height.
I'd like to be able to write icons with custom width and height that would preserve colors from the original image. Is this possible to achive in .NET?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用名称空间 System.IO 的工作示例可以是这样的
a working sample using the name space System.IO can be like this