/// <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;
}