从资源文件中提取Icon对象到Image对象的小技巧。
直接从资源文件中提取的Icon对象,常常要转换成image对象才能使用,如过用直接用Icon.ToBitmap()方法,就会发生丢色现象。
通过下面的方法就不会了。
通过下面的方法就不会了。
private Image geticonimage(string name)
{
Icon icon = null;
Image image = null;
Stream stream = new MemoryStream();
try
{
object iconobj = GetImageResource(name);
if (iconobj is Icon)
{
icon = (Icon)iconobj;
icon.Save(stream);
image = Image.FromStream(stream);
}
else
{
image = (Image)iconobj;
}
}
catch
{
stream.Close();
//throw e;
image = null;
}
return image;
}
{
Icon icon = null;
Image image = null;
Stream stream = new MemoryStream();
try
{
object iconobj = GetImageResource(name);
if (iconobj is Icon)
{
icon = (Icon)iconobj;
icon.Save(stream);
image = Image.FromStream(stream);
}
else
{
image = (Image)iconobj;
}
}
catch
{
stream.Close();
//throw e;
image = null;
}
return image;
}
-----------------------------------
http://www.cnblogs.com/rock_chen/