本文转自: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. Android Your content must have a ListView whose id attribute is 'android.R.id.list'错误的解决办法

    在Android开发中,ListView有着很重要的地位,使用的场合也非常的多 错误提示:Your content must have a ListView whose id attribute is ...

  2. java获取本机IP地址

    转载自:http://blog.csdn.net/thunder09/article/details/5360251 在网上找了几个用java获取本机IP地址的代码,发现都少都有些不完美,自己整理了一 ...

  3. NEFU 1146 又见A+B

    又见a+b Problem:1146 Time Limit:1000ms Memory Limit:65535K Description 给定两个非负整数A,B,求他们的和. Input 多组输入,每 ...

  4. Step deep into GLSL

    1 Lighting computation is handled in eye space(需要根据眼睛的位置来计算镜面发射值有多少进入眼睛), hence, when using GLSL (GP ...

  5. JDK 工具列表

    jar — 一个创建和管理 jar 文件的工具. java — Java 应用启动器.在这篇文章里,开发和部署都是用的这个启动器. javac — Java 编译器. javadoc — API 文档 ...

  6. Json简介与转换数据例子

    Json是什么,Json就是javascript对象或者数组格式的字符串,Http协议不能传递JavaScript对象,所以要转换为字符串进行传输.AJAX传递复杂数据如果自己进行格式定义的话会经历组 ...

  7. POSIX线程--同时执行

    //#define _REENTRANT//#define _POSIX_C_SOURCE#include <iostream>#include <string>#includ ...

  8. jquery easy ui 1.3.4 快速入门(1)

    什么是easyui jQuery EasyUI是一组基于jQuery的UI插件集合,而jQuery EasyUI的目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面.开发者不需要编写复杂 ...

  9. Xamarin.Android开发实践(六)

    Xamarin.Android通知详解 一.发送通知的机制 在日常的app应用中经常需要使用通知,因为服务.广播后台活动如果有事件需要通知用户,则需要通过通知栏显示,而在Xamarin.Android ...

  10. VBScript [ 译 ]

    VBScript ( Visual Basic Scripting Edition) 使用COM 来和宿主环境交互.从Microsoft Windows 98 开始的所有的微软桌面操作系统的relea ...