[转].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[] ...
随机推荐
- Java for LeetCode 030 Substring with Concatenation of All Words【HARD】
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- BestCoder9 1003 Revenge of kNN(hdu 4995) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4995 题目意思:在一个一维坐标轴上,给出位置 xi 和值 vi,对于 M 次询问,每次询问给出inde ...
- python基础——递归函数
python基础——递归函数 递归函数 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数.举个例子,我们来计算阶乘n! = 1 x 2 x 3 x ... x n,用 ...
- 查看运行的KVM的虚机
[root@ok Desktop]# cat demo.py #!/usr/bin/python import libvirt conn = libvirt.open("qemu:///sy ...
- hibernate的sqlQuery自动封装
1.Query query = session.createSQLQuery("SQL").addEntity(Tree.class); //返回对象 List list = ...
- Ubuntu配置java环境变量
参考文章: http://www.cnblogs.com/BigIdiot/archive/2012/03/26/2417547.html 方法1:修改/etc/profile 文件所有用户的 she ...
- Web Service 其他服务器检测不到查询测试按钮
1.Web Service 其他服务器检测不到查询测试按钮,config需要修改地方: <system.web> <webServices> <protocols> ...
- Xamarin.Android开发实践(十三)
Xamarin.Android之SQLite.NET ORM 一.前言 通过<Xamarin.Android之SQLiteOpenHelper>和<Xamarin.Android之C ...
- hdu 4003 树形dp+分组背包 2011大连赛区网络赛C
题意:求K个机器人从同一点出发,遍历所有点所需的最小花费 链接:点我 Sample Input 3 1 1 //3个点,从1出发,1个机器人 1 2 1 1 3 1 3 1 2 1 2 1 1 3 1 ...
- hdu 4292 最大流 水题
很裸的一道最大流 格式懒得排了,注意把人拆成两份,一份连接食物,一份连接饮料 4 3 3 //4个人,3种食物,3种饮料 1 1 1 //食物每种分别为1 1 1 1 //饮料每种数目分别为1 YYN ...