NET给图片添加水印的方法代码 .pdf

上传人:zh****71 文档编号:133538598 上传时间:2020-05-28 格式:PDF 页数:6 大小:23.03KB
返回 下载 相关 举报
NET给图片添加水印的方法代码 .pdf_第1页
第1页 / 共6页
NET给图片添加水印的方法代码 .pdf_第2页
第2页 / 共6页
亲,该文档总共6页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《NET给图片添加水印的方法代码 .pdf》由会员分享,可在线阅读,更多相关《NET给图片添加水印的方法代码 .pdf(6页珍藏版)》请在金锄头文库上搜索。

1、 NET 给图片添加水印的方法代码 private void Btn Upload Click object sender System EventArgs e if UploadFile PostedFile FileName Trim 上传文件 string extension Path GetExtension UploadFile PostedFile FileName ToUpper string fileName DateTime Now Year ToString DateTime Now Month ToStrin g DateTime Now Day ToString Dat

2、eTime Now Hour ToString DateTime Now Min ute ToString DateTime Now Second ToString string path Server MapPath UploadFile fileName extension UploadFile PostedFile SaveAs path 加文字水印 注意 这里的代码和以下加图片水印的代码不能共存 System Drawing Image image System Drawing Image FromFile path Graphics g Graphics FromImage imag

3、e g DrawImage image 0 0 image Width image Height Font f new Font Verdana 32 Brush b new SolidBrush Color White string addText AddText Value Trim g DrawString addText f b 10 10 g Dispose 加图片水印 System Drawing Image image System Drawing Image FromFile path System Drawing Image copyImage System Drawing

4、Image FromFile Server Map Path Alex gif Graphics g Graphics FromImage image g DrawImage copyImage new Rectangle image Width copyImage Width image Hei ght copyImage Height copyImage Width copyImage Height 0 0 copyImage Width copyI mage Height GraphicsUnit Pixel g Dispose 保存加水印过后的图片 删除原始图片 string newP

5、ath Server MapPath UploadFile fileName new e xtension image Save newPath image Dispose if File Exists path File Delete path Response Redirect newPath 生成缩略图 region 生成缩略图 生成缩略图 源图路径 物理路径 缩略图路径 物理路径 缩略图宽度 缩略图高度 生成缩略图的模式 public static void MakeThumbnail string str originalImagePath string str ThumbnailP

6、ath int i width int i height string str mode System Drawing Image img OriginalImage Image FromFile str originalImagePath int i ToWidth i width int i ToHeight i height int x 0 int y 0 int i OriginalWidth img OriginalImage Width int i OriginalHeight img OriginalImage Height switch str mode case HW 按照指

7、定的高度和宽度进行缩放 可能变形 break case W 指定宽度 高度按照比例缩放 i ToHeight img OriginalImage Height i width img OriginalImage Width break case H 指定高度 宽度按照比例缩放 i ToWidth img OriginalImage Width i Height img OriginalImage Height break case CUT 按照指定的高度和宽度剪裁 不变形 if double img OriginalImage Width double img OriginalImage He

8、ight doubl e i ToWidth double i ToHeight i OriginalHeight img OriginalImage Height i OriginalWidth img OriginalImage Height i ToWidth i ToHeight y 0 x img OriginalImage Width i OriginalWidth 2 else i OriginalWidth img OriginalImage Width i OriginalHeight img OriginalImage Width i Height i ToWidth x

9、0 y img OriginalImage Height i OriginalHeight 2 break default break 新建一个BMP 图片 Image img BitMap new System Drawing Bitmap i ToWidth i ToHeight 新建一个画板 System Drawing Graphics gp new System Drawing Graphics FromImage img BitMa p 设置高质量插值法 gp InterpolationMode System Drawing Drawing2D InterpolationMode

10、High 设置高质量 低速度呈现平滑程度 gp SmoothingMode System Drawing Drawing2D SmoothingMode HighQuality 清空画布并以透明背景色填充 gp Clear Color Transparent 指定位置并按大小绘制原图片的指定部分 gp DrawImage img OriginalImage new Rectangle 0 0 i ToWidth i ToHeight new Rect angle x y i OrignalWidth i OriginalHeight GraphicsUnit Pixel try 以 JPG格式保存图片 img BitMap Save str ThumbnailPath System Drawing Imaging ImageFormat Jpeg catch Exception e throw e finally img OriginalImage Dispose img BitMap Dispose gp Dispose endregion

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 其它文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号