C#给图片添加水印完整实例
本文实例讲述了C#给图片添加水印的方法。分享给大家供大家参考,具体如下:
usingSystem; usingSystem.Data; usingSystem.Configuration; usingSystem.Web; usingSystem.Web.Security; usingSystem.Web.UI; usingSystem.Web.UI.WebControls; usingSystem.Web.UI.WebControls.WebParts; usingSystem.Web.UI.HtmlControls; usingSystem.Drawing; usingSystem.IO; usingSystem.Drawing.Imaging; ///<summary> ///ImgWater的摘要说明 ///</summary> publicclassImgWater { publicImgWater() { // //TODO:在此处添加构造函数逻辑 // } ///<summary> ///图片水印 ///</summary> ///<paramname="ImgFile">原图文件地址</param> ///<paramname="WaterImg">水印图片地址</param> ///<paramname="sImgPath">水印图片保存地址</param> ///<paramname="Alpha">水印透明度设置</param> ///<paramname="iScale">水印图片在原图上的显示比例</param> ///<paramname="intDistance">水印图片在原图上的边距确定,以图片的右边和下边为准,当设定的边距超过一定大小后参数会自动失效</param> publicboolzzsImgWater( stringImgFile ,stringWaterImg ,stringsImgPath ,floatAlpha ,floatiScale ,intintDistance ) { try { //装载图片 FileStreamfs=newFileStream(ImgFile,FileMode.Open); BinaryReaderbr=newBinaryReader(fs); byte[]bytes=br.ReadBytes((int)fs.Length); br.Close(); fs.Close(); MemoryStreamms=newMemoryStream(bytes); System.Drawing.ImageimgPhoto=System.Drawing.Image.FromStream(ms); //System.Drawing.ImageimgPhoto=System.Drawing.Image.FromFile(ImgFile); intimgPhotoWidth=imgPhoto.Width; intimgPhotoHeight=imgPhoto.Height; System.Drawing.ImageimgWatermark=newBitmap(WaterImg); intimgWatermarkWidth=imgWatermark.Width; intimgWatermarkHeight=imgWatermark.Height; //计算水印图片尺寸 decimalaScale=Convert.ToDecimal(iScale); decimalpScale=0.05M; decimalMinScale=aScale-pScale; decimalMaxScale=aScale+pScale; intimgWatermarkWidthNew=imgWatermarkWidth; intimgWatermarkHeightNew=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); } //将原图画出来 BitmapbmPhoto=newBitmap(imgPhotoWidth,imgPhotoHeight,System.Drawing.Imaging.PixelFormat.Format24bppRgb); bmPhoto.SetResolution(72,72); GraphicsgbmPhoto=Graphics.FromImage(bmPhoto); gbmPhoto.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.High; gbmPhoto.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality; gbmPhoto.Clear(Color.White); gbmPhoto.DrawImage( imgPhoto ,newRectangle(0,0,imgPhotoWidth,imgPhotoHeight) ,0 ,0 ,imgPhotoWidth ,imgPhotoHeight ,GraphicsUnit.Pixel ); BitmapbmWatermark=newBitmap(bmPhoto); bmWatermark.SetResolution(imgPhoto.HorizontalResolution,imgPhoto.VerticalResolution); GraphicsgWatermark=Graphics.FromImage(bmWatermark); //指定高质量显示水印图片质量 gWatermark.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.High; gWatermark.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality; ImageAttributesimageAttributes=newImageAttributes(); //设置两种颜色,达到合成效果 ColorMapcolorMap=newColorMap(); 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); //用矩阵设置水印图片透明度 float[][]colorMatrixElements={ newfloat[]{1.0f,0.0f,0.0f,0.0f,0.0f}, newfloat[]{0.0f,1.0f,0.0f,0.0f,0.0f}, newfloat[]{0.0f,0.0f,1.0f,0.0f,0.0f}, newfloat[]{0.0f,0.0f,0.0f,Alpha,0.0f}, newfloat[]{0.0f,0.0f,0.0f,0.0f,1.0f} }; System.Drawing.Imaging.ColorMatrixwmColorMatrix=newSystem.Drawing.Imaging.ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(wmColorMatrix,System.Drawing.Imaging.ColorMatrixFlag.Default,System.Drawing.Imaging.ColorAdjustType.Bitmap); //确定水印边距 intxPos=imgPhotoWidth-imgWatermarkWidthNew; intyPos=imgPhotoHeight-imgWatermarkHeightNew; intxPosOfWm=0; intyPosOfWm=0; if(xPos>intDistance) xPosOfWm=xPos-intDistance; else xPosOfWm=xPos; if(yPos>intDistance) yPosOfWm=yPos-intDistance; else yPosOfWm=yPos; gWatermark.DrawImage( imgWatermark ,newRectangle(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(); returntrue; } catch { returnfalse; } } /**////<summary> ///文字水印 ///</summary> ///<paramname="ImgFile">原图文件地址</param> ///<paramname="TextFont">水印文字</param> ///<paramname="sImgPath">文字水印图片保存地址</param> ///<paramname="hScale">高度位置</param> ///<paramname="widthFont">文字块在图片中所占宽度比例</param> ///<paramname="Alpha">文字透明度其数值的范围在0到255</param> publicboolzzsTextWater( stringImgFile ,stringTextFont ,stringsImgPath ,floathScale ,floatwidthFont ,intAlpha ) { try { FileStreamfs=newFileStream(ImgFile,FileMode.Open); BinaryReaderbr=newBinaryReader(fs); byte[]bytes=br.ReadBytes((int)fs.Length); br.Close(); fs.Close(); MemoryStreamms=newMemoryStream(bytes); System.Drawing.ImageimgPhoto=System.Drawing.Image.FromStream(ms); //System.Drawing.ImageimgPhoto=System.Drawing.Image.FromFile(ImgFile); intimgPhotoWidth=imgPhoto.Width; intimgPhotoHeight=imgPhoto.Height; BitmapbmPhoto=newBitmap(imgPhotoWidth,imgPhotoHeight,System.Drawing.Imaging.PixelFormat.Format24bppRgb); bmPhoto.SetResolution(72,72); GraphicsgbmPhoto=Graphics.FromImage(bmPhoto); gbmPhoto.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.High; gbmPhoto.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality; gbmPhoto.DrawImage( imgPhoto ,newRectangle(0,0,imgPhotoWidth,imgPhotoHeight) ,0 ,0 ,imgPhotoWidth ,imgPhotoHeight ,GraphicsUnit.Pixel ); //建立字体大小的数组,循环找出适合图片的水印字体 //int[]sizes=newint[]{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=newint[]{28,26,24,20,16,14,12}; System.Drawing.FontcrFont=null; System.Drawing.SizeFcrSize=newSizeF(); for(inti=0;i<7;i++) { crFont=newFont("微软雅黑",sizes[i],FontStyle.Bold); crSize=gbmPhoto.MeasureString(TextFont,crFont); if((ushort)crSize.Width<(ushort)imgPhotoWidth*widthFont) break; } //设置水印字体的位置 //intyPixlesFromBottom=(int)(imgPhotoHeight*hScale); //floatyPosFromBottom=((imgPhotoHeight-yPixlesFromBottom)-(crSize.Height/2)); //floatxCenterOfImg=(imgPhotoWidth*1/2); floatyPosFromBottom=imgPhotoHeight*0.85f; floatxCenterOfImg=imgPhotoWidth*0.8f; //if(xCenterOfImg<crSize.Height) //xCenterOfImg=(imgPhotoWidth*(1-(crSize.Height)/imgPhotoWidth)); System.Drawing.StringFormatStrFormat=newSystem.Drawing.StringFormat(); StrFormat.Alignment=System.Drawing.StringAlignment.Center; // System.Drawing.SolidBrushsemiTransBrush2=newSystem.Drawing.SolidBrush(Color.FromArgb(Alpha,0,0,0)); gbmPhoto.DrawString( TextFont ,crFont ,semiTransBrush2 ,newSystem.Drawing.PointF(xCenterOfImg+1,yPosFromBottom+1) ,StrFormat ); System.Drawing.SolidBrushsemiTransBrush=newSystem.Drawing.SolidBrush(Color.FromArgb(Alpha,255,255,255)); //gbmPhoto.FillRectangle(semiTransBrush2,newRectangleF(newPointF(xCenterOfImg-crSize.Width/2,yPosFromBottom-4),crSize)); gbmPhoto.DrawString( TextFont ,crFont ,semiTransBrush ,newSystem.Drawing.PointF(xCenterOfImg,yPosFromBottom) ,StrFormat ); bmPhoto.Save(sImgPath,System.Drawing.Imaging.ImageFormat.Jpeg); gbmPhoto.Dispose(); imgPhoto.Dispose(); bmPhoto.Dispose(); returntrue; } catch { returnfalse; } } /**////<summary> ///文字和Logo图片水印 ///</summary> ///<paramname="ImgFile">原图文件地址</param> ///<paramname="WaterImg">水印图片地址</param> ///<paramname="TextFont">水印文字信息</param> ///<paramname="sImgPath">生存水印图片后的保存地址</param> ///<paramname="ImgAlpha">水印图片的透明度</param> ///<paramname="imgiScale">水印图片在原图上的显示比例</param> ///<paramname="intimgDistance">水印图片在原图上的边距确定,以图片的右边和下边为准,当设定的边距超过一定大小后参数会自动失效</param> ///<paramname="texthScale">水印文字高度位置,从图片底部开始计算,0-1</param> ///<paramname="textwidthFont">文字块在图片中所占宽度比例0-1</param> ///<paramname="textAlpha">文字透明度其数值的范围在0到255</param> publicvoidzzsImgTextWater( stringImgFile ,stringWaterImg ,stringTextFont ,stringsImgPath ,floatImgAlpha ,floatimgiScale ,intintimgDistance ,floattexthScale ,floattextwidthFont ,inttextAlpha ) { try { FileStreamfs=newFileStream(ImgFile,FileMode.Open); BinaryReaderbr=newBinaryReader(fs); byte[]bytes=br.ReadBytes((int)fs.Length); br.Close(); fs.Close(); MemoryStreamms=newMemoryStream(bytes); System.Drawing.ImageimgPhoto=System.Drawing.Image.FromStream(ms); //System.Drawing.ImageimgPhoto=System.Drawing.Image.FromFile(ImgFile); intimgPhotoWidth=imgPhoto.Width; intimgPhotoHeight=imgPhoto.Height; BitmapbmPhoto=newBitmap(imgPhotoWidth,imgPhotoHeight,System.Drawing.Imaging.PixelFormat.Format24bppRgb); bmPhoto.SetResolution(72,72); GraphicsgbmPhoto=Graphics.FromImage(bmPhoto); gbmPhoto.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.High; gbmPhoto.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality; gbmPhoto.DrawImage( imgPhoto ,newRectangle(0,0,imgPhotoWidth,imgPhotoHeight) ,0 ,0 ,imgPhotoWidth ,imgPhotoHeight ,GraphicsUnit.Pixel ); //建立字体大小的数组,循环找出适合图片的水印字体 int[]sizes=newint[]{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.FontcrFont=null; System.Drawing.SizeFcrSize=newSizeF(); for(inti=0;i<43;i++) { crFont=newFont("arial",sizes[i],FontStyle.Bold); crSize=gbmPhoto.MeasureString(TextFont,crFont); if((ushort)crSize.Width<(ushort)imgPhotoWidth*textwidthFont) break; } //设置水印字体的位置 intyPixlesFromBottom=(int)(imgPhotoHeight*texthScale); floatyPosFromBottom=((imgPhotoHeight-yPixlesFromBottom)-(crSize.Height/2)); floatxCenterOfImg=(imgPhotoWidth*1/2); System.Drawing.StringFormatStrFormat=newSystem.Drawing.StringFormat(); StrFormat.Alignment=System.Drawing.StringAlignment.Center; // System.Drawing.SolidBrushsemiTransBrush2=newSystem.Drawing.SolidBrush(Color.FromArgb(textAlpha,0,0,0)); gbmPhoto.DrawString( TextFont ,crFont ,semiTransBrush2 ,newSystem.Drawing.PointF(xCenterOfImg+1,yPosFromBottom+1) ,StrFormat ); System.Drawing.SolidBrushsemiTransBrush=newSystem.Drawing.SolidBrush(Color.FromArgb(textAlpha,255,255,255)); gbmPhoto.DrawString( TextFont ,crFont ,semiTransBrush ,newSystem.Drawing.PointF(xCenterOfImg,yPosFromBottom) ,StrFormat ); System.Drawing.ImageimgWatermark=newBitmap(WaterImg); intimgWatermarkWidth=imgWatermark.Width; intimgWatermarkHeight=imgWatermark.Height; //计算水印图片尺寸 decimalaScale=Convert.ToDecimal(imgiScale); decimalpScale=0.05M; decimalMinScale=aScale-pScale; decimalMaxScale=aScale+pScale; intimgWatermarkWidthNew=imgWatermarkWidth; intimgWatermarkHeightNew=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); } //将原图画出来 BitmapbmWatermark=newBitmap(bmPhoto); bmWatermark.SetResolution(imgPhoto.HorizontalResolution,imgPhoto.VerticalResolution); GraphicsgWatermark=Graphics.FromImage(bmWatermark); //指定高质量显示水印图片质量 gWatermark.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.High; gWatermark.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality; System.Drawing.Imaging.ImageAttributesimageAttributes=newSystem.Drawing.Imaging.ImageAttributes(); //设置两种颜色,达到合成效果 System.Drawing.Imaging.ColorMapcolorMap=newSystem.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); //用矩阵设置水印图片透明度 float[][]colorMatrixElements={ newfloat[]{1.0f,0.0f,0.0f,0.0f,0.0f}, newfloat[]{0.0f,1.0f,0.0f,0.0f,0.0f}, newfloat[]{0.0f,0.0f,1.0f,0.0f,0.0f}, newfloat[]{0.0f,0.0f,0.0f,ImgAlpha,0.0f}, newfloat[]{0.0f,0.0f,0.0f,0.0f,1.0f} }; System.Drawing.Imaging.ColorMatrixwmColorMatrix=newSystem.Drawing.Imaging.ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(wmColorMatrix,System.Drawing.Imaging.ColorMatrixFlag.Default,System.Drawing.Imaging.ColorAdjustType.Bitmap); //确定水印边距 intxPos=imgPhotoWidth-imgWatermarkWidthNew; intyPos=imgPhotoHeight-imgWatermarkHeightNew; intxPosOfWm=0; intyPosOfWm=0; if(xPos>intimgDistance) xPosOfWm=xPos-intimgDistance; else xPosOfWm=xPos; if(yPos>intimgDistance) yPosOfWm=yPos-intimgDistance; else yPosOfWm=yPos; gWatermark.DrawImage( imgWatermark ,newRectangle(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> ///<paramname="ImgFile">原图文件地址</param> ///<paramname="sImgPath">缩略图保存地址</param> ///<paramname="ResizeWidth">缩略图宽度</param> ///<paramname="ResizeHeight">缩略图高度</param> ///<paramname="BgColor">缩略图背景颜色,注意,背景颜色只能指定KnownColor中的值,如blue,red,green等</param> publicboolzzsResizeImg(stringImgFile,stringsImgPath,intResizeWidth,intResizeHeight,stringBgColor) { try { FileStreamfs=newFileStream(ImgFile,FileMode.Open); BinaryReaderbr=newBinaryReader(fs); byte[]bytes=br.ReadBytes((int)fs.Length); br.Close(); fs.Close(); MemoryStreamms=newMemoryStream(bytes); System.Drawing.ImageimgPhoto=System.Drawing.Image.FromStream(ms); //System.Drawing.ImageimgPhoto=System.Drawing.Image.FromFile(ImgFile); intimgPhotoWidth=imgPhoto.Width; intimgPhotoHeight=imgPhoto.Height; intstartX=0; intStartY=0; intNewWidth=0; intNewHeight=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; } //计算缩放图片尺寸 BitmapbmPhoto=newBitmap(ResizeWidth,ResizeHeight,System.Drawing.Imaging.PixelFormat.Format24bppRgb); bmPhoto.SetResolution(72,72); GraphicsgbmPhoto=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 ,newRectangle(startX,StartY,NewWidth,NewHeight) ,newRectangle(0,0,imgPhotoWidth,imgPhotoHeight) ,GraphicsUnit.Pixel ); bmPhoto.Save(sImgPath,System.Drawing.Imaging.ImageFormat.Jpeg); imgPhoto.Dispose(); gbmPhoto.Dispose(); bmPhoto.Dispose(); ms.Close(); returntrue; } catch { returnfalse; } } /**////<summary> ///图片剪切 ///</summary> ///<paramname="ImgFile">原图文件地址</param> ///<paramname="sImgPath">缩略图保存地址</param> ///<paramname="PointX">剪切起始点X坐标</param> ///<paramname="PointY">剪切起始点Y坐标</param> ///<paramname="CutWidth">剪切宽度</param> ///<paramname="CutHeight">剪切高度</param> publicboolzzsCutImg(stringImgFile,stringsImgPath,intPointX,intPointY,intCutWidth,intCutHeight) { FileStreamfs=newFileStream(ImgFile,FileMode.Open); BinaryReaderbr=newBinaryReader(fs); try { byte[]bytes=br.ReadBytes((int)fs.Length); br.Close(); fs.Close(); MemoryStreamms=newMemoryStream(bytes); System.Drawing.ImageimgPhoto=System.Drawing.Image.FromStream(ms); //System.Drawing.ImageimgPhoto=System.Drawing.Image.FromFile(ImgFile); //此处只能用filestream,用System.Drawing.Image则会报多过进程访问文件的错误,会锁定文件 BitmapbmPhoto=newBitmap(CutWidth,CutHeight,System.Drawing.Imaging.PixelFormat.Format24bppRgb); bmPhoto.SetResolution(72,72); GraphicsgbmPhoto=Graphics.FromImage(bmPhoto); gbmPhoto.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.High; gbmPhoto.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality; gbmPhoto.DrawImage( imgPhoto ,newRectangle(0,0,CutWidth,CutHeight) ,newRectangle(PointX,PointY,CutHeight,CutHeight) ,GraphicsUnit.Pixel ); bmPhoto.Save(sImgPath,System.Drawing.Imaging.ImageFormat.Jpeg); imgPhoto.Dispose(); gbmPhoto.Dispose(); bmPhoto.Dispose(); ms.Close(); returntrue; } catch { returnfalse; } finally { } } }
希望本文所述对大家C#程序设计有所帮助。