当前位置:首页 > C# > 正文内容

C# 生成图片缩略图

admin7年前 (2017-03-24)C#4535
  1. using System.IO;  
    using System.Drawing;  
    using System.Drawing.Imaging;  
      
    /// <summary>     
    /// 图片处理类     
    /// 1、生成缩略图片或按照比例改变图片的大小和画质     
    /// 2、将生成的缩略图放到指定的目录下     
    /// </summary>     
    public class ImageClass  
    {  
        public Image ResourceImage;  
        private int ImageWidth;  
        private int ImageHeight;  
      
        public string ErrMessage;  
      
        /// <summary>     
        /// 类的构造函数     
        /// </summary>     
        /// <param name="ImageFileName">图片文件的全路径名称</param>     
        public ImageClass(string ImageFileName)  
        {  
            ResourceImage = Image.FromFile(ImageFileName);  
            ErrMessage = "";  
        }  
      
        public bool ThumbnailCallback()  
        {  
            return false;  
        }  
      
        /// <summary>     
        /// 生成缩略图重载方法1,返回缩略图的Image对象     
        /// </summary>     
        /// <param name="Width">缩略图的宽度</param>     
        /// <param name="Height">缩略图的高度</param>     
        /// <returns>缩略图的Image对象</returns>     
        public Image GetReducedImage(int Width, int Height)  
        {  
            try  
            {  
                Image ReducedImage;  
      
                Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);  
      
                ReducedImage = ResourceImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);  
      
                return ReducedImage;  
            }  
            catch (Exception e)  
            {  
                ErrMessage = e.Message;  
                return null;  
            }  
        }  
      
        /// <summary>     
        /// 生成缩略图重载方法2,将缩略图文件保存到指定的路径     
        /// </summary>     
        /// <param name="Width">缩略图的宽度</param>     
        /// <param name="Height">缩略图的高度</param>     
        /// <param name="targetFilePath">缩略图保存的全文件名,(带路径),参数格式:D:Images ilename.jpg</param>     
        /// <returns>成功返回true,否则返回false</returns>     
        public bool GetReducedImage(int Width, int Height, string targetFilePath)  
        {  
            try  
            {  
                Image ReducedImage;  
      
                Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);  
      
                ReducedImage = ResourceImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);  
                ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);  
      
                ReducedImage.Dispose();  
      
                return true;  
            }  
            catch (Exception e)  
            {  
                ErrMessage = e.Message;  
                return false;  
            }  
        }  
      
        /// <summary>     
        /// 生成缩略图重载方法3,返回缩略图的Image对象     
        /// </summary>     
        /// <param name="Percent">缩略图的宽度百分比 如:需要百分之80,就填0.8</param>       
        /// <returns>缩略图的Image对象</returns>     
        public Image GetReducedImage(double Percent)  
        {  
            try  
            {  
                Image ReducedImage;  
      
                Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);  
      
                ImageWidth = Convert.ToInt32(ResourceImage.Width * Percent);  
                ImageHeight = Convert.ToInt32(ResourceImage.Width * Percent);  
      
                ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);  
      
                return ReducedImage;  
            }  
            catch (Exception e)  
            {  
                ErrMessage = e.Message;  
                return null;  
            }  
        }  
      
        /// <summary>     
        /// 生成缩略图重载方法4,返回缩略图的Image对象     
        /// </summary>     
        /// <param name="Percent">缩略图的宽度百分比 如:需要百分之80,就填0.8</param>       
        /// <param name="targetFilePath">缩略图保存的全文件名,(带路径),参数格式:D:Images ilename.jpg</param>     
        /// <returns>成功返回true,否则返回false</returns>     
        public bool GetReducedImage(double Percent, string targetFilePath)  
        {  
            try  
            {  
                Image ReducedImage;  
      
                Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);  
      
                ImageWidth = Convert.ToInt32(ResourceImage.Width * Percent);  
                ImageHeight = Convert.ToInt32(ResourceImage.Width * Percent);  
      
                ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);  
      
                ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);  
      
                ReducedImage.Dispose();  
      
                return true;  
            }  
            catch (Exception e)  
            {  
                ErrMessage = e.Message;  
                return false;  
            }  
        }  
      
    }  

扫描二维码推送至手机访问。

版权声明:本文由视觉博客发布,如需转载请注明出处。

本文链接:http://feelsight.cn/post/32.html

“C# 生成图片缩略图” 的相关文章

C#获取机器码的方法详解(机器名,CPU编号,硬盘编号,网卡mac等)

这篇文章主要介绍了C#获取机器码的方法,结合实例形式详细分析了C#获取硬件机器名、CPU编号、硬盘编号、网卡mac等信息的相关实现方法,需要的朋友可以参考下本文实例讲述了C#获取机器码的方法。分享给大家供大家参考,具体如下:using System.Runtime.InteropServi...

C#实现邮件发送的功能

很多时候需要邮件发送功能,例如:监测应用,需要上报状态。   微软封装好的MailMessage类:主要处理发送邮件的内容(如:收发人地址、标题、主体、图片等等)   微软封装好的SmtpClient类:主要处理用smt...

C# 窗体间传值(Form与From之间互相传值)

C# 窗体间传值(Form与From之间互相传值)

1、委托   两个窗体,窗体很简单,只实现改变颜色功能,一看就会: 代码如下,只贴按钮事件代码: 打开Form2按钮事件 private void button1_Click(object s...

高恪路由器批量管理监控系统

高恪路由器批量管理监控系统

一、采用C# 编写,可以批量监控路由器是否正常 功能: 1.显示当前路由IP、名字、实时上行速度、实时下行速度、主机数量、连接数、CPU占用率、内存占用率、CPU温度、运行时间等 2.可以显示10分钟实时流量 3.可以显示历史2小时、...

c#程序闪退日志记录/异常日志

以下代码是程序入口文件 using DDS_Form1; using System; using System.Collections.Generic; using System.IO; //using System.Linq; using System.Windows.Forms;...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。