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 ...
随机推荐
- Windows下MySQL5.6.21安装步骤
01.把 mysql-advanced-5.6.17-winx64.zip 解压到自定义 D:\mysql-5.6.17-W64 或 D:\mysql-advanced-5.6.17-winx64 目 ...
- 有趣的flash例子
仓鼠 <object type="application/x-shockwave-flash" data="http://cdn.abowman.com/widge ...
- 执行Sqlserver中waitfor delay延时操作或waitfor time定时操作
private static string connectionString = RBAC.Dal.DataRootBase.ConnectionString; private SqlConnecti ...
- 使用IGP和BGP的配合达到降低路由容量目的的实验与总结
概述 1.先说结论,通过eBGP协议,可以显著降低对非核心路由器的路由容量要求,因为核心路由器的数量明显少于非核心路由器,所以,通过此措施即联通网络,又降低设备要求,非常适宜大型网络. 2.因为网络规 ...
- 【爬虫入门01】我第一只由Reuests和BeautifulSoup4供养的Spider
[爬虫入门01]我第一只由Reuests和BeautifulSoup4供养的Spider 广东职业技术学院 欧浩源 1.引言 网络爬虫可以完成传统搜索引擎不能做的事情,利用爬虫程序在网络上取得数据 ...
- (10.11)Java第一小步
在度过大一和大二浑浑噩噩的咸鱼生活之后,我决定 开始为自己的未来负责,开始学习自己喜欢的Java,同时决定以这篇博客来开启自己的博客之旅和Jaca的学习之路. 以后我也会陆续在博客园更新自己的博客,记 ...
- Python学习笔记(十一)
Python学习笔记(十一): 生成器,迭代器回顾 模块 作业-计算器 1. 生成器,迭代器回顾 1. 列表生成式:[x for x in range(10)] 2. 生成器 (generator o ...
- ELK系列~nxlog实现多位置文件的收集
前几天我写了几篇关于ELK日志收集,存储和分析的文章: ELK系列~NLog.Targets.Fluentd到达如何通过tcp发到fluentd ELK系列~Nxlog日志收集加转发(解决log4日志 ...
- Mysql存在则更新,没有则新增
insert ignore 当插入数据时,如出现错误时,如重复数据,将不返回错误,只以警告形式返回. insert ignore into table(col1,col2) values ('val1 ...
- Establish the LAMP (Linux+Apache+MySQL+PHP) Development Environment on Ubuntu 14.04 Operating System
######################################################## Step One: Update the software package in yo ...