[转].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(".") + 1).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 = 100;//设置缩略图初始宽度
int tHeight = 100;//设置缩略图初始宽度

//按比例计算出缩略图的宽度和高度
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(0, 0, tWidth, tHeight), new Rectangle(0, 0, 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 缩略图方法

时间: 2024-07-29 05:56:23

[转].net 缩略图方法的相关文章

iOS获取本地视频和网络URL视频的缩略图方法

iOS获取本地视频和网络URL视频的缩略图方法 字数222 阅读612 评论0 喜欢13 首先大家先添加AVFoundation和CoreMedia.framework两个框架 第一种本地视频获取缩略图 NSString *path = @"www.51ios.net/本地路径" MPMoviePlayerController *51iosMPMovie = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileU

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

打开 ../ include/ common.func.php添加代码 //将缩放图转变为文章第一张图片 function firstimg($str_pic) { $str_sub=substr($str_pic,0,-7).".jpg";//删除缩略图字符串最后七位,然后再补上后缀.jpg return $str_sub; } 调用的方法是: [field:litpic function='firstimg(@me)'/] DEDE列表页调用文章内容第一张图片(非缩略图)方法,布布

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

打开 ../ include/ common.func.php添加代码 //将缩放图转变为文章第一张图片 function firstimg($str_pic) { $str_sub=substr($str_pic,0,-7).".jpg";//删除缩略图字符串最后七位,然后再补上后缀.jpg return $str_sub; } //调用的方法是: [field:litpic function='firstimg(@me)'/] [dedecms]DEDE列表页调用文章内容第一张图片

C# 生成缩略图 方法

#region -生成缩略图- /// <summary> /// 生成缩略图 /// </summary> /// <param name="orginalImagePath">原图片对象</param> /// <param name="thumbnailPath">缩略图的路径</param> /// <param name="width">指定宽度<

生成图片缩略图方法

private void DrawImageRectRect(string rawImgPath, string newImgPath, int width, int height) { System.Drawing.Image imageFrom = System.Drawing.Image.FromFile(rawImgPath); // 源图宽度及高度 int imageFromWidth = imageFrom.Width; int imageFromHeight = imageFrom

一个图片缩略图方法

/* * 缩略图 * @param background 原图 * @param width 缩略图的宽度 * @param height 缩略图的高度 * @param newfile 新图片的名称 * @param object $geo_info 翻转角度 由函数geo_info 获得 主要针对苹果手机上传图片问题 */ function thumb($background, $width, $height, $newfile, $geo_info='') { if( isWap() &&a

Android--生成缩略图------方法总结

在Android中对大图片进行缩放真的很不尽如人意,不知道是不是我的方法不对.下面我列出3种对图片缩放的方法,并给出相应速度.请高人指教. 第一种是BitmapFactory和BitmapFactory.Options. 首先,BitmapFactory.Options有几个Fields很有用: inJustDecodeBounds:If set to true, the decoder will return null (no bitmap), but the out... 也就是说,当inJ

dede 添加 栏目缩略图

一. 涉及到文件如下(注意备份) dede/catalog_add.php dede/catalog_edit.php dede/templets/catalog_add.htm dede/templets/catalog_edit.htm 二.打开文件夹/templets目录(模板),在里面新建一个文件夹typeimg,用于独立存放栏目缩略图 三. 新加字段 typeimg 后台执行SQL(前缀默认为dede_ 具体前缀根据自己网站修改): alter table `dede_arctype`

C#生成缩略图

[csharp] view plain copy print? /// 生成缩略图 /// </summary> /// <param name="originalImagePath">源图路径</param> /// <param name="thumbnailPath">缩略图路径</param> /// <param name="width">缩略图宽度</par