先看调用的方法:

using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;public partial class test_Default : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        generalImage();    }    private void generalImage()    {        string savePath = @"images/123.jpg";//原图路径        string smallPath = @"images/small/123.jpg";//要生成的小图        savePath = Server.MapPath(savePath);//这一步别忘了。        smallPath = Server.MapPath(smallPath);//同样这里也不要忘了。        YD.Common.ImageClass imageClass = new YD.Common.ImageClass();        imageClass.ShowThumbnail(savePath, smallPath, 200, 153);//这样调用    }}

生成小图的类:

using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Drawing;using System.Drawing.Imaging;using System.IO;/// <summary>/// ImageClass 的摘要说明/// </summary>namespace YD.Common{    public class ImageClass    {        public bool ThumbnailCallback()        {            return false;        }        public void ShowThumbnail(string oldfile, string newfile, int white, int height)        {            System.Drawing.Image image = System.Drawing.Image.FromFile(oldfile);            //获取原图高度和宽度            int oldh = image.Height;            int oldw = image.Width;            int neww, newh;            neww = white; newh = height;   //直接设定新图的高宽,,            try            {                System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);                System.Drawing.Image bt = new System.Drawing.Bitmap(neww, newh);                System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bt);                gr.Clear(Color.White);                gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;                gr.DrawImage(image, new Rectangle(0, 0, neww, newh), 0, 0, oldw, oldh, GraphicsUnit.Pixel);                switch (oldfile.Substring(oldfile.Length - 3).ToUpper())                {                    case "JPG":                        bt.Save(newfile, ImageFormat.Jpeg);                        break;                    case "GIF":                        bt.Save(newfile, ImageFormat.Gif);                        break;                    case "PNG":                        bt.Save(newfile, ImageFormat.Png);                        break;                    default:                        bt.Save(newfile, ImageFormat.Jpeg);                        break;                }                gr.Dispose();                bt.Dispose();                image.Dispose();            }            catch { }        }    }}

C#生成缩略图源码的更多相关文章

  1. Google Protocol Buffers 快速入门(带生成C#源码的方法)

    Google Protocol Buffers是google出品的一个协议生成工具,特点就是跨平台,效率高,速度快,对我们自己的程序定义和使用私有协议很有帮助. Protocol Buffers入门: ...

  2. 身份证号码查询与生成(C#源码)

    项目要用到这个功能,就写了一个,完整类也就二百来行,很简单.可以在项目中用,也可以作为学习. 源码下载 http://yunpan.cn/cmQCSWkhDnZLJ  访问密码 0227 核心代码如下 ...

  3. 2018-09-13 代码翻译尝试-使用Roaster解析和生成Java源码

    此文是前文使用现有在线翻译服务进行代码翻译的体验的编程语言方面第二点的一个尝试. 参考Which framework to generate source code ? - Cleancode and ...

  4. Hibernate 5.x 生成 SessionFactory 源码跟踪分析

    我们要使用 Hibernate 的功能,首先需要读取 Hibernate 的配置文件,根据配置启动 Hibernate ,然后创建 SessionFactory. 创建 SessionFactory ...

  5. iOS雪花动画、音频图、新闻界面框架、2048游戏、二维码条形码扫码生成等源码

    iOS精选源码 粒子雪花与烟花的动画 iOS 2048游戏 JHSoundWaveView - 简单地声波图.音波图 一个可快速集成的新闻详情界面框架,类似今日头条,腾讯新闻 二维码/条形码扫描及扫描 ...

  6. vue-cli随机生成port源码

    const portfinder = require('portfinder'): const port = await portfinder.getPortPromise(): 两行代码 端口搜索范 ...

  7. 1、Hibernate之生成SessionFactory源码追踪

    Hibernate的所有session都是由sessionFactory来生成的,那么,sessionFactory是怎么得来的呢?它与我们配置的xxx.cfg.xml文件以及xxx.hbm.xml文 ...

  8. 关于aspx 页面生成html 源码顶部空行不得不说的事儿

    原文引用自 http://www.360doc.com/content/12/0910/21/10504424_235418578.shtml 使用.aspx生成的页面一般都会有一个或多个空行,当然这 ...

  9. C#生成电子印章源码

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

随机推荐

  1. 自适应 Tab 宽度可以滑动文字逐渐变色的 TabLayout(仿今日头条顶部导航)

    TabLayout相信大家都用过,2015年Google大会上发布了新的Android Support Design库里面包含了很多新的控件,其中就包含TabLayout,它可以配合ViewPager ...

  2. Python实战之dict简单练习

    ['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__forma ...

  3. Echarts图表统计学习

    史上最全的Echarts图表学习文档 http://echarts.baidu.com/doc/doc.html 勤加练习,多做总结! http://www.stepday.com/topic/?79 ...

  4. Echarts数据可视化series-graph关系图,开发全解+完美注释

    全栈工程师开发手册 (作者:栾鹏) Echarts数据可视化开发代码注释全解 Echarts数据可视化开发参数配置全解 6大公共组件详解(点击进入): title详解. tooltip详解.toolb ...

  5. 构造函数,This关键字

    构造函数: 即构建创造对象时调用的函数.在new的时候自动执行,给对象进行初始化.创建对象都必须要通过构造函数初始化.(有参和无参) 一个类中如果没有定义过构造函数,那么类中会有一个默认的空参数构造函 ...

  6. Latex 去掉行号

    本文主要讲如何去掉Latex的行号 删除\modulolinenumbers删除所有\linenumbers 删除\usepackage{lineno,hyperref} modulolinenumb ...

  7. linux udev、mdev 介绍

    Udev介绍 Udev的下载网址:http://www.kernel.org/pub/linux/utils/kernel/hotplug/udev/ Udev分为三个子计划:namedev,libs ...

  8. 文本可视化[二]——《今生今世》人物关系可视化python实现

    文本可视化[二]--<今生今世>人物关系可视化python实现 在文本可视化[一]--<今生今世>词云生成与小说分析一文中,我使用了jieba分词和wordcloud实现了,文 ...

  9. oracle 表空间不足解决办法

    问题:在对某一表空间进行新建表的时候,出现ora-01658的错误. create 语句: create table OA_ORGCONFIG(  OAOC_UNID      INTEGER not ...

  10. Gridview 动态添加行

    /// <summary> /// 首次加载绑定 /// </summary> private void DataLoad()         { list.Add(new P ...