C#.NET:图标转换为byte [],然后再返回 [英] C#.NET: Convert Icon to byte[] and back again

查看:86
本文介绍了C#.NET:图标转换为byte [],然后再返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个人如何在 System.Drawing.Icon 转换类型和字节[] ?我在找一些简单的可以(希望)工作.NET2。

How does one convert between System.Drawing.Icon type and byte[]? I'm looking for something simple that can (hopefully) work in .NET2.

推荐答案

您去通过的MemoryStream ,基本上是:

public static byte[] IconToBytes(Icon icon)
{
    using (MemoryStream ms = new MemoryStream())
    {
        icon.Save(ms);
        return ms.ToArray();
    }
}

public static Icon BytesToIcon(byte[] bytes)
{
    using (MemoryStream ms = new MemoryStream(bytes))
    {
        return new Icon(ms);
    }
}

(历史记:我不知道是否是安全处置传递到构造流的它的不是的安全,这样做是为了位图,例如......保存到数据流,并可以从中读出后,显然也没关系的图标不过,我想的MSDN制作得更为清晰。)

(Historical note: I wasn't sure whether or not it was safe to dispose of the stream passed to the constructor. It isn't safe to do so for Bitmap, for example... that holds on to the stream and may read from it later. Apparently it's okay for Icon though. I wish MSDN made this clearer...)

这篇关于C#.NET:图标转换为byte [],然后再返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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