如何将位图另存为图标?

发布于 2024-09-29 12:25:17 字数 727 浏览 1 评论 0原文

我需要保存从图像文件(.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 技术交流群。

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

发布评论

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

评论(1

第几種人 2024-10-06 12:25:17

使用名称空间 System.IO 的工作示例可以是这样的

[System.Runtime.InteropServices.DllImport("user32.dll")]
extern static bool DestroyIcon(IntPtr handle);

private void buttonConvert2Ico_Click(object sender, EventArgs e)
{
    OpenFileDialog openFileDialog1 = new OpenFileDialog 

    openFileDialog1.InitialDirectory = "C:\\Data\\";
    openFileDialog1.Filter = "BitMap(*.bmp)|*.bmp";
    openFileDialog1.FilterIndex = 2;
    openFileDialog1.RestoreDirectory = true;

    if(openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        try
        {
            string sFn = openFileDialog1.FileName;
            MessageBox.Show("Filename=" + sFn);
            string destFileName = sFn.Substring(0, sFn.Length -3) +"ico";

            // Create a Bitmap object from an image file.
            Bitmap bmp = new Bitmap(sFn);

            // Get an Hicon for myBitmap. 
            IntPtr Hicon = bmp.GetHicon();

            // Create a new icon from the handle. 
            Icon newIcon = Icon.FromHandle(Hicon);

            //Write Icon to File Stream
            System.IO.FileStream fs = new System.IO.FileStream(destFileName, System.IO.FileMode.OpenOrCreate);
            newIcon.Save(fs);
            fs.Close();
            DestroyIcon(Hicon);

            //DestroyIcon( hIcon);
            setStatus("Created icon From=" + sFn + ", into " + destFileName);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: Could not read/write file. Original error: " + ex.Message);
        }
    }
}

a working sample using the name space System.IO can be like this

[System.Runtime.InteropServices.DllImport("user32.dll")]
extern static bool DestroyIcon(IntPtr handle);

private void buttonConvert2Ico_Click(object sender, EventArgs e)
{
    OpenFileDialog openFileDialog1 = new OpenFileDialog 

    openFileDialog1.InitialDirectory = "C:\\Data\\";
    openFileDialog1.Filter = "BitMap(*.bmp)|*.bmp";
    openFileDialog1.FilterIndex = 2;
    openFileDialog1.RestoreDirectory = true;

    if(openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        try
        {
            string sFn = openFileDialog1.FileName;
            MessageBox.Show("Filename=" + sFn);
            string destFileName = sFn.Substring(0, sFn.Length -3) +"ico";

            // Create a Bitmap object from an image file.
            Bitmap bmp = new Bitmap(sFn);

            // Get an Hicon for myBitmap. 
            IntPtr Hicon = bmp.GetHicon();

            // Create a new icon from the handle. 
            Icon newIcon = Icon.FromHandle(Hicon);

            //Write Icon to File Stream
            System.IO.FileStream fs = new System.IO.FileStream(destFileName, System.IO.FileMode.OpenOrCreate);
            newIcon.Save(fs);
            fs.Close();
            DestroyIcon(Hicon);

            //DestroyIcon( hIcon);
            setStatus("Created icon From=" + sFn + ", into " + destFileName);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: Could not read/write file. Original error: " + ex.Message);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文