如何位图保存为图标? [英] How to save Bitmap as icon?

查看:199
本文介绍了如何位图保存为图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要保存的图像文件的位图对象加载(巴纽,.JPEG,.BMP),并将其保存为一个图标(.ICO),以一个单独的文件。

I need to save Bitmap object loaded from image file (.png, .jpeg, .bmp) and save it as an icon (.ico) to a separate file.

首先,我尝试在保存位图对象与图标的imageformat文件:

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();

这一次没有做这项工作了。尽管图标的文件被正确写入,它只有16种颜色和有限的宽度和高度。

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.

我希望能写的图标,自定义的宽度和高度,将$从原始图像P $ pserve颜色。这是可能在.NET achive?

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?

在此先感谢。

推荐答案

使用名称空间System.IO可就是这样一个工作示例

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);
        }
    }
}

这篇关于如何位图保存为图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆