本文转自: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 缩略图方法的更多相关文章

  1. 【dedecms】DEDE列表页调用文章内容第一张图片(非缩略图)方法

    打开 ../ include/ common.func.php 添加代码 //将缩放图转变为文章第一张图片 function firstimg($str_pic) { $str_sub=substr( ...

  2. Proe4.0使用VB.net生成缩略图方法

    Private Sub btnRasterImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ...

  3. 织梦dedecms将指定图片自动生成指定尺寸的小图、缩略图、图片的方法

    对于普通企业网站来讲,织梦原来的程序只是提供了一个缩略图,但是这样对于一些相对来说图片会比较多的网站来说,图片太大当缩略图会导致网站整体的访问速度,所以我今天就来教你织梦把一张大图转换成生成一张小图或 ...

  4. C# webform上传图片并生成缩略图

    其实里面写的很乱,包括修改文件名什么的都没有仔细去写,主要是想记录下缩略图生成的几种方式 ,大家明白就好! void UpImgs() { if (FileUpload1.HasFile) { str ...

  5. android 视频的缩略图 缓存机制和 异步加载缩略图

    在这次的工作开发项目中,涉及到一个视频缩略图的视频列表:这个在大家看来,制作视频缩略图就是两行代码就搞定的事.确实是这样的,百度一下,每个帖子都知道制作视频缩略图的方法,在这里确实也是一样的,但是我要 ...

  6. dedecms内容页调用缩略图 缩略图多种用法(借鉴)

    给大家分享一下文章内容页调用缩略图的方法. 这种问题是:文章有缩略图,但是文章里面没有,想把缩略图添加到文章里面. 1.文章内容页调用缩略图方法如下两种.第一种没有大小设置.原图显示.第二种.可以设大 ...

  7. dedecms内容页调用缩略图 缩略图多种用法(借鉴)

    文章内容页调用缩略图方法如下两种.第一种没有大小设置.原图显示.第二种.可以设大小, (1) {dede:field.image/} (2)<img src="{dede:field. ...

  8. php远程图片抓取存放到本地路径并生成缩略图

    private function _getcontent($content)    {               $img_dir='../Public/Img/Ycimg'; //远程图片抓取存放 ...

  9. asp.net生成缩略图、文字图片水印

    /// <summary> /// 会产生graphics异常的PixelFormat /// </summary> private static PixelFormat[] ...

随机推荐

  1. 【wireshark】打开后显示There are no interfaces on which a capture can be done

    解决方式:用管理员方式打开wireshark即可

  2. JavaEE填空与判断

    Java EE软件工程师认证考试 试题库- 填空题和选择题   一.     填空题 1. HTML网页文件的标记是__html__,网页文件的主体标记是_body__,标记页面标题的标记是__tit ...

  3. [Android Pro] ant 编译android工程

    参考文章: http://blog.csdn.net/xyz_lmn/article/details/7268582?reload http://hubingforever.blog.163.com/ ...

  4. VS2010中“工具>选项中的VC++目录编辑功能已被否决”解决方法

    转自:http://blog.csdn.net/chaijunkun/article/details/6658923 这是VS2010的改变,不能够在“工具-选项”中看到“VC++目录”了. 但是呢, ...

  5. Human Gene Functions(poj 1080)

    题目大意是:给定两组DNA序列,要你求出它们的最大相似度 每个字母与其他字母或自身和空格对应都有一个打分,求在这两个字符串中插入空格,让这两个字符串的匹配分数最大 /* 思路是很好想的,设f[i][j ...

  6. ajax 中文乱码问题 主要是IE浏览器

    解决方案: 提交前采用encodeURI两次编码,注:一定是两次 举例: var taText = $("#txtName").val(); taText = encodeURI( ...

  7. hrbustoj 1545:基础数据结构——顺序表(2)(数据结构,顺序表的实现及基本操作,入门题)

    基础数据结构——顺序表(2) Time Limit: 1000 MS    Memory Limit: 10240 K Total Submit: 355(143 users) Total Accep ...

  8. 开源的DevOps开发工具箱

    DevOps是一组过程.方法与系统的统称,用于促进开发(应用程序/软件工程).技术运营和质量保障(QA)部门之间的沟通.协作与整合.在DevOps的整个流程中,使用一些开源工具可以促进开发与运维之间的 ...

  9. 惊鸿一瞥(Glimpse)——开发之时即可掌控ASP.NET应用的性能

    今天要推荐的东西不是一篇文章,而是我实际使用的武器之一--用于ASP.NET应用性能诊断的大杀器.我的武器库中的武器之前已经介绍过Hangfire了,接下来我会不断和大家分享我使用的一些函数库和工具. ...

  10. 硬盘工具diskpart

    1.cmd 2.diskpart 3.list disk           (查看硬盘) 4.select disk n (选择硬盘,n是数字) 5.list partition   (查看分区) ...