久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何將位圖另存為圖標?

How to save Bitmap as icon?(如何將位圖另存為圖標?)
本文介紹了如何將位圖另存為圖標?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我需要保存從圖像文件(.png、.jpeg、.bmp)加載的 Bitmap 對象,并將其作為圖標 (.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.

首先,我嘗試使用 Icon ImageFormat 將 Bitmap 對象保存到文件中:

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.

接下來是從 Bitmap 中獲取 HIcon 并將其保存到文件中:

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.

我希望能夠編寫具有自定義寬度和高度的圖標,以保留原始圖像的顏色.這可以在 .NET 中實現嗎?

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

這篇關于如何將位圖另存為圖標?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Right-click on a Listbox in a Silverlight 4 app(右鍵單擊 Silverlight 4 應用程序中的列表框)
WPF c# webbrowser scrolls over top menu(WPF c# webbrowser 在頂部菜單上滾動)
C# Console app - How do I make an interactive menu?(C# 控制臺應用程序 - 如何制作交互式菜單?)
How to add an icon to System.Windows.Forms.MenuItem?(如何向 System.Windows.Forms.MenuItem 添加圖標?)
How to avoid duplicate form creation in .NET Windows Forms?(如何避免在 .NET Windows Forms 中創建重復的表單?)
Building a database driven menu with ASP.NET, JQuery and Suckerfish(使用 ASP.NET、JQuery 和 Suckerfish 構建數據庫驅動的菜單)
主站蜘蛛池模板: 免费国产视频在线观看 | 一道本不卡视频 | 日日夜夜天天 | 在线婷婷| 国产污视频在线 | 天天干天天想 | 亚洲网站观看 | 国产视频线观看永久免费 | 精品一二区 | 91精品国产综合久久久动漫日韩 | 久久久精品一区二区三区 | 国产人免费人成免费视频 | 成人av播放 | 一区二区三区在线播放 | 成人免费在线观看 | 久久久.com | 激情网站在线观看 | 日日夜夜狠狠操 | 天天插天天搞 | 久久久久久中文字幕 | 亚洲一级黄色 | 日韩一区二区三区在线观看 | 国产乱码高清区二区三区在线 | 国产在线观看一区二区三区 | 精品国产欧美日韩不卡在线观看 | 久久精品久久久久久 | 精品国产精品国产偷麻豆 | 免费av手机在线观看 | 成人综合视频在线观看 | 久久久久一区二区三区四区 | 久久精品国产一区二区三区 | 黑人巨大精品 | 亚洲视频欧美视频 | 国产欧美一区二区三区在线播放 | 欧美精品一区二区三区视频 | 亚洲免费视频在线观看 | 欧美一级片a | 一区二区三区高清在线观看 | 精品国产精品 | 午夜影院中文字幕 | 国产日韩一区二区三区 |