给图片添加水印效果图的函数(可以在图片上添加自己的版权和LOGO图片的水印)

简介: Code 1 protected void Button1_ServerClick(object sender, System.EventArgs e) 2         { 3             if(File1.
img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif Code
 1 protected void Button1_ServerClick(object sender, System.EventArgs e)
 2         {
 3             if(File1.PostedFile!=null)
 4             {    
 5                 string fileName = File1.PostedFile.FileName ;
 6                 //取得上传文件的扩展名
 7                 string fileExtName =fileName.Substring(fileName.LastIndexOf("."));
 8                 //创建GUID,作为上传文件的唯一标识
 9                 System.Guid myGuid = System.Guid.NewGuid();
10                 string savePath = Server.MapPath("~").ToString() + "//uploadfile//" + myGuid.ToString() + "." + fileExtName;
11                 string newSavePath = Server.MapPath("~").ToString() + "//uploadfile1//" + myGuid.ToString() + "." + fileExtName;
12                 string waterMarkPath = Server.MapPath("~").ToString() + "//uploadfile//watermark.bmp"
13                 File1.PostedFile.SaveAs(savePath);
14                 watermark myWatermark = new watermark();
15                 myWatermark.MakeWatermark("北京朝阳",waterMarkPath,savePath,newSavePath);
16 //                //文件的相关属性:文件名,文件类型,文件大小
17 //                myFile.PostedFile.FileName;
18 //                myFile.PostedFile.ContentType ;
19 //                myFile.PostedFile.ContentLength.ToString();
20             }
21         }
22 

 

看看 类的处理
/**//*
* 给图片设置水印效果图的函数
* 可以在图片上添加自己的版权和LOGO图片的水印
*
* 原作者:Joel Neubeck ,from bbs.51aspx.com
*
* 本人在原作者的基础上进行了部分修改,并改为一个单独的函数
* 大家可以根据需要修改此代码
* 但请保留原作者信息.
*
* 星宿.net(zhuhee)
* 修改于2006-6-14
*
*/

 

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif Code
  1 using System;
  2 using System.Drawing;
  3 using System.Drawing.Imaging;
  4 using System.Drawing.Drawing2D;
  5 namespace WebWaterMark.Components
  6 {
  7     /**//// <summary>
  8     /// watermark 的摘要说明。
  9     /// </summary>
 10     public class watermark
 11     {
 12         public watermark()
 13         {
 14             //
 15             // TODO: 在此处添加构造函数逻辑
 16             //
 17         }
 18         /**//// <summary>
 19         /// 用于处理图片
 20         /// 给图片加入水印
 21         /// </summary>
 22         /// <param name="Copyright">需要写入的版权信息</param>
 23         /// <param name="MarkBmpPath">需要假如的logo图片</param>
 24         /// <param name="photoPath">需要处理的图片的路径</param>
 25         /// <param name="savePhotoPath">保存的图片路径</param>
 26         public void MakeWatermark(string Copyright,string MarkBmpPath,string photoPath,string savePhotoPath)
 27         {
 28             //创建一个image对象,即要被处理的图片
 29             Image imgPhoto = Image.FromFile(photoPath);
 30             int phWidth = imgPhoto.Width;
 31             int phHeight = imgPhoto.Height;
 32             //创建原始图片大小的Bitmap
 33             Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
 34             bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
 35             //将位图bmPhoto加载到Graphics对象
 36             Graphics grPhoto = Graphics.FromImage(bmPhoto);
 37             //创建一个需要填充水银的Image对象
 38             Image imgWatermark = new Bitmap(MarkBmpPath);
 39             int wmWidth = imgWatermark.Width;
 40             int wmHeight = imgWatermark.Height;
 41             //------------------------------------------------------------
 42             //第一步,插入版权信息
 43             //------------------------------------------------------------
 44             //设置图象的成相质量
 45             grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
 46             //将原始图象绘制到grPhoto上
 47             grPhoto.DrawImage(
 48                 imgPhoto,                              // 要绘制的Image对象
 49                 new Rectangle(00, phWidth, phHeight), // 绘制图象的位置和大小
 50                 0,                                      // 要绘制的原图象部分的左上角的X坐标
 51                 0,                                      // 要绘制的原图象部分的左上角的Y坐标
 52                 phWidth,                                // 要绘制的原图象的高度
 53                 phHeight,                              // 要绘制的原图象的宽度
 54                 GraphicsUnit.Pixel);                    // 源矩形的度量单位
 55             //-------------------------------------------------------
 56             //字体大小放在一个数组中,最大字体为32.
 57             //感觉用处不大,可以自己再修改这里
 58             //-------------------------------------------------------
 59             int[] sizes = new int[]{32,14,12,10,8,6,4};
 60             Font crFont = null;
 61             SizeF crSize = new SizeF();
 62             //循环测试数组中所定义的字体大小是否适合版权信息,如果合适就使用此种字体大小
 63             for (int i=0 ;i<6; i++)
 64             {
 65                 //设置字体类型,可以单独提出,作为参数
 66                 crFont = new Font("迷你繁篆书", sizes[0], FontStyle.Bold);
 67                 //测量此种字体大小
 68                 crSize = grPhoto.MeasureString(Copyright, crFont);
 69                 if((ushort)crSize.Width < (ushort)phWidth)
 70                     break;
 71             }
 72             //给底部保留%3的空间
 73             int yPixlesFromBottom = (int)(phHeight *.03);
 74             //设置字体在图片中的位置
 75             float yPosFromBottom = ((phHeight - yPixlesFromBottom)-(crSize.Height/2));
 76             //float xCenterOfImg = (phWidth/2);
 77             float xCenterOfImg = (phWidth-(crSize.Width)/2);
 78             //设置字体居中
 79             StringFormat StrFormat = new StringFormat();
 80             StrFormat.Alignment = StringAlignment.Center;
 81             
 82             //设置绘制文本的颜色和纹理 (Alpha=153)
 83             SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153000));
 84             //将版权信息绘制到图象上
 85             grPhoto.DrawString(Copyright,                    //版权信息
 86                 crFont,                                      //字体
 87                 semiTransBrush2,                              //绘制文本的颜色及纹理
 88                 new PointF(xCenterOfImg+1,yPosFromBottom+1),  //绘制文本的位置
 89                 StrFormat);                                  //格式
 90             //设置绘制文本的颜色和纹理 (Alpha=153)
 91             SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153255255255));
 92             //重新绘制版权信息,以让其具有阴影效果
 93             //将文本向又下移动一个象素
 94             grPhoto.DrawString(Copyright,                //版权信息
 95                 crFont,                                  //字体
 96                 semiTransBrush,                          //绘制文本的颜色及纹理
 97                 new PointF(xCenterOfImg,yPosFromBottom),  //绘制文本的位置
 98                 StrFormat);                              //格式
 99             
100             //------------------------------------------------------------
101             //第二步,插入图片水印
102             //------------------------------------------------------------
103             //在原来修改过的bmPhoto上创建一个水银位图
104             Bitmap bmWatermark = new Bitmap(bmPhoto);
105             bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
106             //将位图bmWatermark加载到Graphics对象
107             Graphics grWatermark = Graphics.FromImage(bmWatermark);
108             //To achieve a transulcent watermark we will apply (2) color 
109             //manipulations by defineing a ImageAttributes object and 
110             //seting (2) of its properties.
111             ImageAttributes imageAttributes = new ImageAttributes();
112             //The first step in manipulating the watermark image is to replace 
113             //the background color with one that is trasparent (Alpha=0, R=0, G=0, B=0)
114             //to do this we will use a Colormap and use this to define a RemapTable
115             ColorMap colorMap = new ColorMap();
116             //My watermark was defined with a background of 100% Green this will
117             //be the color we search for and replace with transparency
118             colorMap.OldColor = Color.FromArgb(25502550);
119             colorMap.NewColor = Color.FromArgb(0000);
120             ColorMap[] remapTable = { colorMap };
121             imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
122             //The second color manipulation is used to change the opacity of the 
123             //watermark.  This is done by applying a 5x5 matrix that contains the 
124             //coordinates for the RGBA space.  By setting the 3rd row and 3rd column 
125             //to 0.3f we achive a level of opacity
126             float[][] colorMatrixElements = { 
127                                                 new float[] {1.0f,  0.0f,  0.0f,  0.0f0.0f},      
128                                                 new float[] {0.0f,  1.0f,  0.0f,  0.0f0.0f},        
129                                                 new float[] {0.0f,  0.0f,  1.0f,  0.0f0.0f},        
130                                                 new float[] {0.0f,  0.0f,  0.0f,  0.3f0.0f},        
131                                                 new float[] {0.0f,  0.0f,  0.0f,  0.0f1.0f}};
132             ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);
133             imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,
134                 ColorAdjustType.Bitmap);
135             //For this example we will place the watermark in the upper right
136             //hand corner of the photograph. offset down 10 pixels and to the 
137             //left 10 pixles
138             int xPosOfWm = ((phWidth - wmWidth) - 10);
139             int yPosOfWm = 10;
140             grWatermark.DrawImage(imgWatermark,
141                 new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight),  //Set the detination Position
142                 0,                  // x-coordinate of the portion of the source image to draw. 
143                 0,                  // y-coordinate of the portion of the source image to draw. 
144                 wmWidth,            // Watermark Width
145                 wmHeight,            // Watermark Height
146                 GraphicsUnit.Pixel, // Unit of measurment
147                 imageAttributes);  //ImageAttributes Object
148             //Replace the original photgraphs bitmap with the new Bitmap
149             imgPhoto = bmWatermark;
150             grPhoto.Dispose();
151             grWatermark.Dispose();
152             //save new image to file system.
153             imgPhoto.Save(savePhotoPath, ImageFormat.Jpeg);
154             imgPhoto.Dispose();
155             imgWatermark.Dispose();
156         }
157     }
158 }
159 

 

目录
相关文章
|
9月前
|
人工智能 并行计算 调度
【AI系统】CUDA 编程模式
本文介绍了英伟达GPU的CUDA编程模型及其SIMT执行模式,对比了SIMD和SIMT的特点,阐述了SIMT如何提高并行计算效率和编程灵活性。同时简要提及了AMD的GPU架构及编程模型,包括最新的MI300X和ROCm平台。
341 5
|
API 云计算 开发者
使用宜搭平台带来的便利:技术解析与实践
【9月更文第8天】随着企业信息化建设的不断深入,业务流程自动化的需求日益增长。宜搭平台作为一种高效的应用构建工具,为企业提供了快速搭建各类业务系统的可能。本文将探讨使用宜搭平台给企业和开发者带来的便利,并通过具体的代码示例展示其优势。
362 11
|
SQL 关系型数据库 Java
实时计算 Flink版操作报错之读取业务库数据,提示类型转换错误,该怎么处理
在使用实时计算Flink版过程中,可能会遇到各种错误,了解这些错误的原因及解决方法对于高效排错至关重要。针对具体问题,查看Flink的日志是关键,它们通常会提供更详细的错误信息和堆栈跟踪,有助于定位问题。此外,Flink社区文档和官方论坛也是寻求帮助的好去处。以下是一些常见的操作报错及其可能的原因与解决策略。
|
前端开发 UED
【专栏:CSS 基础篇】CSS 字体与文本样式:美化你的网页内容
【4月更文挑战第30天】网页设计中,字体和文本样式至关重要,影响视觉效果和用户体验。CSS允许设计师设置字体家族、大小、颜色、加粗、倾斜、行高和对齐方式等。高级特性包括引入外部字体和使用字体变体。响应式设计适应不同设备,确保良好阅读体验。实际案例和最佳实践强调易读性和一致性。掌握这些技巧能提升网页美感和用户交互,创造更多可能。
458 0
|
计算机视觉
[Halcon&图像] 图像增强算子汇总
[Halcon&图像] 图像增强算子汇总
542 1
|
SQL 存储 OLAP
2G内存搞定一亿数据的分析引擎
EuclidOLAP是一个可以在低配置服务器上执行上亿数据量分析并且快速响应和支持复杂查询的开源OLAP数据库。
257 2
|
前端开发 Java 容器
SpringBoot中注册Servlet、Filter和Listener(代码和注解两种方式)
SpringBoot中注册Servlet、Filter和Listener(代码和注解两种方式)
328 0
|
数据安全/隐私保护
code-server详细安装,最好云端同步写代码神器
code-server详细安装,最好云端同步写代码神器
code-server详细安装,最好云端同步写代码神器