對應代碼下載:http://download.csdn.net/source/1210790
//原始圖片 public void originalImage() { String sourcePath = @"../../Resources/test.jpg"; System.Drawing.Image image = System.Drawing.Image.FromFile(sourcePath); resultImage.Image = image; } //加文字浮水印 public void makeWordWatermark() { String sourcePath = @"../../Resources/test.jpg"; System.Drawing.Image image = System.Drawing.Image.FromFile(sourcePath); System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(image); graphics.DrawImage(image, 0, 0, image.Width, image.Height); System.Drawing.Font font = new System.Drawing.Font("Verdana", 30); System.Drawing.Brush brush = new System.Drawing.SolidBrush(System.Drawing.Color.White); string addText = "圖片添加文字浮水印效果"; graphics.DrawString(addText, font, brush, 10, 10); graphics.Dispose(); resultImage.Image = image; } //圖片整體設定透明度 public void makeImageWatermark() { String sourcePath = @"../../Resources/test.jpg"; String copyPath = @"../../Resources/image.jpg"; System.Drawing.Image image = System.Drawing.Image.FromFile(sourcePath); System.Drawing.Image copyImage = System.Drawing.Image.FromFile(copyPath); Graphics g = Graphics.FromImage(image); //透明度設定 float alpha = 0.15f; //ImageAttributes 對象包含有關在呈現時如何操作位元影像和圖元檔案顏色的資訊。 ImageAttributes imageAttributes = new ImageAttributes(); //Colormap: 定義轉換顏色的映射 ColorMap colorMap = new ColorMap(); //此處浮水印圖被定義成擁有綠色背景色的圖片被替換成透明 colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); ColorMap[] remapTable = { colorMap }; imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap); float[][] colorMatrixElements = { new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, // red紅色 new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, //green綠色 new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, //blue藍色 new float[] {0.0f, 0.0f, 0.0f, alpha, 0.0f}, //透明度 new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}}; // ColorMatrix:定義包含 RGBA 空間座標的 5 x 5 矩陣。 // ImageAttributes 類的若干方法通過使用顏色矩陣調整映像顏色。 ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); g.DrawImage(copyImage, new Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel, imageAttributes); g.Dispose(); resultImage.Image = image; }