关于Icon.Save()一问?

javaduke 2002-02-10 12:49:08
我打算保存图标为文件
FileStream fs=File.Create("icon.bmp");
icon.Save(fs);
fs.Close();
以上代码编译可以通过。但是运行后无法生成
icon.bmp。并且没有任何提示,不知道为什么?
...全文
78 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
javaduke 2002-02-10
  • 打赏
  • 举报
回复
但是好像不能提取16*16的图标,只能提取大图标,另外生成的图像为何有锯齿。
javaduke 2002-02-10
  • 打赏
  • 举报
回复
问题找到,原来生成的文件在别的目录。
不过如何生成透明背景的图标
颜色调整转化成bitmap即可。
javaduke 2002-02-10
  • 打赏
  • 举报
回复
哎,看来大概是beta2的缘故了。bug!
CForce 2002-02-10
  • 打赏
  • 举报
回复
我也是xp啊,你怎么这么神奇?(vs.net正式版)
把上面Debug 改成 Release. 指明文件路径为"C:\\computer.bmp"试试?
或者再开一个新project,把代码粘上去.....
一定是你什么地方搞错了
javaduke 2002-02-10
  • 打赏
  • 举报
回复
haha,我是在xp下用的,生成的文件找不到,真是怪哉!搜索也找不到。但是没有任何错误抛出。
CForce 2002-02-10
  • 打赏
  • 举报
回复
不过图片颜色失真了,要改一下吧
CForce 2002-02-10
  • 打赏
  • 举报
回复
有趣的程序啊。你的程序没问题啊?我运行正常。默认是生成放在bin目录里。
你搜索一下computer.bmp吧。
cforce@963.net
多多指教!
javaduke 2002-02-10
  • 打赏
  • 举报
回复
CForce老兄,留个联系方法好吗?我的Email是dukejava@hotmail.com
javaduke 2002-02-10
  • 打赏
  • 举报
回复
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.IO;

namespace ExtractIconApplication
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private Icon[] icons;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
[DllImport("Shell32.dll")]
public static extern IntPtr ExtractIcon(
IntPtr hInst, // instance handle
string lpszExeFileName, // file name
int nIconIndex // icon index
);


public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.panel1 = new System.Windows.Forms.Panel();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(389, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// panel1
//
this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox1,
this.button1});
this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.panel1.Location = new System.Drawing.Point(0, 270);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(464, 24);
this.panel1.TabIndex = 2;
//
// button1
//
this.button1.Dock = System.Windows.Forms.DockStyle.Right;
this.button1.Location = new System.Drawing.Point(389, 0);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 24);
this.button1.TabIndex = 1;
this.button1.Text = "Browse";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Dock = System.Windows.Forms.DockStyle.Top;
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(464, 23);
this.button2.TabIndex = 3;
this.button2.Text = "Analysis";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(464, 294);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button2,
this.panel1});
this.Name = "Form1";
this.Text = "Form1";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog filedlg=new OpenFileDialog();
filedlg.Filter="Executable File(*.exe)|*.exe|Dynamic Link Liabrary(*.dll)|*.dll";
if(filedlg.ShowDialog()==DialogResult.OK){
textBox1.Text=filedlg.FileName;
}
}

private void button2_Click(object sender, System.EventArgs e)
{
int number=Int32.Parse(ExtractIcon(this.Handle,textBox1.Text,-1).ToString());
icons=new Icon[number];
for(int i=0;i<number;i++){
icons[i]=Icon.FromHandle(ExtractIcon(this.Handle,textBox1.Text,i));
}
textBox1.Text="Total number:"+number.ToString();
FileStream fs=new FileStream("computer.bmp",FileMode.Create);
icons[0].Save(fs);
fs.Close();
this.Invalidate();

}

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
if(icons==null) return;
int y=0;int x=0;
for(int i=0;i<icons.Length;i++){
e.Graphics.DrawIconUnstretched(icons[i],new Rectangle(Icon.Width*x,Icon.Height*y+40,icons[i].Width,icons[i].Height));
icons[i].Dispose();
x++;
if(i%20==0) {y++;x=0;}
}

}
}
}
干脆把源代码贴出来。欢迎大家指教。
javaduke 2002-02-10
  • 打赏
  • 举报
回复
我是用ExtractIcon Api提取的icon,想要保存为文件格式。图标可以正常显示,但是不知道为什么,总无法保存。
CForce 2002-02-10
  • 打赏
  • 举报
回复
我试过可以啊!
不知道你的ico哪里来,我能把它转为bmp.你是不是没注意生成的位置?FileMode?
估计你在写那个读ico的程序吧?^_^

FileStream fs=new FileStream("C:\\icon.bmp",FileMode.Create );
Icon icon=new Icon("C:\\test.ico");
icon.Save(fs);
fs.Close();

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧