分享一个项目中在用的图片处理工具类(图片缩放,旋转,画布格式,字节,image,bitmap转换等)

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Threading;

namespace ConfigLab.Comp.Img.ImgUtils
{
    /// 
    /// 功能简介:图片转换类
    /// 创建时间:2016-9-10
    /// 创建人:pcw
    /// 博客:http://www.cnblogs.com/taohuadaozhu
    /// 
    public class ImgConvert
    {
        /// 
        /// 字节数组转换为bitmap
        /// 
        /// 
        /// 
        public static Bitmap BytesToBitmap(byte[] buffer)
        {
            Bitmap bitmapResult = null;
            if (buffer != null && buffer.Length > 0)
            {
                using (MemoryStream ms = new MemoryStream(buffer))
                {
                    try
                    {
                        bitmapResult = new Bitmap(ms);
                        return bitmapResult;
                    }
                    catch (Exception error)
                    {
                        return bitmapResult;
                    }
                    finally
                    {
                        ms.Close();
                        ms.Dispose();
                    }
                }
            }
            return null;
        }
        /// 
        /// 字节数组转换为Image对象
        /// 
        /// 
        /// 
        public static Image BytesToImage(byte[] buffer)
        {
            if (buffer != null && buffer.Length > 0)
            {
                using (MemoryStream ms = new MemoryStream(buffer))
                {
                    Image image = null;
                    try
                    {
                        image = Image.FromStream(ms);
                        return image;
                    }
                    catch (Exception error)
                    {
                        return image;
                    }
                    finally
                    {
                        ms.Close();
                        ms.Dispose();
                    }
                }
            }
            return null;

        }

        /// 
        /// 按照一定的比率进行放大或缩小
        /// 
        /// 缩略图的宽度百分比 如:需要百分之80,就填0.8 
        /// 
        ///顺时针旋转90度     RotateFlipType.Rotate90FlipNone
        ///逆时针旋转90度     RotateFlipType.Rotate270FlipNone
        ///水平翻转           RotateFlipType.Rotate180FlipY
        ///垂直翻转          RotateFlipType.Rotate180FlipX
        ///保持原样           RotateFlipType.RotateNoneFlipNone
        /// 
        /// 背景是否透明
        /// Bitmap对象
        public static Bitmap GetImage_Graphics(Image ResourceImage, double Percent, RotateFlipType rotateFlipType, bool IsTransparent)
        {
            Bitmap ResultBmp = null;
            try
            {
                if (ResourceImage != null)
                {
                    ResourceImage.RotateFlip(rotateFlipType);
                    int _newWidth = (int)Math.Round(ResourceImage.Width * Percent);
                    int _newHeight = (int)Math.Round(ResourceImage.Height * Percent);
                    ResultBmp = new System.Drawing.Bitmap(_newWidth, _newHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);//创建图片对象
                    Graphics g = null;
                    try
                    {
                        g = Graphics.FromImage(ResultBmp);//创建画板并加载空白图像
                        if (g != null)
                        {
                            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; //System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;//设置保真模式为高度保真
                            g.DrawImage(ResourceImage, new Rectangle(0, 0, _newWidth, _newHeight), 0, 0, ResourceImage.Width, ResourceImage.Height, GraphicsUnit.Pixel);//开始画图
                            if (IsTransparent)
                            {
                                ResultBmp.MakeTransparent(System.Drawing.Color.Transparent);
                            }
                        }
                    }
                    catch (Exception errr)
                    {
                        ConfigLab.Utils.SaveErrorLog(string.Format("GetImage_Graphics(1),异常={0}", errr.Message));
                        Thread.Sleep(100);
                    }
                    finally
                    {
                        if (g != null)
                        {
                            g.Dispose();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ConfigLab.Utils.SaveErrorLog(string.Format("GetImage_Graphics(2),异常={0}", ex.Message));
                Thread.Sleep(100);
                return null;
            }
            finally
            {
                if (ResourceImage != null)
                {
                    ResourceImage.Dispose();
                }
            }
            return ResultBmp;
        }
        /// 
        /// 图片等比缩放
        /// 
        /// 
        /// 
        /// 
        public static bool ChangeImgSize(string sImgFilePath, double Percent,string sNewImgFilePath)
        {
            Image img = null;
            Bitmap bp = null;
            bool bSuccess = false;
            try
            {
                if (File.Exists(sImgFilePath) == false)
                {
                    Utils.SaveErrorLog(string.Format("找不到待压缩的原文件{0}", sImgFilePath));
                    return false;
                }
                img = Image.FromFile(sImgFilePath);
                if (img != null)
                {
                    bp = GetImage_Graphics(img, Percent, RotateFlipType.RotateNoneFlipNone, true);
                    if (bp != null)
                    {
                        string sDirectory = FilePathUtils.getDirectory(sNewImgFilePath);
                        if (sDirectory.EndsWith("\\") == false)
                        {
                            sDirectory = string.Format("{0}\\", sDirectory);
                        }
                        bp.Save(sNewImgFilePath);
                        bSuccess=true;
                    }
                }
            }
            catch(Exception ex)
            {
                Utils.SaveErrorLog(string.Format("ChangeImgSize处理{0}的图片且保存到{1}的任务执行失败",sImgFilePath,sNewImgFilePath), ex);
            }
            finally
            {
                if (img != null)
                {
                    img.Dispose();
                }
                if (bp != null)
                {
                    bp.Dispose();
                }
            }
            return bSuccess;
        }
        /// 
        /// Resize图片
        /// 
        /// 原始Bitmap
        /// 新的宽度
        /// 新的高度
        /// 处理以后的Bitmap
        public static Bitmap ResizeBmp(Bitmap bmp, int newW, int newH)
        {
            try
            {
                Bitmap b = new Bitmap(newW, newH);
                Graphics g = Graphics.FromImage(b);
                g.SmoothingMode = SmoothingMode.HighSpeed;
                g.CompositingQuality = CompositingQuality.HighSpeed;
                g.InterpolationMode = InterpolationMode.Low;
                g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
                g.Dispose();

                return b;
            }
            catch
            {
                return null;
            }
        }

        /// 将图片Image转换成Byte[]
        /// 
        /// image对象
        /// 后缀名
        /// 
        public static byte[] ImageToBytes(Image Image, System.Drawing.Imaging.ImageFormat imageFormat)
        {
            if (Image == null) { return null; }
            byte[] data = null;
            using (MemoryStream ms = new MemoryStream())
            {
                using (Bitmap Bitmap = new Bitmap(Image))
                {
                    Bitmap.Save(ms, imageFormat);
                    ms.Position = 0;
                    data = new byte[ms.Length];
                    ms.Read(data, 0, Convert.ToInt32(ms.Length));
                    ms.Flush();
                }
            }
            return data;
        }

        /// 
        /// 通过FileStream 来打开文件,这样就可以实现不锁定Image文件,到时可以让多用户同时访问Image文件
        /// 
        /// 
        /// 
        public static Bitmap ReadImageFile(string path)
        {
            if (string.IsNullOrEmpty(path))
                return null;
            if (File.Exists(path) == false)
                return null;
            int filelength = 0;
            Bitmap bit = null;
            Byte[] image = null;
            try
            {
                using (FileStream fs = File.OpenRead(path))//OpenRead
                {
                    filelength = (int)fs.Length; //获得文件长度 
                    image = new Byte[filelength]; //建立一个字节数组 
                    fs.Read(image, 0, filelength); //按字节流读取 
                    System.Drawing.Image result = System.Drawing.Image.FromStream(fs);
                    bit = new Bitmap(result);
                    if (fs != null)
                    {
                        fs.Close();
                        fs.Dispose();
                    }
                }
            }
            catch (Exception err)
            {
                ConfigLab.Utils.SaveErrorLog(string.Format("读取图片【{0}】失败,调试信息={1}", path, err.Message + err.StackTrace));
            }
            finally
            {
                if (image != null)
                {
                    image = null;
                }
            }
            return bit;
        }
    }
}

 

你可能感兴趣的:(分享一个项目中在用的图片处理工具类(图片缩放,旋转,画布格式,字节,image,bitmap转换等))