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 ...
随机推荐
- There is no getter for property named xxx' in 'class java.lang.xxx'
在xxxMapper.xml我们使用sql片段来提高sql代码的复用性,当时新手传入参数时常常出现这样的错误: There is no getter for property named xxx' i ...
- wpf 画刷的分类
System.Windows.Media.Brush最上一层画刷 System.Windows.Media.GradientBrush 线性画刷 ,下层主要有两种画刷 System.Windows. ...
- Python格式化字符串--format
format格式化字符串方法相较于老版%格式方法的优点: 1.不需要理会数据类型的问题,在%方法中'%s'只能替代字符串类型. 2.单个参数可以多次输出,参数顺序可以不相同. 3.填充方式十分灵活,对 ...
- SpringMVC 实现文件的上传与下载
一 配置SpringMVC ,并导入与文件上传下载有关的jar包(在此不再赘述) 二 新建 相应 jsp 和controller FileUpAndDown.jsp <%@ page lang ...
- (10.19)Java小作业
在java的学习过程中数组的版块也是十分重要的,包括一些教程也会在这个知识点花上更多的时间来讲解,足以证明 这个知识点的重要性,今天想和大家分享一道学习数组过程中不可避免的求最值题. 已知一个整形数组 ...
- 怎么用SQL语句备份和恢复数据库?
BACKUP DATABASE "mydb" TO DISK ='C:\mybak.db' with init RESTORE DATABASE "mydb" ...
- day2_python的数据类型,sys,os模块,编码解码,列表,字典
今天主要了解了python的数据类型,sys,os模块,编码解码,列表,字典 1.数据类型:int(python3没有长整型)文本总是Unicode,str表示二进制用byte类表示布尔型:True( ...
- 文本可视化[二]——《今生今世》人物关系可视化python实现
文本可视化[二]--<今生今世>人物关系可视化python实现 在文本可视化[一]--<今生今世>词云生成与小说分析一文中,我使用了jieba分词和wordcloud实现了,文 ...
- Windows下使用nginx搭建反向代理服务器
反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时 ...
- js正则验证特殊字符
js正则验证特殊字符 方案一 var regEn = /[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/im, regCn = /[·!#¥(--):: ...