Пример #1
0
 /// <summary>
 /// 将字节流转换为BitmapImage
 /// </summary>
 /// <param name="data">字节数组</param>
 /// <returns>BitmapImage</returns>
 public static BitmapImage Format(byte[] data)
 {
     if (!MatchUtils.IsEmpty(data))
     {
         BitmapImage ibmp = null;
         try
         {
             using (MemoryStream stream = new MemoryStream(data))
             {
                 (ibmp = new BitmapImage()).BeginInit();
                 {
                     ibmp.CacheOption = BitmapCacheOption.OnLoad;
                     {
                         ibmp.StreamSource = stream;
                     }
                 }
                 ibmp.EndInit();
             }
         }
         catch
         {
             ibmp = null;
         }
         finally
         {
             if (ibmp != null && ibmp.CanFreeze)
             {
                 ibmp.Freeze();
             }
         }
         return(ibmp);
     }
     return(null);
 }
Пример #2
0
 /// <summary>
 /// 将BitmapImage转换为字节流
 /// </summary>
 /// <param name="ibmp">BitmapImage</param>
 /// <returns>字节流</returns>
 public static byte[] Format(BitmapImage ibmp)
 {
     byte[] data = null;
     {
         if (!MatchUtils.IsEmpty(ibmp))
         {
             using (Stream stream = ibmp.StreamSource)
             {
                 if (stream != null && stream.Length > 0)
                 {
                     stream.Position = 0;
                     try
                     {
                         using (BinaryReader reader = new BinaryReader(stream))
                         {
                             data = reader.ReadBytes((int)stream.Length);
                         }
                     }
                     catch
                     {
                         data = null;
                     }
                 }
             }
         }
     }
     return(data);
 }