您好,欢迎访问一九零五行业门户网

C#图片加水印实例与代码

本文要提供的类可以为图片加文字水印,以及判断是否是图片文件。经过测试可运行,例子请下载:http://hovertree.com/h/bjaf/5qc5eh6y.htm 例子效果图: 以下是hovercwarter类的代码: 1 using system.drawing; 2 using system.drawing.imaging; 3 using syst
本文要提供的类可以为图片加文字水印,以及判断是否是图片文件。经过测试可运行,例子请下载:http://hovertree.com/h/bjaf/5qc5eh6y.htm
例子效果图:
以下是hovercwarter类的代码:
1 using system.drawing; 2 using system.drawing.imaging; 3 using system.io; 4 5 namespace hovertreebatch.hovercframe 6 { 7 public class hovercwarter 8 { 9 public static image addtexttoimg(image image, string text)10 {11 bitmap bitmap = new bitmap(image, image.width, image.height);12 graphics g = graphics.fromimage(bitmap);13 14 float fontsize = 12.0f; //字体大小15 float textwidth = text.length * fontsize; //文本的长度16 //下面定义一个矩形区域,以后在这个矩形里画上白底黑字17 float rectx = 0;18 float recty = 0;19 float rectwidth = text.length * (fontsize + 8);20 float rectheight = fontsize + 8;21 //声明矩形域22 rectanglef textarea = new rectanglef(rectx, recty, rectwidth, rectheight);23 24 font font = new font(宋体, fontsize); //定义字体25 brush whitebrush = new solidbrush(color.white); //白笔刷,画文字用26 brush blackbrush = new solidbrush(color.black); //黑笔刷,画背景用27 28 g.fillrectangle(blackbrush, rectx, recty, rectwidth, rectheight);29 30 g.drawstring(text, font, whitebrush, textarea);31 memorystream ms = new memorystream();32 //保存为jpg类型33 bitmap.save(ms, imageformat.jpeg);34 35 image h_hovercimg = image.fromstream(ms);36 37 g.dispose();38 bitmap.dispose();39 40 41 return h_hovercimg;42 }43 44 45 /// 46 /// 根据文件头判断上传的文件类型47 /// 48 /// filepath是文件的完整路径 49 /// 返回true或false50 public static bool ispicture(string filepath)51 {52 try53 {54 filestream fs = new filestream(filepath, filemode.open, fileaccess.read);55 binaryreader reader = new binaryreader(fs);56 string fileclass;57 byte buffer;58 buffer = reader.readbyte();59 fileclass = buffer.tostring();60 buffer = reader.readbyte();61 fileclass += buffer.tostring();62 reader.close();63 fs.close();64 if (fileclass == 255216 || fileclass == 7173 || fileclass == 13780 || fileclass == 6677)65 //何问起 hovertree.com66 //255216是jpg;7173是gif;6677是bmp,13780是png;7790是exe,8297是rar 67 {68 return true;69 }70 else71 {72 return false;73 }74 }75 catch76 {77 return false;78 }79 }80 }81 }
另外出一道.net的题目:http://hovertree.com/shortanswer/bjaf/9vqxwuda.htm
开发技术文章收集: http://www.cnblogs.com/sosoft/p/kaifajishu.html
其它类似信息

推荐信息