首页  >  文章  >  php教程  >  C#图片加水印实例与代码

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

WBOY
WBOY原创
2016-07-06 13:30:201842浏览

本文要提供的类可以为图片加文字水印,以及判断是否是图片文件。经过测试可运行,例子请下载: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类的代码:

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">using</span><span style="color: #000000;"> System.Drawing;
</span><span style="color: #008080;"> 2</span> <span style="color: #0000ff;">using</span><span style="color: #000000;"> System.Drawing.Imaging;
</span><span style="color: #008080;"> 3</span> <span style="color: #0000ff;">using</span><span style="color: #000000;"> System.IO;
</span><span style="color: #008080;"> 4</span> 
<span style="color: #008080;"> 5</span> <span style="color: #0000ff;">namespace</span><span style="color: #000000;"> HoverTreeBatch.HovercFrame
</span><span style="color: #008080;"> 6</span> <span style="color: #000000;">{
</span><span style="color: #008080;"> 7</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> HovercWarter
</span><span style="color: #008080;"> 8</span> <span style="color: #000000;">{
</span><span style="color: #008080;"> 9</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> Image AddTextToImg(Image image, <span style="color: #0000ff;">string</span><span style="color: #000000;"> text)
</span><span style="color: #008080;">10</span> <span style="color: #000000;">{
</span><span style="color: #008080;">11</span> Bitmap bitmap = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Bitmap(image, image.Width, image.Height);
</span><span style="color: #008080;">12</span> Graphics g =<span style="color: #000000;"> Graphics.FromImage(bitmap);
</span><span style="color: #008080;">13</span> 
<span style="color: #008080;">14</span> <span style="color: #0000ff;">float</span> fontSize = <span style="color: #800080;">12.0f</span>; <span style="color: #008000;">//</span><span style="color: #008000;">字体大小</span>
<span style="color: #008080;">15</span> <span style="color: #0000ff;">float</span> textWidth = text.Length * fontSize; <span style="color: #008000;">//</span><span style="color: #008000;">文本的长度
</span><span style="color: #008080;">16</span> <span style="color: #008000;">//</span><span style="color: #008000;">下面定义一个矩形区域,以后在这个矩形里画上白底黑字</span>
<span style="color: #008080;">17</span> <span style="color: #0000ff;">float</span> rectX = <span style="color: #800080;">0</span><span style="color: #000000;">;
</span><span style="color: #008080;">18</span> <span style="color: #0000ff;">float</span> rectY = <span style="color: #800080;">0</span><span style="color: #000000;">;
</span><span style="color: #008080;">19</span> <span style="color: #0000ff;">float</span> rectWidth = text.Length * (fontSize + <span style="color: #800080;">8</span><span style="color: #000000;">);
</span><span style="color: #008080;">20</span> <span style="color: #0000ff;">float</span> rectHeight = fontSize + <span style="color: #800080;">8</span><span style="color: #000000;">;
</span><span style="color: #008080;">21</span> <span style="color: #008000;">//</span><span style="color: #008000;">声明矩形域</span>
<span style="color: #008080;">22</span> RectangleF textArea = <span style="color: #0000ff;">new</span><span style="color: #000000;"> RectangleF(rectX, rectY, rectWidth, rectHeight);
</span><span style="color: #008080;">23</span> 
<span style="color: #008080;">24</span> Font font = <span style="color: #0000ff;">new</span> Font(<span style="color: #800000;">"</span><span style="color: #800000;">宋体</span><span style="color: #800000;">"</span>, fontSize); <span style="color: #008000;">//</span><span style="color: #008000;">定义字体</span>
<span style="color: #008080;">25</span> Brush whiteBrush = <span style="color: #0000ff;">new</span> SolidBrush(Color.White); <span style="color: #008000;">//</span><span style="color: #008000;">白笔刷,画文字用</span>
<span style="color: #008080;">26</span> Brush blackBrush = <span style="color: #0000ff;">new</span> SolidBrush(Color.Black); <span style="color: #008000;">//</span><span style="color: #008000;">黑笔刷,画背景用</span>
<span style="color: #008080;">27</span> 
<span style="color: #008080;">28</span> <span style="color: #000000;">g.FillRectangle(blackBrush, rectX, rectY, rectWidth, rectHeight);
</span><span style="color: #008080;">29</span> 
<span style="color: #008080;">30</span> <span style="color: #000000;">g.DrawString(text, font, whiteBrush, textArea);
</span><span style="color: #008080;">31</span> MemoryStream ms = <span style="color: #0000ff;">new</span><span style="color: #000000;"> MemoryStream();
</span><span style="color: #008080;">32</span> <span style="color: #008000;">//</span><span style="color: #008000;">保存为Jpg类型</span>
<span style="color: #008080;">33</span> <span style="color: #000000;">bitmap.Save(ms, ImageFormat.Jpeg);
</span><span style="color: #008080;">34</span> 
<span style="color: #008080;">35</span> Image h_hovercImg =<span style="color: #000000;"> Image.FromStream(ms);
</span><span style="color: #008080;">36</span> 
<span style="color: #008080;">37</span> <span style="color: #000000;">g.Dispose();
</span><span style="color: #008080;">38</span> <span style="color: #000000;">bitmap.Dispose();
</span><span style="color: #008080;">39</span> 
<span style="color: #008080;">40</span> 
<span style="color: #008080;">41</span> <span style="color: #0000ff;">return</span><span style="color: #000000;"> h_hovercImg;
</span><span style="color: #008080;">42</span> <span style="color: #000000;">}
</span><span style="color: #008080;">43</span> 
<span style="color: #008080;">44</span> 
<span style="color: #008080;">45</span> <span style="color: #808080;">///</span> <span style="color: #808080;"><summary></summary></span>
<span style="color: #008080;">46</span> <span style="color: #808080;">///</span><span style="color: #008000;"> 根据文件头判断上传的文件类型
</span><span style="color: #008080;">47</span> <span style="color: #808080;">///</span> <span style="color: #808080;"></span>
<span style="color: #008080;">48</span> <span style="color: #808080;">///</span> <span style="color: #808080;"><param name="filePath"></span><span style="color: #008000;">filePath是文件的完整路径 </span><span style="color: #808080;"></span>
<span style="color: #008080;">49</span> <span style="color: #808080;">///</span> <span style="color: #808080;"><returns></returns></span><span style="color: #008000;">返回true或false</span><span style="color: #808080;"></span>
<span style="color: #008080;">50</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">bool</span> IsPicture(<span style="color: #0000ff;">string</span><span style="color: #000000;"> filePath)
</span><span style="color: #008080;">51</span> <span style="color: #000000;">{
</span><span style="color: #008080;">52</span> <span style="color: #0000ff;">try</span>
<span style="color: #008080;">53</span> <span style="color: #000000;">{
</span><span style="color: #008080;">54</span> FileStream fs = <span style="color: #0000ff;">new</span><span style="color: #000000;"> FileStream(filePath, FileMode.Open, Fileaccess.Read);
</span><span style="color: #008080;">55</span> BinaryReader reader = <span style="color: #0000ff;">new</span><span style="color: #000000;"> BinaryReader(fs);
</span><span style="color: #008080;">56</span> <span style="color: #0000ff;">string</span><span style="color: #000000;"> fileClass;
</span><span style="color: #008080;">57</span> <span style="color: #0000ff;">byte</span><span style="color: #000000;"> buffer;
</span><span style="color: #008080;">58</span> buffer =<span style="color: #000000;"> reader.ReadByte();
</span><span style="color: #008080;">59</span> fileClass =<span style="color: #000000;"> buffer.ToString();
</span><span style="color: #008080;">60</span> buffer =<span style="color: #000000;"> reader.ReadByte();
</span><span style="color: #008080;">61</span> fileClass +=<span style="color: #000000;"> buffer.ToString();
</span><span style="color: #008080;">62</span> <span style="color: #000000;">reader.Close();
</span><span style="color: #008080;">63</span> <span style="color: #000000;">fs.Close();
</span><span style="color: #008080;">64</span> <span style="color: #0000ff;">if</span> (fileClass == <span style="color: #800000;">"</span><span style="color: #800000;">255216</span><span style="color: #800000;">"</span> || fileClass == <span style="color: #800000;">"</span><span style="color: #800000;">7173</span><span style="color: #800000;">"</span> || fileClass == <span style="color: #800000;">"</span><span style="color: #800000;">13780</span><span style="color: #800000;">"</span> || fileClass == <span style="color: #800000;">"</span><span style="color: #800000;">6677</span><span style="color: #800000;">"</span><span style="color: #000000;">)
</span><span style="color: #008080;">65</span> <span style="color: #008000;">//</span><span style="color: #008000;">何问起 hovertree.com
</span><span style="color: #008080;">66</span> <span style="color: #008000;">//</span><span style="color: #008000;">255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar </span>
<span style="color: #008080;">67</span> <span style="color: #000000;">{
</span><span style="color: #008080;">68</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
</span><span style="color: #008080;">69</span> <span style="color: #000000;">}
</span><span style="color: #008080;">70</span> <span style="color: #0000ff;">else</span>
<span style="color: #008080;">71</span> <span style="color: #000000;">{
</span><span style="color: #008080;">72</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
</span><span style="color: #008080;">73</span> <span style="color: #000000;">}
</span><span style="color: #008080;">74</span> <span style="color: #000000;">}
</span><span style="color: #008080;">75</span> <span style="color: #0000ff;">catch</span>
<span style="color: #008080;">76</span> <span style="color: #000000;">{
</span><span style="color: #008080;">77</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
</span><span style="color: #008080;">78</span> <span style="color: #000000;">}
</span><span style="color: #008080;">79</span> <span style="color: #000000;">}
</span><span style="color: #008080;">80</span> <span style="color: #000000;">}
</span><span style="color: #008080;">81</span> }

另外出一道.NET的题目:http://hovertree.com/shortanswer/bjaf/9vqxwuda.htm

开发技术文章收集: http://www.cnblogs.com/sosoft/p/kaifajishu.html


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn