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;
                }
            }
        }
    }

  • 相关阅读:
    jquery、js调用iframe父窗口与子窗口元素的方法整理
    js中的各种宽高以及位置总结
    javascript call与apply关键字的作用
    javascript之window对象
    CSS3 3D立方体效果
    CSS3 3D transform变换
    JS查找字符串中出现次数最多的字符
    写一个函数将传入的字符串转换成驼峰表示法
    数组去重方法
    内核升级得一个模范
  • 原文地址:https://www.cnblogs.com/Kingly/p/2215602.html
Copyright © 2011-2022 走看看