asp.net C#可設定透明度的圖片加浮水印標記代碼(1/3)

來源:互聯網
上載者:User

/// <summary>  
/// creating a watermarked photograph with gdi+ for .net  
/// </summary>  
/// <param name="rsrcimgpath">原始圖片的實體路徑</param>  
/// <param name="rmarkimgpath">浮水印圖片的實體路徑</param>  
/// <param name="rmarktext">浮水印文字(不顯示浮水印文字設為空白串)</param>  
/// <param name="rdstimgpath">輸出合成後的圖片的實體路徑</param>  
/// @整理: 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;  
     }   
   

首頁 1 2 3 末頁
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.