silverlight添加图片 image和byte[] 的相互转换

2016-11-09 08:18:27来源:CSDN作者:qq_32493189人点击

1.添加图片并转换成byte[]数组

using System.IO;using System.Linq;using System.Windows.Media.Imaging;public void Browse_Click(object sender, RoutedEventArgs e)        {            OpenFileDialog dlg = new OpenFileDialog();            dlg.Filter = "图片|*.jpg;*.png;*.gif;*.bmp;*.jpeg";            Nullable<bool> result = dlg.ShowDialog();            if (result == true)            {                string filename = dlg.File.Name;                ImgText = filename;                using (FileStream textStream = dlg.File.OpenRead())                {                    //创建字节流数组                    byte[] bytes = new byte[textStream.Length];                    //读取相关数据                    textStream.Read(bytes, 0, Convert.ToInt32(textStream.Length));                    Img = bytes;                }            }        }

2.将byte[]数据转换成图片 在image控件中显示出来

   public void getImage(byte[] img)        {            Stream ms = new MemoryStream(img);            BitmapImage bitmap = new BitmapImage();            bitmap.SetSource(ms);            ImgSource = bitmap;        }

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台