一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

Drawing 圖片處理

 貌似小白 2013-04-19

Drawing 圖片處理

分類: Asp.Net 309人閱讀 評論(0) 收藏 舉報

[c-sharp] view plaincopy?
  1.   /// <summary>  
  2.     /// 圖片處理類  
  3.     /// </summary>  
  4.     public class PhotoUtility {  
  5.         /// <summary>  
  6.         /// 水印位置  
  7.         /// </summary>  
  8.         public enum Position {  
  9.             TopLeft  
  10.             ,  
  11.             TopRight  
  12.             ,  
  13.             TopCenter  
  14.             ,  
  15.             Middle  
  16.             ,  
  17.             BottomLeft  
  18.             ,  
  19.             BottomCenter  
  20.             ,  
  21.             BottomRight  
  22.         };  
  23.         /// <summary>      
  24.         /// 壓縮圖片    
  25.         /// </summary>  
  26.         /// <param name="sourceImage">原圖</param>  
  27.         /// <param name="filename">原圖壓縮圖片的保存文件名 </param>  
  28.         /// <param name="r">壓縮圖片大小,如果為0則不壓縮</param>  
  29.         /// <param name="WaterString">水印文字信息</param>  
  30.         /// <param name="F">水印文字字體,如果為null則系統(tǒng)設(shè)置默認(rèn)值</param>  
  31.         /// <param name="brush">筆刷 如果為null則系統(tǒng)設(shè)置為默認(rèn)值</param>  
  32.         /// <param name="p">水印位置</param>  
  33.         public static void Compress(Image sourceImage, string filename, int r, string WaterString, Font F, Brush brush, Position p)  
  34.         {  
  35.             int targetWidth = r;  
  36.             int targetHeight = r;  
  37.             //當(dāng)大于0的時候才壓縮  
  38.             if (r > 0)  
  39.             {  
  40.                 if (sourceImage.Height <= r && sourceImage.Width <= r)  
  41.                 {  
  42.                     targetHeight = sourceImage.Height;  
  43.                     targetWidth = sourceImage.Width;  
  44.                 }  
  45.                 else if (sourceImage.Height > sourceImage.Width)  
  46.                 {  
  47.                     targetWidth = sourceImage.Width * r / sourceImage.Height;  
  48.                 }  
  49.                 else  
  50.                 {  
  51.                     targetHeight = sourceImage.Height * r / sourceImage.Width;  
  52.                 }  
  53.             }  
  54.             else  
  55.             {  
  56.                 targetHeight = sourceImage.Height;  
  57.                 targetWidth = sourceImage.Width;  
  58.             }  
  59.   
  60.             //縮放圖片     
  61.             Image targetImage = Image.FromHbitmap(new Bitmap(targetWidth, targetHeight, PixelFormat.Format32bppRgb).GetHbitmap());  
  62.             Graphics g = Graphics.FromImage(targetImage);  
  63.             //設(shè)置縮略圖的平滑度及質(zhì)量  
  64.             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;  
  65.             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;  
  66.             g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;  
  67.             g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;  
  68.             g.DrawImage(sourceImage, 0, 0, targetWidth, targetHeight);  
  69.             //水印文字  
  70.             if (WaterString!=null && WaterString != string.Empty){  
  71.                 F = (F == null ? new Font("Arial Black", 28, FontStyle.Bold) : F);  
  72.                 brush = (brush == null ? Brushes.Silver : brush);  
  73.                 float WaterStringLength = (float)WaterString.Length * F.Size;  
  74.                 switch (p){                         
  75.                     case Position.Middle:  
  76.                         g.DrawString(WaterString, F, brush, (targetWidth - WaterStringLength) / 2, targetHeight / 2 - 50);  
  77.                         break;  
  78.                     case Position.TopCenter:  
  79.                         g.DrawString(WaterString, F, brush, (targetWidth - WaterStringLength) / 2, F.Size);  
  80.                         break;  
  81.                     case Position.BottomCenter:  
  82.                         g.DrawString(WaterString, F, brush, (targetWidth - WaterStringLength) / 2, targetHeight - F.Size * 2);  
  83.                         break;  
  84.                     case Position.TopLeft:  
  85.                         g.DrawString(WaterString, F, brush, 2, F.Size);  
  86.                         break;  
  87.                     case Position.BottomLeft:  
  88.                         g.DrawString(WaterString, F, brush, 2, targetHeight - F.Size * 2);  
  89.                         break;  
  90.                     default:  
  91.                         g.DrawString(WaterString, F, brush, (targetWidth - WaterStringLength) / 2, targetHeight / 2 - 50);  
  92.                         break;  
  93.                 }  
  94.             }  
  95.             g.Dispose();  
  96.             //設(shè)置輸出格式     
  97.             EncoderParameters encParams = new EncoderParameters(1);  
  98.             encParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 65L);  
  99.             ImageCodecInfo codeInfo = null;  
  100.             ImageCodecInfo[] codeInfos = ImageCodecInfo.GetImageEncoders();  
  101.             foreach (ImageCodecInfo info in codeInfos)  
  102.             {  
  103.                 if (info.MimeType.Equals("image/jpeg"))  
  104.                 {  
  105.                     codeInfo = info;  
  106.                     break;  
  107.                 }  
  108.             }  
  109.             if (codeInfo != null)  
  110.             {  
  111.                 targetImage.Save(filename, codeInfo, encParams);  
  112.             }  
  113.             targetImage.Dispose();  
  114.             sourceImage.Dispose();  
  115.         }  
  116.         /// <summary>  
  117.         /// 壓縮圖片,默認(rèn)水印為居中  
  118.         /// </summary>  
  119.         /// <param name="sourceImage">原圖</param>  
  120.         /// <param name="filename">壓縮圖片保存路徑</param>  
  121.         /// <param name="r">壓縮大小</param>  
  122.         /// <param name="WaterString">水印文字</param>  
  123.         /// <param name="F">水印文字字體,如果為null則系統(tǒng)設(shè)置默認(rèn)值</param>  
  124.         /// <param name="brush">筆刷 如果為null則系統(tǒng)設(shè)置為默認(rèn)值</param>  
  125.         public static void Compress(Image sourceImage, string filename, int r, string WaterString, Font F, Brush brush) {  
  126.             Compress(sourceImage, filename, r, WaterString, F, brush, Position.Middle);  
  127.         }  
  128.         /// <summary>  
  129.         /// 不壓縮圖片,不設(shè)置水印  
  130.         /// </summary>  
  131.         /// <param name="sourceImage">原圖</param>  
  132.         /// <param name="filename">圖片保存路徑</param>  
  133.         public static void Compress(Image sourceImage, string filename) {  
  134.             Compress(sourceImage, filename, 0, nullnullnull, Position.Middle);  
  135.         }  
  136.         /// <summary>  
  137.         /// 壓縮圖片,不設(shè)置水印  
  138.         /// </summary>  
  139.         /// <param name="sourceImage">原圖</param>  
  140.         /// <param name="filename">壓縮圖片保存路徑</param>  
  141.         /// <param name="r">壓縮大小</param>  
  142.         public static void Compress(Image sourceImage, string filename, int r){  
  143.             Compress(sourceImage, filename, r, nullnullnull, Position.Middle);  
  144.         }  
  145.         /// <summary>      
  146.         /// 生成縮略圖     
  147.         /// </summary>  
  148.         /// <param name="sourceImage">原圖</param>  
  149.         /// <param name="thumbWidth">縮略圖寬度</param>  
  150.         /// <param name="thumbHeight">縮略圖高度 </param>  
  151.         /// <param name="filename">縮略圖的保存文件名  </param>  
  152.         public static void CreateThumb(Image sourceImage, int thumbWidth, int thumbHeight, string filename) {  
  153.             int tw = thumbWidth;  
  154.             int th = thumbHeight;  
  155.             if (sourceImage.Height < th && sourceImage.Width < tw)  
  156.             {  
  157.                 th = sourceImage.Height;  
  158.                 tw = sourceImage.Width;  
  159.             }  
  160.             else if (sourceImage.Height / th > sourceImage.Width / tw)  
  161.             {  
  162.                 tw = sourceImage.Width * th / sourceImage.Height;  
  163.             }  
  164.             else  
  165.             {  
  166.                 th = sourceImage.Height * tw / sourceImage.Width;  
  167.             }  
  168.             //將圖片縮放到一個空白背景的圖片上,生成大小一致的縮略圖     
  169.             Image thumbImage = Image.FromHbitmap(new Bitmap(thumbWidth, thumbHeight, PixelFormat.Format32bppRgb).GetHbitmap());  
  170.             Graphics g = Graphics.FromImage(thumbImage);  
  171.               
  172.             //g.Clear(Color.Transparent);  
  173.             //設(shè)置縮略圖的平滑度及質(zhì)量  
  174.             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;  
  175.             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;  
  176.             g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;  
  177.             g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;  
  178.             //繪制背景  
  179.             SolidBrush brush = new SolidBrush(Color.White);  
  180.             g.FillRectangle(brush, 0, 0, thumbWidth, thumbHeight);  
  181.   
  182.             //繪圖  
  183.             g.DrawImage(sourceImage, (thumbWidth - tw) / 2, (thumbHeight - th) / 2, tw, th);  
  184.             g.Dispose();  
  185.             //保存縮略圖  
  186.             thumbImage.Save(filename, ImageFormat.Png);  
  187.             thumbImage.Dispose();  
  188.             sourceImage.Dispose();  
  189.         }  
  190.         /// <summary>  
  191.         /// 切割一個圖片  
  192.         /// </summary>  
  193.         /// <param name="sourceImage">原圖</param>  
  194.         /// <param name="X">要切割的X軸位置</param>  
  195.         /// <param name="Y">要切割的Y軸位置</param>  
  196.         /// <param name="r">切割的矩形大小</param>  
  197.         public static void Clip(Image sourceImage, int X, int Y,int Width,int Height,string filename) {  
  198.             Bitmap b = new Bitmap(sourceImage);  
  199.             Bitmap b1 = b.Clone(new Rectangle(X, Y, Width, Height), PixelFormat.Format24bppRgb);  
  200.             b1.Save(filename);  
  201.             b.Dispose();  
  202.             b1.Dispose();  
  203.         }  
  204.     }  
  205. ------------------  
  206. /// <summary>  
  207.     /// 縮略圖處理類  
  208.     /// </summary>  
  209.     public class ThumbImage  
  210.     {  
  211.         public enum CutWay  
  212.         {  
  213.             /// <summary>  
  214.             /// 指定高寬縮放(可能變形)   
  215.             /// </summary>  
  216.             HW,  
  217.             /// <summary>  
  218.             /// 指定寬,高按比例   
  219.             /// </summary>  
  220.             W,  
  221.             /// <summary>  
  222.             /// 指定高,寬按比例  
  223.             /// </summary>  
  224.             H,  
  225.             /// <summary>  
  226.             /// 指定高寬裁減(不變形)   
  227.             /// </summary>  
  228.             Cut,  
  229.             /// <summary>  
  230.             /// 等比縮放(不變形,如果高大按高,寬大按寬縮放)  
  231.             /// </summary>  
  232.             DB  
  233.         }  
  234.         /**/  
  235.         /// <summary>  
  236.         /// 生成縮略圖  
  237.         /// </summary>  
  238.         /// <param name="originalImagePath">源圖路徑(物理路徑)</param>  
  239.         /// <param name="thumbnailPath">縮略圖路徑(物理路徑)</param>  
  240.         /// <param name="width">縮略圖寬度</param>  
  241.         /// <param name="height">縮略圖高度</param>  
  242.         /// <param name="cutWay">生成縮略圖的方式</param>          
  243.         public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, CutWay cutWay)  
  244.         {  
  245.             System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);  
  246.              
  247.             int towidth = width;  
  248.             int toheight = height;  
  249.             int x = 0;  
  250.             int y = 0;  
  251.             int ow = originalImage.Width;  
  252.             int oh = originalImage.Height;  
  253.             switch (cutWay)  
  254.             {  
  255.                 case CutWay.HW://指定高寬縮放(可能變形)   
  256.                     break;  
  257.                 case CutWay.W://指定寬,高按比例   
  258.                     if ((double)originalImage.Width < (double)towidth && (double)originalImage.Height < (double)toheight)  
  259.                     {  
  260.                         towidth = originalImage.Width;  
  261.                         toheight = originalImage.Height;  
  262.                     }  
  263.                     else { toheight = originalImage.Height * width / originalImage.Width; }  
  264.                     break;  
  265.                 case CutWay.H://指定高,寬按比例  
  266.                     if ((double)originalImage.Width < (double)towidth && (double)originalImage.Height < (double)toheight)  
  267.                     {  
  268.                         towidth = originalImage.Width;  
  269.                         toheight = originalImage.Height;  
  270.                     }  
  271.                     else  
  272.                     {  
  273.                         towidth = originalImage.Width * height / originalImage.Height;  
  274.                     }  
  275.                     break;  
  276.                 case CutWay.Cut://指定高寬裁減(不變形)   
  277.                     if ((double)originalImage.Width < (double)towidth && (double)originalImage.Height < (double)toheight)  
  278.                     {  
  279.                         towidth = originalImage.Width;  
  280.                         toheight = originalImage.Height;  
  281.                     }  
  282.                     else if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)  
  283.                     {  
  284.                         oh = originalImage.Height;  
  285.                         ow = originalImage.Height * towidth / toheight;  
  286.                         y = 0;  
  287.                         x = (originalImage.Width - ow) / 2;  
  288.                     }  
  289.                     else  
  290.                     {  
  291.                         ow = originalImage.Width;  
  292.                         oh = originalImage.Width * height / towidth;  
  293.                         x = 0;  
  294.                         y = (originalImage.Height - oh) / 2;  
  295.                     }  
  296.                     break;  
  297.                 case CutWay.DB://等比縮放(不變形,如果高大按高,寬大按寬縮放)   
  298.                     if ((double)originalImage.Width<(double)towidth&&(double)originalImage.Height<(double)toheight)  
  299.                     {  
  300.                         towidth = originalImage.Width;  
  301.                         toheight = originalImage.Height;  
  302.                     }  
  303.                     else if ((double)originalImage.Width / (double)towidth < (double)originalImage.Height / (double)toheight)  
  304.                     {  
  305.                         toheight = height;  
  306.                         towidth = originalImage.Width * height / originalImage.Height;  
  307.                     }  
  308.                     else  
  309.                     {  
  310.                         towidth = width;  
  311.                         toheight = originalImage.Height * width / originalImage.Width;  
  312.                     }  
  313.                     break;  
  314.                 default:  
  315.                     break;  
  316.             }  
  317.             //新建一個bmp圖片  
  318.             System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);  
  319.             //新建一個畫板  
  320.             System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);  
  321.             //設(shè)置高質(zhì)量插值法  
  322.             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;  
  323.             //設(shè)置高質(zhì)量,低速度呈現(xiàn)平滑程度  
  324.             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;  
  325.             //清空畫布并以透明背景色填充  
  326.             g.Clear(System.Drawing.Color.Transparent);  
  327.             //在指定位置并且按指定大小繪制原圖片的指定部分  
  328.             g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),  
  329.             new System.Drawing.Rectangle(x, y, ow, oh),  
  330.             System.Drawing.GraphicsUnit.Pixel);  
  331.             try  
  332.             {  
  333.                
  334.                bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);  
  335.                      
  336.             }  
  337.             catch (System.Exception e)  
  338.             {  
  339.                 throw e;  
  340.             }  
  341.             finally  
  342.             {  
  343.                 originalImage.Dispose();  
  344.                 bitmap.Dispose();  
  345.                 g.Dispose();  
  346.             }  
  347.         }  
  348.         /**/  
  349.         /// <summary>  
  350.         /// 在圖片上增加文字水印  
  351.         /// </summary>  
  352.         /// <param name="Path">原服務(wù)器圖片路徑</param>  
  353.         /// <param name="Path_sy">生成的帶文字水印的圖片路徑</param>  
  354.         public static void AddShuiYinWord(string Path, string Path_sy)  
  355.         {  
  356.             string addText = "測試水印";  
  357.             System.Drawing.Image image = System.Drawing.Image.FromFile(Path);  
  358.             System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);  
  359.             g.DrawImage(image, 0, 0, image.Width, image.Height);  
  360.             System.Drawing.Font f = new System.Drawing.Font("Verdana", 16);  
  361.             System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Blue);  
  362.             g.DrawString(addText, f, b, 15, 15);  
  363.             g.Dispose();  
  364.             image.Save(Path_sy);  
  365.             image.Dispose();  
  366.         }  
  367.         /**/  
  368.         /// <summary>  
  369.         /// 在圖片上生成圖片水印  
  370.         /// </summary>  
  371.         /// <param name="Path">原服務(wù)器圖片路徑</param>  
  372.         /// <param name="Path_syp">生成的帶圖片水印的圖片路徑</param>  
  373.         /// <param name="Path_sypf">水印圖片路徑</param>  
  374.         public static void AddShuiYinPic(string Path, string Path_syp, string Path_sypf)  
  375.         {  
  376.             System.Drawing.Image image = System.Drawing.Image.FromFile(Path);  
  377.             System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);  
  378.             System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);  
  379.             g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);  
  380.             g.Dispose();  
  381.             image.Save(Path_syp);  
  382.             image.Dispose();  
  383.         }  
  384.         /// <summary>  
  385.         /// 獲得圖片信息  
  386.         /// </summary>  
  387.         /// <param name="Path">物理路徑</param>  
  388.         /// <returns></returns>  
  389.         public static ImageInformation GetImageInforation(string Path)  
  390.         {  
  391.             using (Image img = Image.FromFile(Path))  
  392.             {  
  393.                   
  394.                 return new ImageInformation() { Width=img.Width, Height=img.Height };  
  395.             }  
  396.         }  
  397.   
  398.          
  399.     }  
  400.     /// <summary>  
  401.     /// 獲得圖片信息  
  402.     /// </summary>  
  403.     public struct ImageInformation  
  404.     {  
  405.         /// <summary>  
  406.         /// 圖片寬  
  407.         /// </summary>  
  408.         public int Width { getset; }  
  409.         /// <summary>  
  410.         /// 圖片高  
  411.         /// </summary>  
  412.         public int Height { getset; }  
  413.     }  

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    不卡在线播放一区二区三区| 91偷拍视频久久精品| 国产精品一区日韩欧美| 绝望的校花花间淫事2| 久久99午夜福利视频| 最新国产欧美精品91| 丰满少妇被猛烈插入在线观看| 区一区二区三中文字幕| 久久亚洲成熟女人毛片| 日本少妇三级三级三级| 亚洲精品一区二区三区日韩| 91精品国产综合久久不卡| 免费在线观看欧美喷水黄片| 果冻传媒在线观看免费高清| 亚洲熟女精品一区二区成人| 日本欧美一区二区三区在线播| 欧美午夜国产在线观看| 国产成人精品一区二区三区| 欧美成人一区二区三区在线| 日韩精品一级一区二区| 中文字幕日韩精品人一妻| 国产精品一区二区三区欧美| 国产av一区二区三区麻豆| 丁香六月啪啪激情综合区| 欧美二区视频在线观看| 欧美日韩一区二区午夜| 老熟妇2久久国内精品| 国产精品流白浆无遮挡| 久久精品免费视看国产成人| 国产日韩在线一二三区| 亚洲欧美日韩国产自拍| 深夜福利欲求不满的人妻| 深夜视频在线观看免费你懂| 午夜福利国产精品不卡| 正在播放国产又粗又长| 欧美日韩国产自拍亚洲| 色婷婷成人精品综合一区| 91人妻人澡人人爽人人精品| 成人午夜免费观看视频| 久久夜色精品国产高清不卡| 国产又粗又猛又爽又黄|