在ASP.NET中上传图像时加入版权信息

时间:2006/5/21 14:34:40      阅读:1746          
      你试过用.net来进行图像处理吗?在网上上传图片时可能需要加入版权信息,以便保护自已的知识产权。以前在ASP中可能要花费不少周折,但现在有了ASP.NET,一切都迎刃而解了。 
 1Imports System.Drawing 
 2Imports System.IO 
 3Imports System.Drawing.Imaging 
 4 
 5Private  Sub AddWaterMark(ByVal fileName As String, ByVal text As String) 
 6 If Not File.Exists(MapPath(fileName)) Then
 7  Throw New FileNotFoundException("The file don't existNot ")
 8 End If
 9 
10 If text = String.Empty Then
11  Return
12 End If
13 '还需要判断文件类型是否为图像类型,这里就不赘述了 
14 
15 Dim image As System.Drawing.Image =  System.Drawing.Image.FromFile(MapPath(fileName)) 
16 Dim bitmap As Bitmap =  New Bitmap(image,image.Width,image.Height) 
17 Dim g As Graphics =  Graphics.FromImage(bitmap) 
18 
19 Dim fontSize As single =  12.0f  '字体大小 
20 Dim textWidth As single =  text.Length*fontSize  '文本的长度 
21 '下面定义一个矩形区域,以后在这个矩形里画上白底黑字 
22 Dim rectX As single =  0 
23 Dim rectY As single =  0 
24 Dim rectWidth As single =  text.Length*(fontSize+8) 
25 Dim rectHeight As single =  fontSize+8 
26 '声明矩形域 
27 Dim textArea As RectangleF =  New RectangleF(rectX,rectY,rectWidth,rectHeight) 
28 
29 Dim font As Font =  New Font("宋体",fontSize)  '定义字体 
30 Dim whiteBrush As Brush =  New SolidBrush(Color.White)  '白笔刷,画文字用 
31 Dim blackBrush As Brush =  New SolidBrush(Color.Black)  '黑笔刷,画背景用 
32 
33 g.FillRectangle(blackBrush,rectX,rectY,rectWidth,rectHeight) 
34 
35 g.DrawString(text,font,whiteBrush,textArea) 
36 Dim ms As MemoryStream =  New MemoryStream() 
37 '保存为Jpg类型 
38 bitmap.Save(ms,ImageFormat.Jpeg) 
39 
40 '输出处理后的图像,这里为了演示方便,我将图片显示在页面中了 
41 Response.Clear() 
42 Response.ContentType = "image/jpeg" 
43 Response.BinaryWrite(ms.ToArray()) 
44 
45 g.Dispose()  
46 bitmap.Dispose() 
47 image.Dispose()
48End Sub

      只要在程序中调用一下这个方法AddWaterMark("me.jpg","Binken’s Blog"),一切都OK了!
评论
  • Re:在ASP.NET中上传图像时加入版权信息  (2006/8/16 15:05:12) by 飘萍 
        就是给图片加水印吧?看代码不是很复杂啊。
  • Re:在ASP.NET中上传图像时加入版权信息  (2007/11/18 7:41:36) by doitnow 
    这个有用。
  • Re:在ASP.NET中上传图像时加入版权信息  (2009/3/30 17:07:36) by aghlqp 
    还是做个记号,以后用到在研究下
标 题:
 
姓 名:
 
主 页:

验证码:

评论:
 

Because of the cache,you may see your comments several minutes later.