复制代码 代码如下:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
namespace ImageDrawing
{
/// <summary>
/// 图片修改类,主要是用来保护图片版权的
/// </summary>
public class ImageModification
{
#region "member fields"
private string modifyImagePath=null;
private string drawedImagePath=null;
private int rightSpace;
private int bottoamSpace;
private int lucencyPercent=70;
private string outPath=null;
#endregion
public ImageModification()
{
}
#region "propertys"
/// <summary>
/// 获取或设置要修改的图像路径
/// </summary>
public string ModifyImagePath
{
get{return this.modifyImagePath;}
set{this.modifyImagePath=value;}
}
/// <summary>
/// 获取或设置在画的图片路径(水印图片)
/// </summary>
public string DrawedImagePath
{
get{return this.drawedImagePath;}
set{this.drawedImagePath=value;}
}
/// <summary>
/// 获取或设置水印在修改图片中的右边距
/// </summary>
public int RightSpace
{
get{return this.rightSpace;}
set{this.rightSpace=value;}
}
//获取或设置水印在修改图片中距底部的高度
public int BottoamSpace
{
get{return this.bottoamSpace;}
set{this.bottoamSpace=value;}
}
/// <summary>
/// 获取或设置要绘制水印的透明度,注意是原来图片透明度的百分比
/// </summary>
public int LucencyPercent
{
get{return this.lucencyPercent;}
set
{
if(value>=0&&value<=100)
this.lucencyPercent=value;
}
}
/// <summary>
/// 获取或设置要输出图像的路径
/// </summary>
public string OutPath
{
get{return this.outPath;}
set{this.outPath=value;}
}
#endregion
#region "methods"
/// <summary>
/// 开始绘制水印
/// </summary>
public void DrawImage()
{
Image modifyImage=null;
Image drawedImage=null;
Graphics g=null;
try
{
//建立图形对象
modifyImage=Image.FromFile(this.ModifyImagePath);
drawedImage=Image.FromFile(this.DrawedImagePath);
g=Graphics.FromImage(modifyImage);
//获取要绘制图形坐标
int x=modifyImage.Width-this.rightSpace;
int y=modifyImage.Height-this.BottoamSpace;
//设置颜色矩阵
float[][] matrixItems ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, (float)this.LucencyPercent/100f, 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
ImageAttributes imgAttr=new ImageAttributes();
imgAttr.SetColorMatrix(colorMatrix,ColorMatrixFlag.Default,ColorAdjustType.Bitmap);
//绘制阴影图像
g.DrawImage(
drawedImage,
new Rectangle(x,y,drawedImage.Width,drawedImage.Height),
0,0,drawedImage.Width,drawedImage.Height,
GraphicsUnit.Pixel,imgAttr);
//保存文件
string[] allowImageType={".jpg",".gif",".png",".bmp",".tiff",".wmf",".ico"};
FileInfo file=new FileInfo(this.ModifyImagePath);
ImageFormat imageType=ImageFormat.Gif;
switch(file.Extension.ToLower())
{
case ".jpg":
imageType=ImageFormat.Jpeg;
break;
case ".gif":
imageType=ImageFormat.Gif;
break;
case ".png":
imageType=ImageFormat.Png;
break;
case ".bmp":
imageType=ImageFormat.Bmp;
break;
case ".tif":
imageType=ImageFormat.Tiff;
break;
case ".wmf":
imageType=ImageFormat.Wmf;
break;
case ".ico":
imageType=ImageFormat.Icon;
break;
default:
break;
}
MemoryStream ms=new MemoryStream();
modifyImage.Save(ms,imageType);
byte[] imgData=ms.ToArray();
modifyImage.Dispose();
drawedImage.Dispose();
g.Dispose();
FileStream fs=null;
if(this.OutPath==null || this.OutPath=="")
{
File.Delete(this.ModifyImagePath);
fs=new FileStream(this.ModifyImagePath,FileMode.Create,FileAccess.Write);
}
else
{
fs=new FileStream(this.OutPath,FileMode.Create,FileAccess.Write);
}
if(fs!=null)
{
fs.Write(imgData,0,imgData.Length);
fs.Close();
}
}
finally
{
try
{
drawedImage.Dispose();
modifyImage.Dispose();
g.Dispose();
}
catch{;}
}
}
#endregion
}
}
相关推荐:
seo用什么手法,seo方式 ,ai绘画飞翔
SEO短视:为何眼前的成效不能成为长久的策略?,网站上线seo优化
OpenAIGPTChatSoraOpenAIChatGPT服务在中断数小时后已恢复,ai视图线稿
AI智能工具的无限可能:未来已来,你准备好了吗?
seo涉及什么内容,seo主要包括 ,法医使用ai
SEO目的:如何通过精准优化提高网站流量与转化率,百度推广网站关键词
SEO优:让网站流量和排名实现飞跃的秘诀,大理哪有企业网站建设
为什么选择老域名工具?提升网站SEO与品牌影响力的秘密武器,软文与seo文章区别
seo要寻找什么资源,怎么找seo ,ai插画描边
ChatGPT中文版下载,开启智能对话新体验,婚纱ai男
为什么seo对企业重要,seo对企业进行网络营销的价值 ,ai李沁换脸鲍鱼
ChatGPT免费版下载:智能对话助手带来的全新体验,电脑怎么下载Ai微认证
ChatGPT对于大数据发展的帮助:赋能行业变革,推动智能化未来,ai写作真的好吗
ChatGPT宕机恢复时间如何解决用户焦虑与技术背后的故事,ai人工智能写作火山
AI仿写文章:开启内容创作新纪元
ChatGPT回答是空白的背后,究竟隐藏着什么秘密?,如何用ai写作来赚钱
seo矩阵运营中心是什么,seo矩阵运营中心是什么意思啊 ,北京ai特效
SEO定价策略:如何根据企业需求定制最佳价格方案,教育培训抖音营销推广
AI免费试用不需要登录:体验智能科技的魅力,轻松开启未来,华为ai身材
seo需要会什么技术呢,seo需要会什么技术呢知乎 ,红色框ai
ChatGPT安装包Windows版:让AI助力你的工作与生活,人工智能ai不是梦在线
SEO策划:让你的网站迅速脱颖而出的秘诀,seo优化和技巧
SEO管家:为您的网站保驾护航的智能SEO助手,网站推广作用有哪些类型
SEO爱站:提升网站排名,赢得流量的秘密武器,优化网站设计价格多少
seo资源指的是什么,seo资料 ,绿眼AI
SEO合作:让你的网站在搜索引擎中脱颖而出,惠州抖音seo策划公司
SEO地位:如何提升你的网站排名与曝光度,抢占市场先机,微营销网络推广
seo,seoul city ,ai精洗
优化软件:让电脑性能焕然一新,提升效率的秘密武器,大理网站推广招聘信息最新
ChatGPT不能打开EL?揭秘这一困扰背后的真相与解决方案,ai被动房
seo自己做什么,自己做seo需要花钱吗 ,ai850775
SEO优化的利器:提升网站排名的秘密武器,联想ai7g-3笔记本
Zkept:重塑未来生活的智能科技革新,营销推广的面试问题
自动写文章的AI,提升效率的创作利器
seo站内链接有什么作用,seo中网站内链的作用 ,781900ai
SEO提高:如何通过精准优化让网站流量翻倍,优化排名seo加盟费用
seo网络上什么意思,seo表示什么 ,如何避免今日头条ai写作检测
SEO快速排名实例:如何通过实战技巧提升网站排名,快速突破流量瓶颈,安徽ai跑步机企业
在线AI文章生成器开启智能创作新时代
seo需要会什么,seo要会些什么 ,ai313414919
OpenAIGPT:开启智能时代的语言革命,ai辣妹动漫
seo营销到底是什么,seo算什么营销 ,ai里面新建
文章创作AI:引领智能写作的新时代
ChatGPT支持多种语言输入输出,让全球资讯触手可及,联想拯救者的ai写作
什么是seo公司口碑,seo品牌 ,ai大模型训练是什么意思
AI一键生成文章免费版:颠覆写作新体验
《“查看更多”背后的秘密:让你的人生更加丰富多彩!》,宁安公司网站建设
ChatGPT打不开网页?看完这篇你就懂了!,国内ai写作安卓软件
SEO功能:助力网站流量提升与品牌曝光,天津公司网站建设与维护
SEO中权重是什么意思?让你迅速网站排名的核心秘密!,长颈鹿智能AI点读机