教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp.net  >  正文 Asp.Net 上传图片并生成高清晰缩略图

Asp.Net 上传图片并生成高清晰缩略图

发布时间:2016-12-03   编辑:jiaochengji.com
教程集为您提供Asp.Net 上传图片并生成高清晰缩略图 等资源,欢迎您收藏本站,我们将为您提供最新的Asp.Net 上传图片并生成高清晰缩略图 资源
在asp.net中,上传图片功能或者是常用的,生成缩略图也是常用的。baidu或者google,c#的方法也是很多的,但是一用却发现缩略图不清晰啊,缩略图片太大之类的事情,下面是我在处理图片上的代码,效果不错,所以拿出来分享
不是很复杂,大概写一下。目的只在于实现,未仔细按照标准什么的来写。其中参考了网上已经存在的代码。
using System.Drawing;
页面,如图:
 

点击提交按钮:

<table style="background: #fb7" border="0" cellspacing="1" cellpadding="1" width="620" align="center"> <tbody> <tr> <td bgcolor="#ffe7ce" height="27" width="464"> 代码如下</td> <td style="cursor: pointer" bgcolor="#ffe7ce" width="109" align="center" onclick="doCopy('copy4444')">复制代码</td> </tr> <tr> <td style="padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px" id="copy4444" class="copyclass" bgcolor="#ffffff" valign="top" colspan="2">

httpPostedFile hpf = UploadImage.PostedFile;
//取得文件名(不含路径)
string Filename = Path.GetFileName(hpf.FileName);//原文修改
if (hpf.FileName.Length < 1)
{
Response.Write("请选择您要上传的图片文件");
return;
}
if (hpf.ContentType != "image/jpeg" && hpf.ContentType != "image/gif")//原文修改
{
Response.Write("只允许上传 GIF JPG类型的文件");
return;
}
else
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(DateTime.Now.Year.ToString());
sb.Append(DateTime.Now.Month.ToString());
sb.Append(DateTime.Now.Day.ToString());
sb.Append(DateTime.Now.Hour.ToString());
sb.Append(DateTime.Now.Minute.ToString());
sb.Append(DateTime.Now.Second.ToString());
if (Filename.ToLower().EndsWith("gif"))
{
sb.Append(".gif");
}
else if (Filename.ToLower().EndsWith("jpg"))
{
sb.Append(".jpg");
}
else if (Filename.ToLower().EndsWith("jpeg"))
{
sb.Append(".jpeg");
}
Filename = sb.ToString();
}

// 保存图片到服务器上
try
{
hpf.SaveAs(Server.MapPath("Album\") Filename);//自己修改!
}
catch (Exception ee)
{
Response.Write("上传图片失败,原因" ee.Message);
return;
}

// 生成缩略图
//原始图片名称
string originalFilename = hpf.FileName;
//生成的高质量图片名称
string strFile = Server.MapPath("Album\Small_") Filename;

//从文件取得图片对象
System.Drawing.Image image = System.Drawing.Image.FromStream(hpf.InputStream, true);

Double Width = Double.Parse(TextBox1.Text.Trim());
Double Height = Double.Parse(TextBox2.Text.Trim());
System.Double NewWidth, NewHeight;

if (image.Width > image.Height)
{
NewWidth = Width;
NewHeight = image.Height * (NewWidth / image.Width);
}
else
{
NewHeight = Height;
NewWidth = (NewHeight / image.Height) * image.Width;
}

if (NewWidth > Width)
{
NewWidth = Width;
}
if (NewHeight > Height)
{
NewHeight = Height;
}

System.Drawing.Size size = new Size((int)NewWidth, (int)NewHeight); // 图片大小
System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height); //新建bmp图片
System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap); //新建画板
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //设置高质量,低速度呈现平滑程度
graphics.Clear(Color.White); //清空画布
//在指定位置画图
graphics.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
System.Drawing.GraphicsUnit.Pixel);

//文字水印
System.Drawing.Graphics textGraphics = System.Drawing.Graphics.FromImage(bitmap);
System.Drawing.Font font = new Font("宋体", 10);
System.Drawing.Brush brush = new SolidBrush(Color.Black);
textGraphics.DrawString(TextBox3.Text.Trim(), font, brush, 10, 10);
textGraphics.Dispose();


///图片水印
//System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("pic/1.gif"));
//Graphics a = Graphics.FromImage(bitmap);
//a.DrawImage(copyImage, new Rectangle(bitmap.Width-copyImage.Width,bitmap.Height-copyImage.Height,copyImage.Width, copyImage.Height),0,0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);


//copyImage.Dispose();
//a.Dispose();
//copyImage.Dispose();


//保存缩略图
try
{
bitmap.Save(strFile, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
{
Response.Write("保存缩略图失败:" ex.Message);
}

graphics.Dispose();
image.Dispose();
bitmap.Dispose();

</td> </tr> </tbody> </table>
整个实现的过程如下面的图:
浏览页面,选择图片:
 
点击提交后,图片以及缩略图都已经生成到了目标文件夹里面:
 
可以看到,缩略图里面文字水印已经生成:
 
至此,整个功能已实现。

 

您可能感兴趣的文章:
Asp.Net 上传图片并生成高清晰缩略图
c#(asp.net)图片上传且生成高清缩略图的代码
php图片裁剪与缩略图示例
PHP原生写的生成图片缩略图类
php缩略图填充白边的示例代码
php绘图不显示图片怎么办
php生成缩略图自动填充白边例子
php图片文件上传类(可自动生成缩略图)
c#生成图片缩略图的实例与思路分享
php gd库实现服务端图片裁剪与缩略图

[关闭]
~ ~