http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 13316|回复: 3

[GDI+] 生成缩略图的类文件SmallImage

[复制链接]
发表于 2012-8-20 15:20:54 | 显示全部楼层 |阅读模式
导读部分
-------------------------------------------------------------------------------------------------------------
C#基类|C#自定义类|C#帮助类--系列导航文章
http://www.sufeinet.com/thread-655-1-1.html

直接看代码吧,
大家可以直接复制使用
[C#] 纯文本查看 复制代码
/// <summary>
/// 类说明:SmallImage类,
/// 编码日期:2012-08-20
/// 编 码 人:  苏飞
/// 联系方式:361983679  Email:[url=mailto:sufei.1013@163.com]sufei.1013@163.com[/url]  Blogs:[url=http://www.sufeinet.com]http://www.sufeinet.com[/url]
/// 修改日期:2012-08-20
/// </summary>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SystemImage
{
    public class SmallImage
    {
        //产生缩略图,高是100像素,宽按比例缩小
        public static void GetSmallImage(string originalImagePath, string thumbnailPath, int width, int height)
        {
            System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
            if (originalImage.Width < originalImage.Height)
            {
                width = originalImage.Width * width / originalImage.Height;//按比例指定宽度,高度固定不变,是100个像素,整个图像按比例缩小            
            }
            else
            {
                height = originalImage.Height * height / originalImage.Width;
            }
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(width,height);            
            Graphics g = System.Drawing.Graphics.FromImage(bitmap);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;//.HighQualityBicubic;//设置高质量插值法            
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;//.HighQuality;//设置高质量,低速度呈现平滑程度           
            g.Clear(Color.Transparent); //清空画布并以透明背景色填充
            g.DrawImage(originalImage, new Rectangle(0, 0, width, height), new Rectangle(0, 0, originalImage.Width, originalImage.Height), GraphicsUnit.Pixel);
            bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
            originalImage.Dispose(); 
            bitmap.Dispose();
            g.Dispose();           
        }

        //检查大图像,如果宽超过四百,或高超过六百,就按比例缩放
        public static bool CheckBigAreaImg(string url,string newUrl)
        {
            bool result = false;
            using (System.Drawing.Image img = Bitmap.FromFile(url))
            {
                int height = img.Size.Height;//获得用户上传图片的宽度和高度
                int width = img.Size.Width;               
         
                if (height > 800 || width > 530)
                {
                    result = true;
                    GetSmallImage(url, newUrl, 500, 500);
                } 
            }
            if (result)
            {
                File.Delete(url);
            }
            return result;
        }
    }    
}



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2014-4-9 15:07:26 | 显示全部楼层
生成缩略图的类文件SmallImage
这段生成缩略图的代码不错,正是我需要的,可以做为参考。有代码有真相。谢谢!
发表于 2014-5-14 16:22:34 | 显示全部楼层
很不错,值得借鉴
发表于 2014-10-11 10:27:23 | 显示全部楼层
这个功能很实用,收藏
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-20 04:03

© 2014-2021

快速回复 返回顶部 返回列表