现在的位置: 首页 > 综合 > 正文

从资源文件中提取Icon对象到Image对象的小技巧。

2011年06月12日 ⁄ 综合 ⁄ 共 836字 ⁄ 字号 评论关闭
直接从资源文件中提取的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;
            
        }

抱歉!评论已关闭.