资源预览内容
第1页 / 共6页
第2页 / 共6页
亲,该文档总共6页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
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 DateTime 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 image 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 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 newPath 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 ThumbnailPath 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 按照指定的高度和宽度进行缩放 可能变形 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 Height 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 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 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
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号