【www.gdgbn.com--php常用代码】

///

  
/// creating a watermarked photograph with gdi+ for .net  
///
  
/// 原始图片的物理路径  
/// 水印图片的物理路径  
/// 水印文字(不显示水印文字设为空串)  
/// 输出合成后的图片的物理路径  
/// @整理: anyrock@mending.cn  
public void buildwatermark(string rsrcimgpath,string rmarkimgpath,string rmarktext,string rdstimgpath)  
{  
     //以下(代码)从一个指定文件创建了一个image 对象,然后为它的 width 和 height定义变量。  
     //这些长度待会被用来建立一个以24 bits 每像素的格式作为颜色数据的bitmap对象。  
     image imgphoto = image.fromfile(rsrcimgpath);  
     int phwidth = imgphoto.width;  
     int phheight = imgphoto.height;  
     bitmap bmphoto=new bitmap(phwidth,phheight, pixelformat.format24bpprgb);  
     bmphoto.setresolution(72,72);  
     graphics grphoto = graphics.fromimage(bmphoto);  
     //这个代码载入水印图片,水印图片已经被保存为一个bmp文件,以绿色(a=0,r=0,g=255,b=0)作为背景颜色。  
     //再一次,会为它的width 和height定义一个变量。  
     image imgwatermark = new bitmap(rmarkimgpath);  
     int wmwidth = imgwatermark.width;  
     int wmheight = imgwatermark.height;  
     //这个代码以100%它的原始大小绘制imgphoto 到graphics 对象的(x=0,y=0)位置。  
     //以后所有的绘图都将发生在原来照片的顶部。  
     grphoto.smoothingmode = smoothingmode.antialias;  
     grphoto.drawimage(  
          imgphoto,                                        
          new rectangle(0, 0, phwidth, phheight),   
          0,                                                    
          0,                                                      
          phwidth,                                          
          phheight,                                        
          graphicsunit.pixel);  
     //为了最大化版权信息的大小,我们将测试7种不同的字体大小来决定我们能为我们的照片宽度使用的可能的最大大小。  
     //为了有效地完成这个,我们将定义一个整型数组,接着遍历这些整型值测量不同大小的版权字符串。  
     //一旦我们决定了可能的最大大小,我们就退出循环,绘制文本  
     int[] sizes = new int[]{16,14,12,10,8,6,4};  
     font crfont = null;   
     sizef crsize = new  sizef();   
     for (int i=0 ;i<7; i++)  
     {   
          crfont = new font("arial", sizes[i],  
                fontstyle.bold);  
          crsize = grphoto.measurestring(rmarktext,  
                crfont);  
          if((ushort)crsize.width < (ushort)phwidth)  
                break;  
     }   
   

    //因为所有的照片都有各种各样的高度,所以就决定了从图象底部开始的5%的位置开始。  
     //使用rmarktext字符串的高度来决定绘制字符串合适的y坐标轴。  
     //通过计算图像的中心来决定x轴,然后定义一个stringformat 对象,设置stringalignment 为center。  
     int ypixlesfrombottom = (int)(phheight *.05);  
     float yposfrombottom = ((phheight -   
          ypixlesfrombottom)-(crsize.height/2));  
     float xcenterofimg = (phwidth/2);  
     stringformat strformat = new stringformat();  
     strformat.alignment = stringalignment.center;  
     //现在我们已经有了所有所需的位置坐标来使用60%黑色的一个color(alpha值153)创建一个solidbrush 。  
     //在偏离右边1像素,底部1像素的合适位置绘制版权字符串。  
     //这段偏离将用来创建阴影效果。使用brush重复这样一个过程,在前一个绘制的文本顶部绘制同样的文本。  
     solidbrush semitransbrush2 =   
          new solidbrush(color.fromargb(153, 0, 0,0));   
     grphoto.drawstring(rmarktext,                          
          crfont,                                                   
          semitransbrush2,                                      
          new pointf(xcenterofimg+1,yposfrombottom+1),   
          strformat);  
     solidbrush semitransbrush = new solidbrush(  
          color.fromargb(153, 255, 255, 255));  
     grphoto.drawstring(rmarktext,                      
          crfont,                                               
          semitransbrush,                                     
          new pointf(xcenterofimg,yposfrombottom),    
          strformat);  
     //根据前面修改后的照片创建一个bitmap。把这个bitmap载入到一个新的graphic对象。  
     bitmap bmwatermark = new bitmap(bmphoto);   
     bmwatermark.setresolution(  
          imgphoto.horizontalresolution,   
          imgphoto.verticalresolution);  
     graphics grwatermark =  
          graphics.fromimage(bmwatermark);  
     //通过定义一个imageattributes 对象并设置它的两个属性,我们就是实现了两个颜色的处理,以达到半透明的水印效果。  
     //处理水印图象的第一步是把背景图案变为透明的(alpha=0, r=0, g=0, b=0)。我们使用一个colormap 和定义一个remaptable来做这个。  
     //就像前面展示的,我的水印被定义为100%绿色背景,我们将搜到这个颜色,然后取代为透明。  
     imageattributes imageattributes =  
          new imageattributes();  
     colormap colormap = new colormap();  
     colormap.oldcolor=color.fromargb(255, 0, 255, 0);  
     colormap.newcolor=color.fromargb(0, 0, 0, 0);  
     colormap[] remaptable = {colormap};  
     //第二个颜色处理用来改变水印的不透明性。  
     //通过应用包含提供了坐标的rgba空间的5x5矩阵来做这个。  
     //通过设定第三行、第三列为0.3f我们就达到了一个不透明的水平。结果是水印会轻微地显示在图象底下一些。  
     imageattributes.setremaptable(remaptable,  
          coloradjusttype.bitmap);  
     float[][] colormatrixelements = {   
                                                     new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f},  
                                                     new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f},  
                                                     new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f},  
                                                     new float[] {0.0f,  0.0f,  0.0f,  0.3f, 0.0f},  
                                                     new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}  
                                                };  
     colormatrix wmcolormatrix = new  
          colormatrix(colormatrixelements);  
     imageattributes.setcolormatrix(wmcolormatrix,   
          colormatrixflag.default,   
          coloradjusttype.bitmap);   
        //随着两个颜色处理加入到imageattributes 对象,我们现在就能在照片右手边上绘制水印了。  
     //我们会偏离10像素到底部,10像素到左边。  
     int markwidth;  
     int markheight;  
     //mark比原来的图宽  
     if(phwidth<=wmwidth)  
     {  
          markwidth = phwidth-10;  
          markheight = (markwidth*wmheight)/wmwidth;  
     }  
     else if(phheight<=wmheight)  
     {  
          markheight = phheight-10;  
          markwidth = (markheight*wmwidth)/wmheight;  
     }  
     else  
     {  
          markwidth = wmwidth;  
          markheight = wmheight;  
     }  
     int xposofwm = ((phwidth - markwidth)-10);  
     int yposofwm = 10;  
     grwatermark.drawimage(imgwatermark,   
          new rectangle(xposofwm,yposofwm,markwidth,  
          markheight),  
          0,                          
          0,                           
          wmwidth,                
          wmheight,               
          graphicsunit.pixel,   
          imageattributes);  
     //最后的步骤将是使用新的bitmap取代原来的image。 销毁两个graphic对象,然后把image 保存到文件系统。  
     imgphoto = bmwatermark;  
     grphoto.dispose();  
     grwatermark.dispose();  
     imgphoto.save(rdstimgpath,imageformat.jpeg);  
     imgphoto.dispose();  
     imgwatermark.dispose();                  
}  

 

本文来源:http://www.gdgbn.com/jiaocheng/28634/