• 图片处理类


    /*****************************************************************
     * 创建人:sunwugang
     * 主要作用:图片处理类
     * 1、生成缩略图片或按照比例改变图片的大小和画质  
     * 2、将生成的缩略图放到指定的目录下  
     *  创建日期:2018-07-27
     *****************************************************************/
    namespace KK.TriageSys.Helper
    {
        using System;
        using System.Drawing;
        using System.Drawing.Imaging;
    
        public class ImageHelper
        {
            private static ImageHelper instance;
            public static ImageHelper Instance
            {
                get
                {
                    if (instance == null) instance = new ImageHelper();
                    return ImageHelper.instance;
                }
            }
    
            //图片宽
            private int ImageWidth;
            /// <summary>
            /// 图片高
            /// </summary>
            private int ImageHeight;
    
            /// <summary>   
            /// 根据文件路径获取图片对象   
            /// </summary>   
            /// <param name="ImageFileName">图片文件的全路径名称</param>   
            public Image GetImageByPath(string ImageFileName)
            {
                return Image.FromFile(ImageFileName);
            }
    
            public bool ThumbnailCallback()
            {
                return false;
            }
    
            /// <summary>   
            /// 生成缩略图重载方法1,返回缩略图的Image对象   
            /// </summary>   
            /// <param name="img">图片对象</param>   
            /// <param name="Width">缩略图的宽度</param>   
            /// <param name="Height">缩略图的高度</param>   
            /// <returns>缩略图的Image对象</returns>   
            public Image GetReducedImage(Image img,int Width, int Height)
            {
                try
                {
                    Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
                    return img.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);
                }
                catch (Exception ex)
                {
                    LogHelper.Default.SaveText("ImageHelper  GetReducedImage:" + ex.Message + ex.StackTrace);
                    return null;
                }
            }
    
            /// <summary>   
            /// 生成缩略图重载方法2,将缩略图文件保存到指定的路径   
            /// </summary>   
            /// <param name="img">图片对象</param>   
            /// <param name="Width">缩略图的宽度</param>   
            /// <param name="Height">缩略图的高度</param>   
            /// <param name="targetFilePath">缩略图保存的全文件名,(带路径),参数格式:D:Images ilename.jpg</param>   
            /// <returns>成功返回true,否则返回false</returns>   
            public bool GetReducedImage(Image img, int Width, int Height, string targetFilePath)
            {
                try
                {
                    Image ReducedImage;
                    Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
                    ReducedImage = img.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);
                    ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);
                    ReducedImage.Dispose();
                    return true;
                }
                catch (Exception ex)
                {
                    LogHelper.Default.SaveText("ImageHelper  GetReducedImage:" + ex.Message + ex.StackTrace);
                    return false;
                }
            }
    
            /// <summary>   
            /// 生成缩略图重载方法3,返回缩略图的Image对象   
            /// </summary>   
            /// <param name="img">图片对象</param>   
            /// <param name="Percent">缩略图的宽度百分比 如:需要百分之80,就填0.8</param>     
            /// <returns>缩略图的Image对象</returns>   
            public Image GetReducedImage(Image img, double Percent)
            {
                try
                {
                    Image ReducedImage;
                    Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
                    ImageWidth = Convert.ToInt32(img.Width * Percent);
                    ImageHeight = Convert.ToInt32(img.Width * Percent);
                    ReducedImage = img.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);
                    return ReducedImage;
                }
                catch (Exception ex)
                {
                    LogHelper.Default.SaveText("ImageHelper  GetReducedImage:" + ex.Message + ex.StackTrace);
                    return null;
                }
            }
    
            /// <summary>   
            /// 生成缩略图重载方法4,返回缩略图的Image对象   
            /// </summary>   
            /// <param name="img">图片对象</param>   
            /// <param name="Percent">缩略图的宽度百分比 如:需要百分之80,就填0.8</param>     
            /// <param name="targetFilePath">缩略图保存的全文件名,(带路径),参数格式:D:Images ilename.jpg</param>   
            /// <returns>成功返回true,否则返回false</returns>   
            public bool GetReducedImage(Image img, double Percent, string targetFilePath)
            {
                try
                {
                    Image ReducedImage;
                    Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
                    ImageWidth = Convert.ToInt32(img.Width * Percent);
                    ImageHeight = Convert.ToInt32(img.Width * Percent);
                    ReducedImage = img.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);
                    ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);
                    ReducedImage.Dispose();
                    return true;
                }
                catch (Exception ex)
                {
                    LogHelper.Default.SaveText("ImageHelper  GetReducedImage:" + ex.Message + ex.StackTrace);
                    return false;
                }
            }
    
        }
    }
    

      

  • 相关阅读:
    prometheus 文档
    go
    nginx
    python使用xlrd读取合并单元格
    python使用xlrd模块读取Excel
    创建Excel文件:ModuleNotFoundError: No module named 'openpyxl'
    openpyxl模块安装时报错: You are using pip version 19.2.3, however version 20.0.2 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.
    Python定义字典函数报错TypeError: takes 0 positional arguments but 1 was given
    Mysql--information_scherma(虚拟库)
    day07--MySQL索引
  • 原文地址:https://www.cnblogs.com/YYkun/p/9377938.html
Copyright © 2020-2023  润新知