C#給圖片添加水印完整實(shí)例
本文實(shí)例講述了C#給圖片添加水印的方法。分享給大家供大家參考,具體如下:
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Drawing; using System.IO; using System.Drawing.Imaging; /// <summary> ///ImgWater 的摘要說明 /// </summary> public class ImgWater { public ImgWater() { // //TODO: 在此處添加構(gòu)造函數(shù)邏輯 // } /// <summary> /// 圖片水印 /// </summary> /// <param name="ImgFile">原圖文件地址</param> /// <param name="WaterImg">水印圖片地址</param> /// <param name="sImgPath">水印圖片保存地址</param> /// <param name="Alpha">水印透明度設(shè)置</param> /// <param name="iScale">水印圖片在原圖上的顯示比例</param> /// <param name="intDistance">水印圖片在原圖上的邊距確定,以圖片的右邊和下邊為準(zhǔn),當(dāng)設(shè)定的邊距超過一定大小后參數(shù)會自動(dòng)失效</param> public bool zzsImgWater( string ImgFile , string WaterImg , string sImgPath , float Alpha , float iScale , int intDistance ) { try { //裝載圖片 FileStream fs = new FileStream(ImgFile, FileMode.Open); BinaryReader br = new BinaryReader(fs); byte[] bytes = br.ReadBytes((int)fs.Length); br.Close(); fs.Close(); MemoryStream ms = new MemoryStream(bytes); System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms); //System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile); int imgPhotoWidth = imgPhoto.Width; int imgPhotoHeight = imgPhoto.Height; System.Drawing.Image imgWatermark = new Bitmap(WaterImg); int imgWatermarkWidth = imgWatermark.Width; int imgWatermarkHeight = imgWatermark.Height; //計(jì)算水印圖片尺寸 decimal aScale = Convert.ToDecimal(iScale); decimal pScale = 0.05M; decimal MinScale = aScale - pScale; decimal MaxScale = aScale + pScale; int imgWatermarkWidthNew = imgWatermarkWidth; int imgWatermarkHeightNew = imgWatermarkHeight; if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth >= imgPhotoHeight) if (imgWatermarkWidth > imgWatermarkHeight) if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7) <= MaxScale)) { } else { imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale); imgWatermarkHeightNew = Convert.ToInt32((imgPhotoWidth * aScale / imgWatermarkWidth) * imgWatermarkHeight); } else if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7) <= MaxScale)) { } else { imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale); imgWatermarkWidthNew = Convert.ToInt32((imgPhotoHeight * aScale / imgWatermarkHeight) * imgWatermarkWidth); } if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth >= imgWatermarkHeight) { imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale); imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight); } if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight) { imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale); imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight); } if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight) { imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale); imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth); } if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth <= imgPhotoHeight) if (imgWatermarkWidth > imgWatermarkHeight) if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7) <= MaxScale)) { } else { imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale); imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight); } else if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7) <= MaxScale)) { } else { imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale); imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth); } if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth <= imgWatermarkHeight) { imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale); imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth); } if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight) { imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale); imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight); } if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight) { imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale); imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth); } //將原圖畫出來 Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); bmPhoto.SetResolution(72, 72); Graphics gbmPhoto = Graphics.FromImage(bmPhoto); gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; gbmPhoto.Clear(Color.White); gbmPhoto.DrawImage( imgPhoto , new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight) , 0 , 0 , imgPhotoWidth , imgPhotoHeight , GraphicsUnit.Pixel ); Bitmap bmWatermark = new Bitmap(bmPhoto); bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); Graphics gWatermark = Graphics.FromImage(bmWatermark); //指定高質(zhì)量顯示水印圖片質(zhì)量 gWatermark.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; gWatermark.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; ImageAttributes imageAttributes = new ImageAttributes(); //設(shè)置兩種顏色,達(dá)到合成效果 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, System.Drawing.Imaging.ColorAdjustType.Bitmap); //用矩陣設(shè)置水印圖片透明度 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, Alpha, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f} }; System.Drawing.Imaging.ColorMatrix wmColorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(wmColorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap); //確定水印邊距 int xPos = imgPhotoWidth - imgWatermarkWidthNew; int yPos = imgPhotoHeight - imgWatermarkHeightNew; int xPosOfWm = 0; int yPosOfWm = 0; if (xPos > intDistance) xPosOfWm = xPos - intDistance; else xPosOfWm = xPos; if (yPos > intDistance) yPosOfWm = yPos - intDistance; else yPosOfWm = yPos; gWatermark.DrawImage( imgWatermark , new Rectangle(xPosOfWm, yPosOfWm, imgWatermarkWidthNew, imgWatermarkHeightNew) , 0 , 0 , imgWatermarkWidth , imgWatermarkHeight , GraphicsUnit.Pixel , imageAttributes ); imgPhoto = bmWatermark; //以jpg格式保存圖片 imgPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg); //銷毀對象 gbmPhoto.Dispose(); gWatermark.Dispose(); imgPhoto.Dispose(); imgWatermark.Dispose(); return true; } catch { return false ; } } /**//// <summary> /// 文字水印 /// </summary> /// <param name="ImgFile">原圖文件地址</param> /// <param name="TextFont">水印文字</param> /// <param name="sImgPath">文字水印圖片保存地址</param> /// <param name="hScale">高度位置</param> /// <param name="widthFont">文字塊在圖片中所占寬度比例</param> /// <param name="Alpha">文字透明度 其數(shù)值的范圍在0到255</param> public bool zzsTextWater( string ImgFile , string TextFont , string sImgPath , float hScale , float widthFont , int Alpha ) { try { FileStream fs = new FileStream(ImgFile, FileMode.Open); BinaryReader br = new BinaryReader(fs); byte[] bytes = br.ReadBytes((int)fs.Length); br.Close(); fs.Close(); MemoryStream ms = new MemoryStream(bytes); System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms); //System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile); int imgPhotoWidth = imgPhoto.Width; int imgPhotoHeight = imgPhoto.Height; Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); bmPhoto.SetResolution(72, 72); Graphics gbmPhoto = Graphics.FromImage(bmPhoto); gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; gbmPhoto.DrawImage( imgPhoto , new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight) , 0 , 0 , imgPhotoWidth , imgPhotoHeight , GraphicsUnit.Pixel ); //建立字體大小的數(shù)組,循環(huán)找出適合圖片的水印字體 //int[] sizes = new int[] { 1000, 800, 700, 650, 600, 560, 540, 500, 450, 400, 380, 360, 340, 320, 300, 280, 260, 240, 220, 200, 180, 160, 140, 120, 100, 80, 72, 64, 48, 32, 28, 26, 24, 20, 28, 16, 14, 12, 10, 8, 6, 4, 2 }; int[] sizes = new int[] { 28, 26, 24, 20, 16, 14, 12 }; System.Drawing.Font crFont = null; System.Drawing.SizeF crSize = new SizeF(); for (int i = 0; i < 7; i++) { crFont = new Font("微軟雅黑", sizes[i], FontStyle.Bold); crSize = gbmPhoto.MeasureString(TextFont, crFont); if ((ushort)crSize.Width < (ushort)imgPhotoWidth * widthFont) break; } //設(shè)置水印字體的位置 //int yPixlesFromBottom = (int)(imgPhotoHeight * hScale); //float yPosFromBottom = ((imgPhotoHeight - yPixlesFromBottom) - (crSize.Height / 2)); //float xCenterOfImg = (imgPhotoWidth * 1 / 2); float yPosFromBottom = imgPhotoHeight *0.85f; float xCenterOfImg = imgPhotoWidth * 0.8f; //if (xCenterOfImg<crSize.Height) // xCenterOfImg = (imgPhotoWidth * (1 - (crSize.Height)/ imgPhotoWidth)); System.Drawing.StringFormat StrFormat = new System.Drawing.StringFormat(); StrFormat.Alignment = System.Drawing.StringAlignment.Center; // System.Drawing.SolidBrush semiTransBrush2 = new System.Drawing.SolidBrush(Color.FromArgb(Alpha, 0, 0, 0)); gbmPhoto.DrawString( TextFont , crFont , semiTransBrush2 , new System.Drawing.PointF(xCenterOfImg + 1, yPosFromBottom + 1) , StrFormat ); System.Drawing.SolidBrush semiTransBrush = new System.Drawing.SolidBrush(Color.FromArgb(Alpha, 255, 255, 255)); //gbmPhoto.FillRectangle(semiTransBrush2, new RectangleF(new PointF(xCenterOfImg - crSize.Width / 2, yPosFromBottom - 4), crSize)); gbmPhoto.DrawString( TextFont , crFont , semiTransBrush , new System.Drawing.PointF(xCenterOfImg, yPosFromBottom) , StrFormat ); bmPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg); gbmPhoto.Dispose(); imgPhoto.Dispose(); bmPhoto.Dispose(); return true; } catch { return false; } } /**//// <summary> /// 文字和Logo圖片水印 /// </summary> /// <param name="ImgFile">原圖文件地址</param> /// <param name="WaterImg">水印圖片地址</param> /// <param name="TextFont">水印文字信息</param> /// <param name="sImgPath">生存水印圖片后的保存地址</param> /// <param name="ImgAlpha">水印圖片的透明度</param> /// <param name="imgiScale">水印圖片在原圖上的顯示比例</param> /// <param name="intimgDistance">水印圖片在原圖上的邊距確定,以圖片的右邊和下邊為準(zhǔn),當(dāng)設(shè)定的邊距超過一定大小后參數(shù)會自動(dòng)失效</param> /// <param name="texthScale">水印文字高度位置,從圖片底部開始計(jì)算,0-1</param> /// <param name="textwidthFont">文字塊在圖片中所占寬度比例 0-1</param> /// <param name="textAlpha">文字透明度 其數(shù)值的范圍在0到255</param> public void zzsImgTextWater( string ImgFile , string WaterImg , string TextFont , string sImgPath , float ImgAlpha , float imgiScale , int intimgDistance , float texthScale , float textwidthFont , int textAlpha ) { try { FileStream fs = new FileStream(ImgFile, FileMode.Open); BinaryReader br = new BinaryReader(fs); byte[] bytes = br.ReadBytes((int)fs.Length); br.Close(); fs.Close(); MemoryStream ms = new MemoryStream(bytes); System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms); //System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile); int imgPhotoWidth = imgPhoto.Width; int imgPhotoHeight = imgPhoto.Height; Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); bmPhoto.SetResolution(72, 72); Graphics gbmPhoto = Graphics.FromImage(bmPhoto); gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; gbmPhoto.DrawImage( imgPhoto , new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight) , 0 , 0 , imgPhotoWidth , imgPhotoHeight , GraphicsUnit.Pixel ); //建立字體大小的數(shù)組,循環(huán)找出適合圖片的水印字體 int[] sizes = new int[] { 1000, 800, 700, 650, 600, 560, 540, 500, 450, 400, 380, 360, 340, 320, 300, 280, 260, 240, 220, 200, 180, 160, 140, 120, 100, 80, 72, 64, 48, 32, 28, 26, 24, 20, 28, 16, 14, 12, 10, 8, 6, 4, 2 }; System.Drawing.Font crFont = null; System.Drawing.SizeF crSize = new SizeF(); for (int i = 0; i < 43; i++) { crFont = new Font("arial", sizes[i], FontStyle.Bold); crSize = gbmPhoto.MeasureString(TextFont, crFont); if ((ushort)crSize.Width < (ushort)imgPhotoWidth * textwidthFont) break; } //設(shè)置水印字體的位置 int yPixlesFromBottom = (int)(imgPhotoHeight * texthScale); float yPosFromBottom = ((imgPhotoHeight - yPixlesFromBottom) - (crSize.Height / 2)); float xCenterOfImg = (imgPhotoWidth * 1 / 2); System.Drawing.StringFormat StrFormat = new System.Drawing.StringFormat(); StrFormat.Alignment = System.Drawing.StringAlignment.Center; // System.Drawing.SolidBrush semiTransBrush2 = new System.Drawing.SolidBrush(Color.FromArgb(textAlpha, 0, 0, 0)); gbmPhoto.DrawString( TextFont , crFont , semiTransBrush2 , new System.Drawing.PointF(xCenterOfImg + 1, yPosFromBottom + 1) , StrFormat ); System.Drawing.SolidBrush semiTransBrush = new System.Drawing.SolidBrush(Color.FromArgb(textAlpha, 255, 255, 255)); gbmPhoto.DrawString( TextFont , crFont , semiTransBrush , new System.Drawing.PointF(xCenterOfImg, yPosFromBottom) , StrFormat ); System.Drawing.Image imgWatermark = new Bitmap(WaterImg); int imgWatermarkWidth = imgWatermark.Width; int imgWatermarkHeight = imgWatermark.Height; //計(jì)算水印圖片尺寸 decimal aScale = Convert.ToDecimal(imgiScale); decimal pScale = 0.05M; decimal MinScale = aScale - pScale; decimal MaxScale = aScale + pScale; int imgWatermarkWidthNew = imgWatermarkWidth; int imgWatermarkHeightNew = imgWatermarkHeight; if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth >= imgPhotoHeight) if (imgWatermarkWidth > imgWatermarkHeight) if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7) <= MaxScale)) { } else { imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale); imgWatermarkHeightNew = Convert.ToInt32((imgPhotoWidth * aScale / imgWatermarkWidth) * imgWatermarkHeight); } else if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7) <= MaxScale)) { } else { imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale); imgWatermarkWidthNew = Convert.ToInt32((imgPhotoHeight * aScale / imgWatermarkHeight) * imgWatermarkWidth); } if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth >= imgWatermarkHeight) { imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale); imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight); } if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight) { imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale); imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight); } if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight) { imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale); imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth); } if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth <= imgPhotoHeight) if (imgWatermarkWidth > imgWatermarkHeight) if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7) <= MaxScale)) { } else { imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale); imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight); } else if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7) <= MaxScale)) { } else { imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale); imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth); } if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth <= imgWatermarkHeight) { imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale); imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth); } if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight) { imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale); imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight); } if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight) { imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale); imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth); } //將原圖畫出來 Bitmap bmWatermark = new Bitmap(bmPhoto); bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); Graphics gWatermark = Graphics.FromImage(bmWatermark); //指定高質(zhì)量顯示水印圖片質(zhì)量 gWatermark.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; gWatermark.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes(); //設(shè)置兩種顏色,達(dá)到合成效果 System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap(); colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); System.Drawing.Imaging.ColorMap[] remapTable = { colorMap }; imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.ColorAdjustType.Bitmap); //用矩陣設(shè)置水印圖片透明度 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, ImgAlpha, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f} }; System.Drawing.Imaging.ColorMatrix wmColorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(wmColorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap); //確定水印邊距 int xPos = imgPhotoWidth - imgWatermarkWidthNew; int yPos = imgPhotoHeight - imgWatermarkHeightNew; int xPosOfWm = 0; int yPosOfWm = 0; if (xPos > intimgDistance) xPosOfWm = xPos - intimgDistance; else xPosOfWm = xPos; if (yPos > intimgDistance) yPosOfWm = yPos - intimgDistance; else yPosOfWm = yPos; gWatermark.DrawImage( imgWatermark , new Rectangle(xPosOfWm, yPosOfWm, imgWatermarkWidthNew, imgWatermarkHeightNew) , 0 , 0 , imgWatermarkWidth , imgWatermarkHeight , GraphicsUnit.Pixel , imageAttributes ); imgPhoto = bmWatermark; //以jpg格式保存圖片 imgPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg); //銷毀對象 gbmPhoto.Dispose(); gWatermark.Dispose(); bmPhoto.Dispose(); imgPhoto.Dispose(); imgWatermark.Dispose(); } catch { } } /**//// <summary> /// 縮略圖 /// </summary> /// <param name="ImgFile">原圖文件地址</param> /// <param name="sImgPath">縮略圖保存地址</param> /// <param name="ResizeWidth">縮略圖寬度</param> /// <param name="ResizeHeight">縮略圖高度</param> /// <param name="BgColor">縮略圖背景顏色,注意,背景顏色只能指定KnownColor中的值,如blue,red,green等</param> public bool zzsResizeImg( string ImgFile , string sImgPath , int ResizeWidth , int ResizeHeight , string BgColor ) { try { FileStream fs = new FileStream(ImgFile, FileMode.Open); BinaryReader br = new BinaryReader(fs); byte[] bytes = br.ReadBytes((int)fs.Length); br.Close(); fs.Close(); MemoryStream ms = new MemoryStream(bytes); System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms); //System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile); int imgPhotoWidth = imgPhoto.Width; int imgPhotoHeight = imgPhoto.Height; int startX = 0; int StartY = 0; int NewWidth = 0; int NewHeight = 0; if (imgPhotoWidth >= ResizeWidth && imgPhotoHeight >= ResizeHeight) { NewWidth = ResizeWidth; NewHeight = Convert.ToInt32(imgPhotoHeight * Math.Round(Convert.ToDecimal(ResizeWidth) / Convert.ToDecimal(imgPhotoWidth), 10)); startX = 0; StartY = (ResizeHeight - NewHeight) / 2; } if (ResizeWidth > imgPhotoWidth && ResizeHeight < imgPhotoHeight) { NewHeight = ResizeHeight; NewWidth = Convert.ToInt32(imgPhotoWidth * Math.Round(Convert.ToDecimal(ResizeHeight) / Convert.ToDecimal(imgPhotoHeight), 10)); startX = (ResizeWidth - NewWidth) / 2; StartY = 0; } if (ResizeWidth < imgPhotoWidth && ResizeHeight > imgPhotoHeight) { NewWidth = ResizeWidth; NewHeight = Convert.ToInt32(imgPhotoHeight * Math.Round(Convert.ToDecimal(ResizeWidth) / Convert.ToDecimal(imgPhotoWidth), 10)); startX = 0; StartY = (ResizeHeight - NewHeight) / 2; } if (imgPhotoWidth < ResizeWidth && imgPhotoHeight < ResizeHeight) { NewWidth = imgPhotoWidth; NewHeight = imgPhotoHeight; startX = (ResizeWidth - imgPhotoWidth) / 2; StartY = (ResizeHeight - imgPhotoHeight) / 2; } //計(jì)算縮放圖片尺寸 Bitmap bmPhoto = new Bitmap(ResizeWidth, ResizeHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); bmPhoto.SetResolution(72, 72); Graphics gbmPhoto = Graphics.FromImage(bmPhoto); gbmPhoto.Clear(Color.FromName(BgColor)); gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; gbmPhoto.DrawImage( imgPhoto , new Rectangle(startX, StartY, NewWidth, NewHeight) , new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight) , GraphicsUnit.Pixel ); bmPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg); imgPhoto.Dispose(); gbmPhoto.Dispose(); bmPhoto.Dispose(); ms.Close(); return true; } catch { return false; } } /**//// <summary> /// 圖片剪切 /// </summary> /// <param name="ImgFile">原圖文件地址</param> /// <param name="sImgPath">縮略圖保存地址</param> /// <param name="PointX">剪切起始點(diǎn) X坐標(biāo)</param> /// <param name="PointY">剪切起始點(diǎn) Y坐標(biāo)</param> /// <param name="CutWidth">剪切寬度</param> /// <param name="CutHeight">剪切高度</param> public bool zzsCutImg(string ImgFile, string sImgPath, int PointX, int PointY, int CutWidth, int CutHeight) { FileStream fs = new FileStream(ImgFile, FileMode.Open); BinaryReader br = new BinaryReader(fs); try { byte[] bytes = br.ReadBytes((int)fs.Length); br.Close(); fs.Close(); MemoryStream ms = new MemoryStream(bytes); System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms); //System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile); //此處只能用filestream,用 System.Drawing.Image則會報(bào)多過進(jìn)程訪問文件的錯(cuò)誤,會鎖定文件 Bitmap bmPhoto = new Bitmap(CutWidth, CutHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); bmPhoto.SetResolution(72, 72); Graphics gbmPhoto = Graphics.FromImage(bmPhoto); gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; gbmPhoto.DrawImage( imgPhoto , new Rectangle(0, 0, CutWidth, CutHeight) , new Rectangle(PointX, PointY, CutHeight, CutHeight) , GraphicsUnit.Pixel ); bmPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg); imgPhoto.Dispose(); gbmPhoto.Dispose(); bmPhoto.Dispose(); ms.Close(); return true; } catch { return false; } finally { } } }
希望本文所述對大家C#程序設(shè)計(jì)有所幫助。
上一篇:C#編程實(shí)現(xiàn)簡易圖片瀏覽器的方法
欄 目:C#教程
下一篇:C#怎么實(shí)現(xiàn)手機(jī)短信發(fā)送功能
本文標(biāo)題:C#給圖片添加水印完整實(shí)例
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6791.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)打開畫圖的同時(shí)載入圖片、最大化顯示畫圖窗體的方法
- 01-10C#編程獲取資源文件中圖片的方法
- 01-10C#獲取動(dòng)態(tài)生成的CheckBox值
- 01-10使用Nopcommerce為商城添加滿XX減XX優(yōu)惠券功能
- 01-10C#中DataGridView動(dòng)態(tài)添加行及添加列的方法
- 01-10http圖片上傳安全性問題 根據(jù)ContentType (MIME) 判斷其實(shí)不準(zhǔn)確、不
- 01-10C#將圖片存放到SQL SERVER數(shù)據(jù)庫中的方法
- 01-10C#操作數(shù)據(jù)庫中存取圖片文件的方法
- 01-10C#圖片處理3種高級應(yīng)用
- 01-10C#連接數(shù)據(jù)庫的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子