C#生成缩略图源码
先看调用的方法:
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#生成缩略图源码的更多相关文章
- Google Protocol Buffers 快速入门(带生成C#源码的方法)
Google Protocol Buffers是google出品的一个协议生成工具,特点就是跨平台,效率高,速度快,对我们自己的程序定义和使用私有协议很有帮助. Protocol Buffers入门: ...
- 身份证号码查询与生成(C#源码)
项目要用到这个功能,就写了一个,完整类也就二百来行,很简单.可以在项目中用,也可以作为学习. 源码下载 http://yunpan.cn/cmQCSWkhDnZLJ 访问密码 0227 核心代码如下 ...
- 2018-09-13 代码翻译尝试-使用Roaster解析和生成Java源码
此文是前文使用现有在线翻译服务进行代码翻译的体验的编程语言方面第二点的一个尝试. 参考Which framework to generate source code ? - Cleancode and ...
- Hibernate 5.x 生成 SessionFactory 源码跟踪分析
我们要使用 Hibernate 的功能,首先需要读取 Hibernate 的配置文件,根据配置启动 Hibernate ,然后创建 SessionFactory. 创建 SessionFactory ...
- iOS雪花动画、音频图、新闻界面框架、2048游戏、二维码条形码扫码生成等源码
iOS精选源码 粒子雪花与烟花的动画 iOS 2048游戏 JHSoundWaveView - 简单地声波图.音波图 一个可快速集成的新闻详情界面框架,类似今日头条,腾讯新闻 二维码/条形码扫描及扫描 ...
- vue-cli随机生成port源码
const portfinder = require('portfinder'): const port = await portfinder.getPortPromise(): 两行代码 端口搜索范 ...
- 1、Hibernate之生成SessionFactory源码追踪
Hibernate的所有session都是由sessionFactory来生成的,那么,sessionFactory是怎么得来的呢?它与我们配置的xxx.cfg.xml文件以及xxx.hbm.xml文 ...
- 关于aspx 页面生成html 源码顶部空行不得不说的事儿
原文引用自 http://www.360doc.com/content/12/0910/21/10504424_235418578.shtml 使用.aspx生成的页面一般都会有一个或多个空行,当然这 ...
- C#生成电子印章源码
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
随机推荐
- 将图标LOGO之类的图形图像转换成字体调用方法大全
借鉴百度对此标题的评价: 使用字体图标的优势 字体图标除了图像清晰度之外,比位图还有哪些优势呢. 适用性:一个图标字体比一系列的图像(特别是在Retina屏中使用双倍大小的图像)要小.一旦图标字体加载 ...
- memcached讲解
Memcached 最近空闲的是时候研究了一下缓存,根据公司环境对缓存进行了系统的了解,我们使用memcacheed进行缓存,下面结合我的 理解,以及网上的相关资料,memecached进行讲解. m ...
- 设置MySQL最大连接数
<pre name="code" class="sql">在使用MySQL数据库的时候,经常会遇到这么一个问题,就是"Can not co ...
- win2003服务器定时自动重启命令
1. win2003可以这样自动重启: 新建一个命令行文件比如reboot.bat 内容如下:shutdown -r -t 30 在计划任务中新建一个任务,程序选择上面这个reboot.cmd文件,时 ...
- WPF DataGrid显格式
Guide to WPF DataGrid formatting using bindings Peter Huber SG, 25 Nov 2013 CPOL 4.83 (13 votes) ...
- 对象反序列化时,抛出java.io.StreamCorruptedException: invalid type code: AC异常
问题描述:在使用java.io.ObjectInputStream类的readObject()方法去读取包含有序列化了多个(两个及两个以上)类的文件时,当读取到第二个类时,会抛出题目中提到的异常. 原 ...
- Extjs6(四)——侧边栏导航根据路由跳转页面
本文基于ext-6.0.0 之前做的时候这个侧边栏导航是通过tab切换来切换页面的,但是总感觉不太对劲,现在终于发现怎么通过路由跳转了,分享给大家,可能有些不完善的地方,望大家读后可以给些指点.欢迎留 ...
- Quartz格式设置说明
这些星号由左到右按顺序代表 : * * * * * * * 格式 ...
- Mapper 动态代理方式
Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同上边Dao接口实现类方法. Mapper接口开发 ...
- Python3爬虫登录模拟
使用Python爬虫登录系统之后,能够实现的操作就多了很多,下面大致介绍下如何使用Python模拟登录. 我们都知道,在前端的加密验证,只要把将加密环境还原出来,便能够很轻易地登录. 首先分析登录的步 ...