您的位置:首页 > 编程语言 > ASP

ASP.NET上传图片并生成水印--缩略图

2006-09-25 10:00 811 查看
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;

namespace WebApplication2
{
/// <summary>
/// WebForm14 的摘要说明。
/// </summary>
public class WebForm14 : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
string filePath = System.Web.HttpContext.Current.Server.MapPath("2.jpg");
Response.Write(filePath);
//数据库路径,GDI处理的图片, 不属于Asp.net的东西一般都要应用物理路径
//WinForm中Application.startPath, 在asp.net中System.Web.HttpContext.Current.Request.ApplicationPath
//它们都可以应用webConfig的方式 System.Configuration.ConfigurationSettings.AppSettings["BbsUrl"];
//相对路径在web中应得很多,对于不属于内部对像中一般都要应用物理路径
MakeSLT(ApplicationPath +@"/2.jpg",ApplicationPath + @"/s2.jpg");
}

public string UploadImage(System.Web.HttpPostedFile postFile)
{
try
{
if(postFile.ContentLength==0)
{
throw(new Exception("NotExistedPostFile"));
}
else
if(postFile.ContentLength>Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["MaxImageSize"]))
{
throw(new Exception("TooLargePostFile"));
}
String strExt=System.IO.Path.GetExtension(postFile.FileName);
if(System.Configuration.ConfigurationSettings.AppSettings["ImageExtension"].IndexOf(";"+strExt+";")>-1)
{
throw(new Exception("ErrorImageExtension"));
}

String strFileName=@"/uploads/images/"+DateTime.Now.ToString("yyyyMMddHHmmssffff")+".jpg";
String strAbsoluteFileName=System.Web.HttpContext.Current.Server.MapPath(strFileName);
String Copyright=System.Configuration.ConfigurationSettings.AppSettings["CopyRight"];

System.Drawing.Image imgPhoto=System.Drawing.Image.FromStream(postFile.InputStream,true);
//取高和宽
int phWidth = imgPhoto.Width;
int phHeight =imgPhoto.Height;
//建新图,指定格式为每像素 24 位;红色、绿色和蓝色分量各使用 8 位。
Bitmap bmPhoto = new Bitmap(phWidth, phHeight,PixelFormat.Format24bppRgb);
//设置分辨率
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,imgPhoto.VerticalResolution);
//准备Graphics
Graphics grPhoto = Graphics.FromImage(bmPhoto);
try
{
//指定消除锯齿的呈现。
grPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
//拷贝原图到做图区
grPhoto.DrawImage(
imgPhoto,
new Rectangle(0, 0, phWidth, phHeight),
0,
0,
phWidth,
phHeight,
GraphicsUnit.Pixel);
//用指定Sizes绘制图片时,测量用指定的字串宽度
//取可能的最大宽度
int[] sizes = new int[]{64,48,32,16,8,6,4};
Font crFont = null;
SizeF crSize = new SizeF();
for (int i=0 ;i<7; i++)
{
crFont = new Font("Verdana", sizes[i],
FontStyle.Bold);
crSize = grPhoto.MeasureString(Copyright,
crFont);
if((ushort)crSize.Width < (ushort)phWidth)
break;
}
//指定做图点
int yPixlesFromBottom = (int)(phHeight *.05);
float yPosFromBottom = ((phHeight -
yPixlesFromBottom)-(crSize.Height/2));
float xCenterOfImg = (phWidth/2);
StringFormat StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Center;
//绘制copyright
SolidBrush semiTransBrush2 =
new SolidBrush(Color.FromArgb(100, 0, 0,0));
grPhoto.DrawString(Copyright,
crFont,
semiTransBrush2,
new PointF(xCenterOfImg+1,yPosFromBottom+1),
StrFormat);
SolidBrush semiTransBrush = new SolidBrush(
Color.FromArgb(100, 255, 255, 255));
grPhoto.DrawString(Copyright,
crFont,
semiTransBrush,
new PointF(xCenterOfImg,yPosFromBottom),
StrFormat);
bmPhoto.Save(strAbsoluteFileName,ImageFormat.Jpeg);
}
finally
{
grPhoto.Dispose();
bmPhoto.Dispose();
imgPhoto.Dispose();
}
return strFileName;
}
catch(Exception excep)
{
throw(excep);
}
}

private void MakeSLT(string oldImagePath,string newImagePath)
{
//oldImagePath -原图地址 newImagePath 生成缩略图地址
int width = 150;//缩略图的宽度
int height = 112;// 缩略图的高度
int level = 80; //缩略图的质量 1-100的范围

System.Drawing.Image oldimage = System.Drawing.Image.FromFile(oldImagePath);
//System.Drawing.Image thumbnailImage = oldimage.GetThumbnailImage(width, height,new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
System.Drawing.Image thumbnailImage = oldimage.GetThumbnailImage(width, height,null, IntPtr.Zero);

Bitmap bm=new Bitmap(thumbnailImage);

//处理JPG质量的函数
ImageCodecInfo[] codecs=ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici=null;
foreach(ImageCodecInfo codec in codecs)
{
if(codec.MimeType=="image/jpeg")
ici=codec;
}
EncoderParameters ep=new EncoderParameters();
ep.Param[0]=new EncoderParameter(Encoder.Quality,(long)level);

bm.Save(newImagePath,ici,ep);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐