代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
C#生成缩略图,加文字或图片水印
作者:
Dezai.CN
/ 发布于
2011/8/22
/
885
<div>**/ /// <summary> /// 生成缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode,string type) { System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath); <font size="+0">int towidth = width; int toheight = height;</font> <font size="+0">int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height;</font> <font size="+0">switch (mode) { case "HW"://指定高宽缩放(可能变形) break; case "W"://指定宽,高按比例 toheight = originalImage.Height * width / originalImage.Width; break; case "H"://指定高,宽按比例 towidth = originalImage.Width * height / originalImage.Height; break; case "Cut"://指定高宽裁减(不变形) if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = (originalImage.Width - ow) / 2; } else { ow = originalImage.Width; oh = originalImage.Width * height / towidth; x = 0; y = (originalImage.Height - oh) / 2; } break; case "DB"://等比缩放(不变形,如果高大按高,宽大按宽缩放) if ((double)originalImage.Width / (double)towidth < (double)originalImage.Height / (double)toheight) { toheight = height; towidth = originalImage.Width * height / originalImage.Height; } else { towidth = width; toheight = originalImage.Height * width / originalImage.Width; } break; default: break; }</font> <font size="+0">//新建一个bmp图片 System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);</font> <font size="+0">//新建一个画板 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);</font> <font size="+0">//设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;</font> <font size="+0">//设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;</font> <font size="+0">//清空画布并以透明背景色填充 g.Clear(System.Drawing.Color.Transparent);</font> <font size="+0">//在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);</font> <font size="+0">try { //保存缩略图 if (type=="JPG") { bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); } if (type=="BMP") { bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Bmp); } if (type=="GIF") { bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Gif); } if (type=="PNG") { bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Png); } } catch (System.Exception e) { throw e; } finally { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); } }</font> <font size="+0">/**/ /// <summary> /// 在图片上增加文字水印 /// </summary> /// <param name="Path">原服务器图片路径</param> /// <param name="Path_sy">生成的带文字水印的图片路径</param> protected void AddShuiYinWord(string Path, string Path_sy) { string addText = "测试水印"; System.Drawing.Image image = System.Drawing.Image.FromFile(Path); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image); g.DrawImage(image, 0, 0, image.Width, image.Height); System.Drawing.Font f = new System.Drawing.Font("Verdana", 16); System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Blue);</font> <font size="+0">g.DrawString(addText, f, b, 15, 15); g.Dispose();</font> <font size="+0">image.Save(Path_sy); image.Dispose(); }</font> <font size="+0">/**/ /// <summary> /// 在图片上生成图片水印 /// </summary> /// <param name="Path">原服务器图片路径</param> /// <param name="Path_syp">生成的带图片水印的图片路径</param> /// <param name="Path_sypf">水印图片路径</param> protected void AddShuiYinPic(string Path, string Path_syp, string Path_sypf) { System.Drawing.Image image = System.Drawing.Image.FromFile(Path); System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image); g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel); g.Dispose();</font> <font size="+0">image.Save(Path_syp); image.Dispose(); } </font> <font size="+0">/**/ /// <summary> /// 生成缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode,string type) { System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);</font> <font size="+0">int towidth = width; int toheight = height;</font> <font size="+0">int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height;</font> <font size="+0">switch (mode) { case "HW"://指定高宽缩放(可能变形) break; case "W"://指定宽,高按比例 toheight = originalImage.Height * width / originalImage.Width; break; case "H"://指定高,宽按比例 towidth = originalImage.Width * height / originalImage.Height; break; case "Cut"://指定高宽裁减(不变形) if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = (originalImage.Width - ow) / 2; } else { ow = originalImage.Width; oh = originalImage.Width * height / towidth; x = 0; y = (originalImage.Height - oh) / 2; } break; case "DB"://等比缩放(不变形,如果高大按高,宽大按宽缩放) if ((double)originalImage.Width / (double)towidth < (double)originalImage.Height / (double)toheight) { toheight = height; towidth = originalImage.Width * height / originalImage.Height; } else { towidth = width; toheight = originalImage.Height * width / originalImage.Width; } break; default: break; }</font> <font size="+0">//新建一个bmp图片 System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);</font> <font size="+0">//新建一个画板 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);</font> <font size="+0">//设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;</font> <font size="+0">//设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;</font> <font size="+0">//清空画布并以透明背景色填充 g.Clear(System.Drawing.Color.Transparent);</font> <font size="+0">//在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);</font> <font size="+0">try { //保存缩略图 if (type=="JPG") { bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); } if (type=="BMP") { bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Bmp); } if (type=="GIF") { bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Gif); } if (type=="PNG") { bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Png); } } catch (System.Exception e) { throw e; } finally { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); } }</font> <font size="+0">/**/ /// <summary> /// 在图片上增加文字水印 /// </summary> /// <param name="Path">原服务器图片路径</param> /// <param name="Path_sy">生成的带文字水印的图片路径</param> protected void AddShuiYinWord(string Path, string Path_sy) { string addText = "测试水印"; System.Drawing.Image image = System.Drawing.Image.FromFile(Path); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image); g.DrawImage(image, 0, 0, image.Width, image.Height); System.Drawing.Font f = new System.Drawing.Font("Verdana", 16); System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Blue);</font> <font size="+0">g.DrawString(addText, f, b, 15, 15); g.Dispose();</font> <font size="+0">image.Save(Path_sy); image.Dispose(); }</font> <font size="+0">/**/ /// <summary> /// 在图片上生成图片水印 /// </summary> /// <param name="Path">原服务器图片路径</param> /// <param name="Path_syp">生成的带图片水印的图片路径</param> /// <param name="Path_sypf">水印图片路径</param> protected void AddShuiYinPic(string Path, string Path_syp, string Path_sypf) { System.Drawing.Image image = System.Drawing.Image.FromFile(Path); System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image); g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel); g.Dispose();</font> <font size="+0">image.Save(Path_syp); image.Dispose(); }</font></div>
试试其它关键字
缩略图
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3