zoukankan      html  css  js  c++  java
  • windows phone开发中图片和二进制相互转换

    引用using System.Windows.Media.Imaging;

    using System;
    using System.Net;
    using System.IO;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    namespace Commons
    {
        /// <summary>
        
    /// 类型转换集合
        
    /// </summary>
        public class TypeConverter
        {
            /// <summary>
            
    /// 把图片转化成二进制
            
    /// </summary>
            
    /// <param name="sourceImage"></param>
            
    /// <returns></returns>
            public static byte[] ImageToByteArray(Image sourceImage)
            {
                BitmapSource bs = sourceImage.Source as BitmapSource;
                using (MemoryStream ms = new MemoryStream())
                {
                    System.Windows.Media.Imaging.WriteableBitmap writeableBitmap = new WriteableBitmap(bs);
                    System.Windows.Media.Imaging.Extensions.SaveJpeg(writeableBitmap, ms, bs.PixelWidth, bs.PixelHeight, 0100);
                    return ms.GetBuffer();
                }
            }

            /// <summary>
            
    /// 把二进制转化为Image图片
            
    /// </summary>
            
    /// <param name="bits"></param>
            
    /// <returns></returns>
            public static Image ByteArrayToImage(byte[] bits)
            {
                BitmapImage bitmapImage = new BitmapImage();
                using (MemoryStream ms = new MemoryStream(bits))
                {
                    bitmapImage.CreateOptions = BitmapCreateOptions.DelayCreation;
                    bitmapImage.SetSource(ms);
                    Image image = new Image();
                    image.Source = bitmapImage;
                    return image;
                }
            }
        }
    }

  • 相关阅读:
    Ubuntu命令行快捷启动Matlab
    用xmanager6启动Linux上的图形界面程序
    Winscp远程连接Linux主机,上传和下载文件
    Xshell6连接Ubuntu18.04
    Windows10通过VNC远程连接Ubuntu18.04
    获取Linux ip
    关联Anaconda和最新Pycharm2018.3.2
    asp.net mvc 外网获取不到port问题解决
    js 毫秒换算成秒
    c# 监听文件夹动作
  • 原文地址:https://www.cnblogs.com/Kingly/p/2215602.html
Copyright © 2011-2022 走看看