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; /**/ /// <summary>/// ImgWater 的摘要说明/// </summary> public class ImgWater ... { /**//// <summary> /// 图片水印 /// </summary> /// <param name="ImgFile">原图文件地址</param> /// <param name="WaterImg">水印图片地址</param> /// <param name="sImgPath">水印图片保存地址</param> /// <param name="Alpha">水印透明度设置</param> /// <param name="iScale">水印图片在原图上的显示比例</param> /// <param name="intDistance">水印图片在原图上的边距确定,以图片的右边和下边为准,当设定的边距超过一定大小后参数会自动失效</param> public void 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; //计算水印图片尺寸 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