• bitmapimage与byte[]相互转换 转


    作者:飘遥(Zhenxing Zhou) tags:WPF Image byte WPF BitmapImage 有时要实现bitmapimage与byte[]相互转换,这里实现两个静态方法,直接调用即可。

    byte[]转换为bitmapimage:
    public static bitmapimage bytearraytobitmapimage(byte[] bytearray)
    {
        bitmapimage bmp
    = null;

        
    try
        {
            bmp
    = new bitmapimage();
            bmp.begininit();
            bmp.streamsource
    = new memorystream(bytearray);
            bmp.endinit();
        }
        
    catch
        {
            bmp
    = null;
        }

        
    return bmp;
    }

    bitmapimage转换为byte[]:
    public static byte[] bitmapimagetobytearray(bitmapimage bmp)
    {
        
    byte[] bytearray = null;

        
    try
        {
            stream smarket
    = bmp.streamsource;

            
    if (smarket != null && smarket.length > 0)
            {
                
    //很重要,因为position经常位于stream的末尾,导致下面读取到的长度为0。
                smarket.position = 0;

                
    using (binaryreader br = new binaryreader(smarket))
                {
                    bytearray
    = br.readbytes((int)smarket.length);
                }
            }
        }
        
    catch
        {
            
    //other exception handling
        }

        
    return bytearray;
    }
  • 相关阅读:
    日期时间检查(格式为:YYYY-MM-DD HH:MM:SS)
    日期合法性验证(格式为:YYYY-MM-DD或YYYY/MM/DD )
    [转]Attribute在.net编程中的应用
    WCF
    [转]使用C#开发ActiveX控件
    在 IIS 中承载 WCF 服务
    [转]一个完整的Installshield安装程序实例
    反射
    特性
    迭代器
  • 原文地址:https://www.cnblogs.com/liye/p/1736101.html
Copyright © 2020-2023  润新知