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

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();
        }
        
    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值