没有合适的资源?快使用搜索试试~ 我知道了~
本文实例讲述了C#给图片添加水印的方法。分享给大家供大家参考,具体如下: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Drawing; usin
资源推荐
资源详情
资源评论












C#给图片添加水印完整实例给图片添加水印完整实例
本文实例讲述了C#给图片添加水印的方法。分享给大家供大家参考,具体如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
/// <summary>
///ImgWater 的摘要说明
/// </summary>
public class ImgWater
{
public ImgWater()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 图片水印
/// </summary>
/// <param name="ImgFile">原图文件地址</param>
/// <param name="WaterImg">水印图片地址</param>
/// <param name="sImgPath">水印图片保存地址</param>
/// <param name="Alpha">水印透明度设置</param>
/// <param name="iScale">水印图片在原图上的显示比例</param>
/// <param name="intDistance">水印图片在原图上的边距确定,以图片的右边和下边为准,当设定的边距超过一定大小后参数会自动失效</param>
public bool zzsImgWater(
string ImgFile
, string WaterImg
, string sImgPath
, float Alpha
, float iScale
, int intDistance
)
{
try
{
//装载图片
FileStream fs = new FileStream(ImgFile, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] bytes = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
MemoryStream ms = new MemoryStream(bytes);
System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);
//System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile);
int imgPhotoWidth = imgPhoto.Width;
int imgPhotoHeight = imgPhoto.Height;
System.Drawing.Image imgWatermark = new Bitmap(WaterImg);
int imgWatermarkWidth = imgWatermark.Width;
int imgWatermarkHeight = imgWatermark.Height;
//计算水印图片尺寸
decimal aScale = Convert.ToDecimal(iScale);
decimal pScale = 0.05M;
decimal MinScale = aScale - pScale;
decimal MaxScale = aScale + pScale;
int imgWatermarkWidthNew = imgWatermarkWidth;
int imgWatermarkHeightNew = imgWatermarkHeight;
if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth >= imgPhotoHeight)
if (imgWatermarkWidth > imgWatermarkHeight)
if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7)) &&
(Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7) <= MaxScale))
{
}
else
{
imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
imgWatermarkHeightNew = Convert.ToInt32((imgPhotoWidth * aScale / imgWatermarkWidth) * imgWatermarkHeight);

}
else
if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7)) &&
(Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7) <= MaxScale))
{
}
else
{
imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
imgWatermarkWidthNew = Convert.ToInt32((imgPhotoHeight * aScale / imgWatermarkHeight) * imgWatermarkWidth);
}
if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth >= imgWatermarkHeight)
{
imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
}
if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight)
{
imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
}
if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight)
{
imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
}
if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth <= imgPhotoHeight)
if (imgWatermarkWidth > imgWatermarkHeight)
if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7)) &&
(Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7) <= MaxScale))
{
}
else
{
imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
}
else
if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7)) &&
(Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7) <= MaxScale))
{
}
else
{
imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
}
if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth <= imgWatermarkHeight)
{
imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
}
if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight)
{
imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
}
if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight)
{
imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
}
//将原图画出来
Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(72, 72);
Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gbmPhoto.Clear(Color.White);
gbmPhoto.DrawImage(
imgPhoto
, new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight)
, 0
, 0
, imgPhotoWidth
, imgPhotoHeight
, GraphicsUnit.Pixel
);
Bitmap bmWatermark = new Bitmap(bmPhoto);
bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
Graphics gWatermark = Graphics.FromImage(bmWatermark);
剩余8页未读,继续阅读
资源评论


weixin_38721405
- 粉丝: 3
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- PLC控制交通灯设计方案毕业论文.docx
- c语言课程设计方案报告.doc
- Windows网络服务搭建管理之WEBFTP(服务器群集负载平衡)CA证书服务器的搭建和配置.doc
- 谈航道系统档案信息化管理存在的问题及发展对策.docx
- 建设工程项目管理存在问题.doc
- 单片机霓虹灯控制系统设计方案.doc
- 专业名称:计算机应用技术.doc
- 企业网络设计规划.doc
- 质量保证计划软件.doc
- PLC实验室项目申请书.doc
- 物联网在平安校园建设中的应用与研究.docx
- BC网站的分析与设计方案.doc
- 基于微课教育的中职计算机应用基础教育研究.docx
- 把MSHFlexGrid里数据导出至Excel.doc
- 计算机在体育管理中应用研究.docx
- 大数据时代初中数学高效课堂的构建.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
