图像相关(三) Bitmap与byte[]、BitmapImage与byte[]互相转换、图像加载与保存

来源:互联网 发布:软件开发学校好吗 编辑:程序博客网 时间:2024/06/06 01:42
using System;using System.Drawing;using System.IO;using System.Windows.Media.Imaging;namespace WpfApplication1.com.utils{    ///     /// 图像相关工具    ///     public static class ImageTools    {        public static byte[] BitmapToBytes(Bitmap bitmap)        {            MemoryStream ms = new MemoryStream();            bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);            byte[] buffer = ms.ToArray();            ms.Close();            ms.Dispose();            return buffer;        }        public static Bitmap BytesToBitmap(byte[] buffer)        {            MemoryStream ms = new MemoryStream(buffer);            Bitmap bmp = new Bitmap(ms);            ms.Close();            ms.Dispose();            return bmp;        }        public static byte[] BitmapImageToBytes(BitmapImage bmp)        {            byte[] buffer = null;            try            {                Stream stream = bmp.StreamSource;                if (stream != null && stream.Length > 0)                {                    //很重要,因为Position经常位于Stream的末尾,导致下面读取到的长度为0。                       stream.Position = 0;                    using (BinaryReader br = new BinaryReader(stream))                    {                        buffer = br.ReadBytes((int)stream.Length);                    }                }            }            catch (Exception ex)            {                Console.WriteLine(ex.Message);            }            return buffer;        }        public static BitmapImage BytesToBitmapImage(byte[] buffer)        {            BitmapImage bmpImg = new BitmapImage();            bmpImg.BeginInit();            bmpImg.StreamSource = new MemoryStream(buffer);            bmpImg.EndInit();            return bmpImg;        }        //需要先把Bitmap转换为byte[],再把byte[]转换为BitmapImage        public static BitmapImage BitmapToBitmapImage(Bitmap bitmap)        {            byte[] buffer = BitmapToBytes(bitmap);            BitmapImage bmpImg = new BitmapImage();            bmpImg.BeginInit();            bmpImg.StreamSource = new MemoryStream(buffer);            bmpImg.EndInit();            return bmpImg;        }        //需要先把BitmapImage转换为byte[],再把byte[]转换为Bitmap        public static Bitmap BitmapImageToBitmap(BitmapImage bitmapImage)        {            byte[] buffer = BitmapImageToBytes(bitmapImage);            Bitmap bmp = BytesToBitmap(buffer);            return bmp;        }            }}using System;using System.Drawing;using System.IO;using System.Windows.Media;using System.Windows.Media.Imaging;namespace WpfApplication1.com.core{    ///     /// 图像管理    ///     public static class ImageManager    {        //加载图像--------------------        public static BitmapImage LoadByBitmapImage(string imgUrl,UriKind uriKind = UriKind.Absolute)        {            BitmapImage bmpImg = new BitmapImage();            bmpImg.BeginInit();            bmpImg.UriSource = new Uri(imgUrl, uriKind);            bmpImg.EndInit();            return bmpImg;        }        public static BitmapImage LoadByFileStream(string imgUrl)        {            FileStream fs = new FileStream(imgUrl,FileMode.Open);            byte[] buffer = new byte[fs.Length];            fs.Read(buffer, 0, buffer.Length);            fs.Close();            BitmapImage bmpImg = new BitmapImage();            bmpImg.BeginInit();            bmpImg.StreamSource = new MemoryStream(buffer);            bmpImg.EndInit();            return bmpImg;        }        public static Bitmap LoadByBitmap(string imgUrl)        {            Bitmap bmp = new Bitmap(imgUrl);            return bmp;        }                //保存图像---------------------        public static void SaveByBimtap(string imgUrl,Bitmap bitmap)        {            //Bitmap可以直接保存图像,与源图像大小一样            bitmap.Save(imgUrl);        }        public static void SaveByFile(string imgUrl,byte[] buffer)        {            //这种保存原始大小            File.WriteAllBytes(imgUrl, buffer);        }        public static void SaveByFileStream(string imgUrl,BitmapImage bmpImg)        {            //这种保存跟源图大小不一样,可能比源图还要大(跟编码有关)            //编码            BitmapEncoder encoder = new JpegBitmapEncoder();//            //BitmapEncoder encoder = new PngBitmapEncoder();            encoder.Frames.Add(BitmapFrame.Create(bmpImg));            FileStream fs = new FileStream(imgUrl, FileMode.Create);            encoder.Save(fs);            fs.Close();        }        //保存wpf组件为图片        //继承关系 FrameworkElemet-UIElement-Visual        public static void SaveVisual(string imgUrl,Visual visual,int width,int height)        {            RenderTargetBitmap rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Default);            rtb.Render(visual);            BitmapEncoder encoder = new JpegBitmapEncoder();            encoder.Frames.Add(BitmapFrame.Create(rtb));            FileStream fs = new FileStream(imgUrl,FileMode.Create);            encoder.Save(fs);            fs.Close();        }            }}
原创粉丝点击