C#给图片添加水印完整实例

 更新时间:2020年6月25日 11:25  点击:2247

本文实例讲述了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: 在此处添加构造函数逻辑
  //
 }
 /// <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 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;
   //计算水印图片尺寸
   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);
   //指定高质量显示水印图片质量
   gWatermark.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
   gWatermark.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    ImageAttributes imageAttributes = new ImageAttributes();
   //设置两种颜色,达到合成效果
   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);
     //用矩阵设置水印图片透明度
   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">文字透明度 其数值的范围在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
    );
   //建立字体大小的数组,循环找出适合图片的水印字体
   //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;
   }
   //设置水印字体的位置
   //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">水印图片在原图上的边距确定,以图片的右边和下边为准,当设定的边距超过一定大小后参数会自动失效</param>
 /// <param name="texthScale">水印文字高度位置,从图片底部开始计算,0-1</param>
 /// <param name="textwidthFont">文字块在图片中所占宽度比例 0-1</param>
 /// <param name="textAlpha">文字透明度 其数值的范围在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
    );
   //建立字体大小的数组,循环找出适合图片的水印字体
   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;
   }
   //设置水印字体的位置
   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;
   //计算水印图片尺寸
   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);
   //指定高质量显示水印图片质量
   gWatermark.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
   gWatermark.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
   System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes();
   //设置两种颜色,达到合成效果
   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);
   //用矩阵设置水印图片透明度
   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;
   }
   //计算缩放图片尺寸
   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">剪切起始点 X坐标</param>
 /// <param name="PointY">剪切起始点 Y坐标</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则会报多过进程访问文件的错误,会锁定文件
   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#程序设计有所帮助。

[!--infotagslink--]

相关文章

  • C#实现简单的登录界面

    我们在使用C#做项目的时候,基本上都需要制作登录界面,那么今天我们就来一步步看看,如果简单的实现登录界面呢,本文给出2个例子,由简入难,希望大家能够喜欢。...2020-06-25
  • 浅谈C# 字段和属性

    这篇文章主要介绍了C# 字段和属性的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下...2020-11-03
  • 使用PHP+JavaScript将HTML页面转换为图片的实例分享

    这篇文章主要介绍了使用PHP+JavaScript将HTML元素转换为图片的实例分享,文后结果的截图只能体现出替换的字体,也不能说将静态页面转为图片可以加快加载,只是这种做法比较interesting XD需要的朋友可以参考下...2016-04-19
  • C#中截取字符串的的基本方法详解

    这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
  • C#连接SQL数据库和查询数据功能的操作技巧

    本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
  • C#实现简单的Http请求实例

    这篇文章主要介绍了C#实现简单的Http请求的方法,以实例形式较为详细的分析了C#实现Http请求的具体方法,需要的朋友可以参考下...2020-06-25
  • C#中new的几种用法详解

    本文主要介绍了C#中new的几种用法,具有很好的参考价值,下面跟着小编一起来看下吧...2020-06-25
  • 使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)

    这篇文章主要介绍了使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • C#开发Windows窗体应用程序的简单操作步骤

    这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • Photoshop古装美女图片转为工笔画效果制作教程

    今天小编在这里就来给各位Photoshop的这一款软件的使用者们来说说把古装美女图片转为细腻的工笔画效果的制作教程,各位想知道方法的使用者们,那么下面就快来跟着小编一...2016-09-14
  • Python 图片转数组,二进制互转操作

    这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
  • C#和JavaScript实现交互的方法

    最近做一个小项目不可避免的需要前端脚本与后台进行交互。由于是在asp.net中实现,故问题演化成asp.net中jiavascript与后台c#如何进行交互。...2020-06-25
  • php抓取网站图片并保存的实现方法

    php如何实现抓取网页图片,相较于手动的粘贴复制,使用小程序要方便快捷多了,喜欢编程的人总会喜欢制作一些简单有用的小软件,最近就参考了网上一个php抓取图片代码,封装了一个php远程抓取图片的类,测试了一下,效果还不错分享...2015-10-30
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • 轻松学习C#的基础入门

    轻松学习C#的基础入门,了解C#最基本的知识点,C#是一种简洁的,类型安全的一种完全面向对象的开发语言,是Microsoft专门基于.NET Framework平台开发的而量身定做的高级程序设计语言,需要的朋友可以参考下...2020-06-25
  • C#变量命名规则小结

    本文主要介绍了C#变量命名规则小结,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-09
  • C#绘制曲线图的方法

    这篇文章主要介绍了C#绘制曲线图的方法,以完整实例形式较为详细的分析了C#进行曲线绘制的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • c#自带缓存使用方法 c#移除清理缓存

    这篇文章主要介绍了c#自带缓存使用方法,包括获取数据缓存、设置数据缓存、移除指定数据缓存等方法,需要的朋友可以参考下...2020-06-25