当前位置: 移动技术网 > IT编程>开发语言>c# > C#图片添加水印的实现代码

C#图片添加水印的实现代码

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例介绍了c#图片添加水印的实现方法,可以为图片加文字水印,及判断是否是图片文件,分享给大家供大家参考,具体内容如下

效果图:

以下是hovercwarter类的代码:

using system.drawing;
using system.drawing.imaging;
using system.io;

namespace hovertreebatch.hovercframe
{
public class hovercwarter
{
public static image addtexttoimg(image image, string text)
{
bitmap bitmap = new bitmap(image, image.width, image.height);
graphics g = graphics.fromimage(bitmap);

float fontsize = 12.0f; //字体大小
float textwidth = text.length * fontsize; //文本的长度
//下面定义一个矩形区域,以后在这个矩形里画上白底黑字
float rectx = 0;
float recty = 0;
float rectwidth = text.length * (fontsize + 8);
float rectheight = fontsize + 8;
//声明矩形域
rectanglef textarea = new rectanglef(rectx, recty, rectwidth, rectheight);

font font = new font("宋体", fontsize); //定义字体
brush whitebrush = new solidbrush(color.white); //白笔刷,画文字用
brush blackbrush = new solidbrush(color.black); //黑笔刷,画背景用

g.fillrectangle(blackbrush, rectx, recty, rectwidth, rectheight);

g.drawstring(text, font, whitebrush, textarea);
memorystream ms = new memorystream();
//保存为jpg类型
bitmap.save(ms, imageformat.jpeg);

image h_hovercimg = image.fromstream(ms);

g.dispose();
bitmap.dispose();


return h_hovercimg;
}


/// <summary>
/// 根据文件头判断上传的文件类型
/// </summary>
/// <param name="filepath">filepath是文件的完整路径 </param>
/// <returns>返回true或false</returns>
public static bool ispicture(string filepath)
{
try
{
filestream fs = new filestream(filepath, filemode.open, fileaccess.read);
binaryreader reader = new binaryreader(fs);
string fileclass;
byte buffer;
buffer = reader.readbyte();
fileclass = buffer.tostring();
buffer = reader.readbyte();
fileclass += buffer.tostring();
reader.close();
fs.close();
if (fileclass == "255216" || fileclass == "7173" || fileclass == "13780" || fileclass == "6677")
//何问起 hovertree.com
//255216是jpg;7173是gif;6677是bmp,13780是png;7790是exe,8297是rar 
{
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}
}
}

以上就是c#实现图片添加水印的关键性代码,希望对大家学习c#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网