TXT

Asp

By Kim Clark,2014-07-15 16:12
9 views 0
Asp

Asp.net(C#)给图片加上水印效果

002

    003 private void Btn_Upload_Click(object sender, System.EventArgs e)

004 {

    005 if(UploadFile.PostedFile.FileName.Trim()!="")

006 {

007 //上传文件

008 string extension =

    Path.GetExtension(UploadFile.PostedFile.FileName).ToUpper();

    009 string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();

010 string path = Server.MapPath(".") + "/UploadFile/" + fileName

    + extension;

    011 UploadFile.PostedFile.SaveAs(path);

012

013 //加文字水印?注意?这里的代码和以下加图片水印的代码不能共存

014 System.Drawing.Image image =

    System.Drawing.Image.FromFile(path);

    015 Graphics g = Graphics.FromImage(image);

    016 g.DrawImage(image, 0, 0, image.Width, image.Height);

    017 Font f = new Font("Verdana", 32);

    018 Brush b = new SolidBrush(Color.White);

    019 string addText = AddText.Value.Trim();

    020 g.DrawString(addText, f, b, 10, 10);

021 g.Dispose();

022

023 //加图片水印

024 System.Drawing.Image image =

    System.Drawing.Image.FromFile(path);

    025 System.Drawing.Image copyImage = System.Drawing.Image.FromFile( Server.MapPath(".") + "/Alex.gif");

    026 Graphics g = Graphics.FromImage(image);

027 g.DrawImage(copyImage, new

    Rectangle(image.Width-copyImage.Width, image.Height-copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);

028 g.Dispose();

029

030 //保存加水印过后的图片,删除原始图片

    031 string newPath = Server.MapPath(".") + "/UploadFile/" + fileName + "_new" + extension;

032 image.Save(newPath);

033 image.Dispose();

034 if(File.Exists(path))

035 {

036 File.Delete(path);

037 }

038

    039 Response.Redirect(newPath);

040 }

041 }

042

043

044 //生成缩略图#region //生成缩略图

045 /**//// <summary>

046 /// 生成缩略图

047 /// </summary>

    048 /// <param name="str_originalImagePath">源图路径;物理路径?</param>

    049 /// <param name="str_ThumbnailPath">缩略图路径;物理路径?</param>

050 /// <param name="i_width">缩略图宽度</param>

051 /// <param name="i_height">缩略图高度</param>

052 /// <param name="str_mode">生成缩略图的模式</param>

053 public static void MakeThumbnail(string str_originalImagePath,string

    str_ThumbnailPath,int i_width,int i_height,string str_mode)

054 {

    055 System.Drawing.Image img_OriginalImage = Image.FromFile(str_originalImagePath);

    056 int i_ToWidth = i_width;

    057 int i_ToHeight = i_height;

058

059 int x = 0;

060 int y = 0;

061

    062 int i_OriginalWidth = img_OriginalImage.Width;

    063 int i_OriginalHeight = img_OriginalImage.Height;

064

065 switch(str_mode)

066 {

067 case "HW": //按照指定的高度和宽度进行缩放;可能变形?

068 break;

069 case "W" : //指定宽度?高度按照比例缩放

070 i_ToHeight =

    img_OriginalImage.Height*i_width/img_OriginalImage.Width;

071 break;

072 case "H" : //指定高度?宽度按照比例缩放

073 i_ToWidth =

    img_OriginalImage.Width*i_Height/img_OriginalImage.Height;

074 break;

075 case "CUT" : //按照指定的高度和宽度剪裁;不变形?

076 if

    ((double)img_OriginalImage.Width/(double)img_OriginalImage.Height > (double)i_ToWidth/(double)i_ToHeight)

077 {

    078 i_OriginalHeight = img_OriginalImage.Height;

079 i_OriginalWidth =

    img_OriginalImage.Height*i_ToWidth/i_ToHeight;

080 y = 0;

081 x = (img_OriginalImage.Width-i_OriginalWidth)/2;

082 }

083 else

084 {

085 i_OriginalWidth = img_OriginalImage.Width;

    086 i_OriginalHeight = img_OriginalImage.Width*i_Height/i_ToWidth;

087 x = 0;

088 y = (img_OriginalImage.Height-i_OriginalHeight)/2;

089 }

090 break;

091 default:

092 break;

093 }

094

095 //新建一个BMP图片

096 Image img_BitMap = new

    System.Drawing.Bitmap(i_ToWidth,i_ToHeight);

097

098 //新建一个画板

    099 System.Drawing.Graphics gp = new System.Drawing.Graphics.FromImage(img_BitMap);

100

101 //设置高质量插值法

102 gp.InterpolationMode =

    System.Drawing.Drawing2D.InterpolationMode.High;

103

104 //设置高质量、低速度呈现平滑程度

105 gp.SmoothingMode =

    System.Drawing.Drawing2D.SmoothingMode.HighQuality;

106

107 //清空画布并以透明背景色填充

    108 gp.Clear(Color.Transparent);

109

110 //指定位置并按大小绘制原图片的指定部分

    111 gp.DrawImage(img_OriginalImage,new Rectangle(0,0,i_ToWidth,i_ToHeight),new

    Rectangle(x,y,i_OrignalWidth,i_OriginalHeight),GraphicsUnit.Pixel);

112

113 try

114 {

115 //JPG格式保存图片

116

    img_BitMap.Save(str_ThumbnailPath,System.Drawing.Imaging.ImageFormat.Jpeg);

117 }

118 catch(Exception e)

119 {

120 throw e;

121 }

122 finally

123 {

124 img_OriginalImage.Dispose();

    125 img_BitMap.Dispose();

    126 gp.Dispose();

127 }

128

129 }

130 #endregion

Report this document

For any questions or suggestions please email
cust-service@docsford.com