[转].net 缩略图方法
本文转自:http://www.cnblogs.com/promic/archive/2010/04/21/1717190.html
private static string strConnect = System.Configuration.ConfigurationManager.AppSettings["connStr"];
//上传图像
protected void btnUpload_Click(object sender, EventArgs e)
{
……
string imgType;
string fullFileName = this.txtPath.PostedFile.FileName;
imgType = fullFileName.Substring(fullFileName.LastIndexOf(".") + ).ToLower();
string UserDirectory = Session["UserID"].ToString();//用户ID为所创建文件夹的名字
string UserPath = Server.MapPath("UpLoad").ToString() + "\\" + UserDirectory;
//如果文件夹不存在则创建
if (!Directory.Exists(UserPath))
{
Directory.CreateDirectory(UserPath);
}
string originalImagePath = UserPath + "\\" + "Original";
if(!Directory.Exists(originalImagePath))
{
Directory.CreateDirectory(originalImagePath);
}
string thumbnailPath = UserPath + "\\" + "Thumbnail";
if (!Directory.Exists(thumbnailPath))
{
Directory.CreateDirectory(thumbnailPath);
}
// 获取上传图像的文件名
string fileName = txtName.Text;
……
if (imgType=="jpg"||imgType=="jpeg"||imgType=="bmp"||imgType=="png"||imgType=="icon")
{
try
{
//生成原图
byte[] oFileByte = new byte[this.txtPath.PostedFile.ContentLength];
Stream oStream = this.txtPath.PostedFile.InputStream;
System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);
int oWidth = oImage.Width;//原图宽度
int oHeight = oImage.Height;//原图高度
int tWidth = ;//设置缩略图初始宽度
int tHeight = ;//设置缩略图初始宽度 //按比例计算出缩略图的宽度和高度
if (oWidth >= oHeight)
{
tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oHeight)));
}
else
{
tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));
} //生成缩略图
Bitmap tImage = new Bitmap(tWidth,tHeight);
Graphics g = Graphics.FromImage(tImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;//设置高质量插值法
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度
g.Clear(Color.Transparent);//清空画布并以透明背景色填充
g.DrawImage(oImage, new Rectangle(, , tWidth, tHeight), new Rectangle(, , oWidth, oHeight), GraphicsUnit.Pixel); //this.txtPath.PostedFile.SaveAs(originalImagePath + "\\" + fileName + "." + imgType);
//string originalImageUrl = "UpLoad/" + UserDirectory + "/" + "Orginal/"+fileName + ".jpg" ;//得到服务端原图片的虚拟路径
// string thumbnailImageUrl = "UpLoad/" + UserDirectory + "/" + "Thumbnail/" + fileName + ".jpg";//得到服务端原图片的虚拟路径
//以JPG格式保存图像
//oImage.Save(originalImageUrl, System.Drawing.Imaging.ImageFormat.Jpeg);/*看看这里这两个保存方式有没有问题*/
// tImage.Save(thumbnailImageUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
string oPath = originalImagePath + "\\" + fileName + ".jpg";
string tPath = thumbnailPath + "\\" + fileName + ".jpg";
//以JPG格式保存图像
oImage.Save(oPath, System.Drawing.Imaging.ImageFormat.Jpeg);
tImage.Save(tPath, System.Drawing.Imaging.ImageFormat.Jpeg); lblMessage.Visible = true;
lblMessage.Text = "图像上传成功!";
txtName.Text = "";
txtDescription.Text = "";
//释放资源
oImage.Dispose();
g.Dispose();
tImage.Dispose();
}
catch (Exception ex)
{
lblMessage.Visible = true;
lblMessage.Text = "由于网络原因,上载文件错误 " + ex.Message;
}
}
else
{
Response.Write("<script language='javascript'>alert('你选择的图像格式错误!');</script>");
lblMessage.Visible = false;
}
}
[转].net 缩略图方法的更多相关文章
- 【dedecms】DEDE列表页调用文章内容第一张图片(非缩略图)方法
打开 ../ include/ common.func.php 添加代码 //将缩放图转变为文章第一张图片 function firstimg($str_pic) { $str_sub=substr( ...
- Proe4.0使用VB.net生成缩略图方法
Private Sub btnRasterImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ...
- 织梦dedecms将指定图片自动生成指定尺寸的小图、缩略图、图片的方法
对于普通企业网站来讲,织梦原来的程序只是提供了一个缩略图,但是这样对于一些相对来说图片会比较多的网站来说,图片太大当缩略图会导致网站整体的访问速度,所以我今天就来教你织梦把一张大图转换成生成一张小图或 ...
- C# webform上传图片并生成缩略图
其实里面写的很乱,包括修改文件名什么的都没有仔细去写,主要是想记录下缩略图生成的几种方式 ,大家明白就好! void UpImgs() { if (FileUpload1.HasFile) { str ...
- android 视频的缩略图 缓存机制和 异步加载缩略图
在这次的工作开发项目中,涉及到一个视频缩略图的视频列表:这个在大家看来,制作视频缩略图就是两行代码就搞定的事.确实是这样的,百度一下,每个帖子都知道制作视频缩略图的方法,在这里确实也是一样的,但是我要 ...
- dedecms内容页调用缩略图 缩略图多种用法(借鉴)
给大家分享一下文章内容页调用缩略图的方法. 这种问题是:文章有缩略图,但是文章里面没有,想把缩略图添加到文章里面. 1.文章内容页调用缩略图方法如下两种.第一种没有大小设置.原图显示.第二种.可以设大 ...
- dedecms内容页调用缩略图 缩略图多种用法(借鉴)
文章内容页调用缩略图方法如下两种.第一种没有大小设置.原图显示.第二种.可以设大小, (1) {dede:field.image/} (2)<img src="{dede:field. ...
- php远程图片抓取存放到本地路径并生成缩略图
private function _getcontent($content) { $img_dir='../Public/Img/Ycimg'; //远程图片抓取存放 ...
- asp.net生成缩略图、文字图片水印
/// <summary> /// 会产生graphics异常的PixelFormat /// </summary> private static PixelFormat[] ...
随机推荐
- 项目总结(二)--- CocoaPods的简单介绍与使用
先谈下,我们为什么要使用CocoaPods,它能为我们做什么吧. 所有开发过iOS应用的小伙伴们都知道,在整个项目中,第三方库的引用必不可少(特别感谢那些优秀第三方开源库的作者,给我们提供了多大的便捷 ...
- 有时间测试dism
dism /capture-image /imagefile:d\win.win /capturedir:c:\ /name:win81 dism /export-image /winboot /so ...
- POSIX线程
大多数线程函数以pthread_开头,.h为pthread.h, 用-lpthread来链接线程库. 编写多线程时,定义宏_REENTRANT告诉编译器需要可重入,此宏必须位于任何#include ...
- 禅道bug安装报错
[root@lnmp src]# grep "session.save" /etc/php.ini ; http://php.net/session.save-handler se ...
- ps -C
[root@Nginx_BackUP keepalived]# ps -C nginx PID TTY TIME CMD 3965 ? 00:00:00 nginx 3966 ? 00:00:00 n ...
- SQLite常用网址
英文版SQLite官网: http://www.sqlite.org/rescode.html中文版SQLite官网:http://www.helplib.net/s/sqlite/9/167.sht ...
- 【Tyvj】1473校门外的树3 线段树/树状数组 <区间修改+单点访问>
描述 校门外有很多树,有苹果树,香蕉树,有会扔石头的,有可以吃掉补充体力的……如今学校决定在某个时刻在某一段种上一种树,保证任一时刻不会出现两段相同种类的树,现有两个操作:K=1,读入l,r表示在l ...
- Spark metrics on wordcount example
I read the section Metrics on spark website. I wish to try it on the wordcount example, I can't make ...
- C#之MemberwiseClone与Clone
MemberwiseClone 方法创建一个浅表副本,具体来说就是创建一个新对象,然后将当前对象的非静态字段复制到该新对象.如果字段是值类型的,则对该字段执行逐位复制.如果字段是引用类型,则复制引用但 ...
- js:数据结构笔记12--排序算法(2)
高级排序算法:(处理大数据:百万以上) 希尔排序:是插入排序的优化版: 首先设置间隔数组,然后按照每个间隔,分别进行排序: 如第一个间隔为5,首先a[5]与a[0]进行插入排序;然后a[6]和a[0] ...