亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > C# > 正文

C#給圖片添加水印完整實例

2020-01-24 01:20:19
字體:
來源:轉載
供稿:網友

本文實例講述了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#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
亚洲xxxx做受欧美| 亚洲午夜av久久乱码| 国产偷国产偷亚洲清高网站| 久久国产精品影视| 久热精品视频在线免费观看| 国产亚洲欧洲高清一区| 日韩成人黄色av| 久久国产精品视频| 日本精品va在线观看| 国产精品中文久久久久久久| 456国产精品| 亚洲成人网av| 毛片精品免费在线观看| 95av在线视频| 伊人久久大香线蕉av一区二区| 韩国美女主播一区| 国产999视频| 日韩精品极品视频| 91成人免费观看网站| 日韩激情av在线免费观看| 最新中文字幕亚洲| 国产亚洲欧美日韩美女| 久久精品99国产精品酒店日本| 一本色道久久综合狠狠躁篇怎么玩| 精品香蕉在线观看视频一| 亚洲美女又黄又爽在线观看| 国产不卡av在线免费观看| 国产在线视频欧美| 国产中文日韩欧美| 性亚洲最疯狂xxxx高清| 国产精品亚洲美女av网站| 午夜精品久久久久久久99黑人| 久久久之久亚州精品露出| 亚洲精品福利视频| 欧美日韩美女视频| 色综合导航网站| 4444欧美成人kkkk| 日韩免费av片在线观看| 久久在线视频在线| 国产精品自拍偷拍视频| 4k岛国日韩精品**专区| 国产精品免费一区豆花| 欧美日韩亚洲视频一区| 欧美黑人狂野猛交老妇| 免费97视频在线精品国自产拍| 91亚洲国产成人精品性色| 亚洲日本aⅴ片在线观看香蕉| 国产成人jvid在线播放| 视频一区视频二区国产精品| 久久久亚洲成人| 国内精品久久久久影院 日本资源| 成人久久久久久久| 国产aⅴ夜夜欢一区二区三区| 国产日韩在线观看av| 久久久久国产精品免费网站| 久久国产精品久久久| 欧美成人中文字幕在线| 97国产在线视频| 亚洲国产天堂久久综合网| 中文字幕在线精品| 欧美午夜精品久久久久久浪潮| yw.139尤物在线精品视频| 九九热r在线视频精品| 国产精品视频免费观看www| 国产成人小视频在线观看| 7777免费精品视频| 国产亚洲欧美日韩一区二区| 欧洲日本亚洲国产区| 神马国产精品影院av| 国模精品视频一区二区三区| 日韩一区视频在线| 亚洲午夜未删减在线观看| 欧美电影免费观看高清完整| 国产中文日韩欧美| 国产精品99久久久久久www| 国产精品第七十二页| 福利一区福利二区微拍刺激| 国内精品久久久久影院优| 91亚洲精品一区二区| 国产精品igao视频| 亚洲精品久久久久中文字幕二区| 久久国产精品视频| 欧洲成人在线观看| 亚洲白虎美女被爆操| 久久国产精品久久精品| 亚洲视频在线观看网站| 日本精品va在线观看| 国产欧美精品一区二区三区-老狼| 亚洲日韩中文字幕| 国产精品福利久久久| 欧美精品videossex性护士| 久久久免费观看视频| 日韩欧美亚洲综合| 国产欧美韩国高清| 色综合久久久久久中文网| 91亚洲精华国产精华| 日韩免费中文字幕| 国产精品一区二区久久国产| 欧美在线视频一区| 国产一区视频在线播放| 中文字幕日韩欧美在线视频| 久久精品久久久久| 国产一区二区三区精品久久久| 九九热视频这里只有精品| 一区二区欧美激情| 国产精品99久久久久久久久久久久| 成人在线视频网| 夜夜嗨av一区二区三区四区| 欧美精品videosex性欧美| 成人做爰www免费看视频网站| 欧美视频在线观看 亚洲欧| 最近中文字幕2019免费| 中文字幕在线看视频国产欧美在线看完整| 午夜精品视频网站| 欧美激情一级欧美精品| 国产成人综合精品在线| 国产精品视频一区二区高潮| 91免费人成网站在线观看18| 亚洲第一福利在线观看| 国产精品免费看久久久香蕉| 日韩精品免费电影| 日韩精品在线影院| 亚洲欧洲国产一区| 精品国产精品三级精品av网址| 国产成人高潮免费观看精品| 亚洲a级在线播放观看| 中文字幕成人精品久久不卡| 久久天天躁夜夜躁狠狠躁2022| 国外成人在线直播| 久久久久久久久久久91| 亚洲一区美女视频在线观看免费| 国产欧美日韩视频| 国产日韩av高清| 精品呦交小u女在线| 中文字幕国产亚洲| 美女av一区二区| 精品女同一区二区三区在线播放| 久久久久九九九九| 亚洲自拍另类欧美丝袜| 国产精品羞羞答答| 国产欧美精品日韩| 成人美女免费网站视频| 色偷偷88888欧美精品久久久| 91精品国产91久久久久久吃药| 欧洲亚洲在线视频| 成人av资源在线播放| 77777少妇光屁股久久一区| 久久亚洲精品小早川怜子66| 国产成人精品一区二区在线| 国产精品88a∨| 日韩亚洲欧美中文高清在线| 国产精品一区二区三区在线播放| 国产精品白嫩初高中害羞小美女| 久久人人爽人人爽人人片av高清| 成人国产精品久久久| 国产有码一区二区| 久热精品视频在线免费观看| 中文字幕不卡在线视频极品| 91精品国产99| 国产综合色香蕉精品| 亚洲成人中文字幕| 中文字幕少妇一区二区三区| 国产精品视频久久| 亚洲美女av在线|