c#圖片處理大全

 

  1. /// <summary>  
  2. /// 獲取一個圖片按等比例縮小後的大小。  
  3. /// </summary>  
  4. /// <param name="maxWidth">需要縮小到的寬度</param>  
  5. /// <param name="maxHeight">需要縮小到的高度</param>  
  6. /// <param name="p_w_picpathOriginalWidth">圖片的原始寬度</param>  
  7. /// <param name="p_w_picpathOriginalHeight">圖片的原始高度</param>  
  8. /// <returns>返回圖片按等比例縮小後的實際大小</returns>  
  9. public static Size GetNewSize(int maxWidth, int maxHeight, int p_w_picpathOriginalWidth, int p_w_picpathOriginalHeight)  
  10. {  
  11.     double w = 0.0;  
  12.     double h = 0.0;  
  13.     double sw = Convert.ToDouble(p_w_picpathOriginalWidth);  
  14.     double sh = Convert.ToDouble(p_w_picpathOriginalHeight);  
  15.     double mw = Convert.ToDouble(maxWidth);  
  16.     double mh = Convert.ToDouble(maxHeight);  
  17.  
  18.     if (sw < mw && sh < mh)  
  19.     {  
  20.         w = sw;  
  21.         h = sh;  
  22.     }  
  23.     else if ((sw / sh) > (mw / mh))  
  24.     {  
  25.         w = maxWidth;  
  26.         h = (w * sh) / sw;  
  27.     }  
  28.     else 
  29.     {  
  30.         h = maxHeight;  
  31.         w = (h * sw) / sh;  
  32.     }  
  33.  
  34.     return new Size(Convert.ToInt32(w), Convert.ToInt32(h));  
  35. }  
  36.  
  37. /// <summary>  
  38. /// 對給定的一個圖片(Image對象)生成一個指定大小的縮略圖。  
  39. /// </summary>  
  40. /// <param name="originalImage">原始圖片</param>  
  41. /// <param name="thumMaxWidth">縮略圖的寬度</param>  
  42. /// <param name="thumMaxHeight">縮略圖的高度</param>  
  43. /// <returns>返回縮略圖的Image對象</returns>  
  44. public static System.Drawing.Image GetThumbNailImage(System.Drawing.Image originalImage, int thumMaxWidth, int thumMaxHeight)  
  45. {  
  46.     Size thumRealSize = Size.Empty;  
  47.     System.Drawing.Image newImage = originalImage;  
  48.     Graphics graphics = null;  
  49.  
  50.     try 
  51.     {  
  52.         thumRealSize = GetNewSize(thumMaxWidth, thumMaxHeight, originalImage.Width, originalImage.Height);  
  53.         newImage = new Bitmap(thumRealSize.Width, thumRealSize.Height);  
  54.         graphics = Graphics.FromImage(newImage);  
  55.  
  56.         graphics.CompositingQuality = CompositingQuality.HighQuality;  
  57.         graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;  
  58.         graphics.SmoothingMode = SmoothingMode.HighQuality;  
  59.  
  60.         graphics.Clear(Color.Transparent);  
  61.  
  62.         graphics.DrawImage(originalImage, new Rectangle(0, 0, thumRealSize.Width, thumRealSize.Height), new Rectangle(0, 0, originalImage.Width, originalImage.Height), GraphicsUnit.Pixel);  
  63.     }  
  64.     catch { }  
  65.     finally 
  66.     {  
  67.         if (graphics != null)  
  68.         {  
  69.             graphics.Dispose();  
  70.             graphics = null;  
  71.         }  
  72.     }  
  73.  
  74.     return newImage;  
  75. }  
  76. /// <summary>  
  77. /// 對給定的一個圖片文件生成一個指定大小的縮略圖。  
  78. /// </summary>  
  79. /// <param name="originalImage">圖片的物理文件地址</param>  
  80. /// <param name="thumMaxWidth">縮略圖的寬度</param>  
  81. /// <param name="thumMaxHeight">縮略圖的高度</param>  
  82. /// <returns>返回縮略圖的Image對象</returns>  
  83. public static System.Drawing.Image GetThumbNailImage(string p_w_picpathFile, int thumMaxWidth, int thumMaxHeight)  
  84. {  
  85.     System.Drawing.Image originalImage = null;  
  86.     System.Drawing.Image newImage = null;  
  87.  
  88.     try 
  89.     {  
  90.         originalImage = System.Drawing.Image.FromFile(p_w_picpathFile);  
  91.         newImage = GetThumbNailImage(originalImage, thumMaxWidth, thumMaxHeight);  
  92.     }  
  93.     catch { }  
  94.     finally 
  95.     {  
  96.         if (originalImage != null)  
  97.         {  
  98.             originalImage.Dispose();  
  99.             originalImage = null;  
  100.         }  
  101.     }  
  102.  
  103.     return newImage;  
  104. }  
  105. /// <summary>  
  106. /// 對給定的一個圖片文件生成一個指定大小的縮略圖,並將縮略圖保存到指定位置。  
  107. /// </summary>  
  108. /// <param name="originalImageFile">圖片的物理文件地址</param>  
  109. /// <param name="thumbNailImageFile">縮略圖的物理文件地址</param>  
  110. /// <param name="thumMaxWidth">縮略圖的寬度</param>  
  111. /// <param name="thumMaxHeight">縮略圖的高度</param>  
  112. public static void MakeThumbNail(string originalImageFile, string thumbNailImageFile, int thumMaxWidth, int thumMaxHeight)  
  113. {  
  114.     System.Drawing.Image newImage = GetThumbNailImage(originalImageFile, thumMaxWidth, thumMaxHeight);  
  115.     try 
  116.     {  
  117.         newImage.Save(thumbNailImageFile, ImageFormat.Jpeg);  
  118.     }  
  119.     catch 
  120.     { }  
  121.     finally 
  122.     {  
  123.         newImage.Dispose();  
  124.         newImage = null;  
  125.     }  
  126. }  
  127. /// <summary>  
  128. /// 將一個圖片的內存流調整爲指定大小,並返回調整後的內存流。  
  129. /// </summary>  
  130. /// <param name="originalImageStream">原始圖片的內存流</param>  
  131. /// <param name="newWidth">新圖片的寬度</param>  
  132. /// <param name="newHeight">新圖片的高度</param>  
  133. /// <returns>返回調整後的圖片的內存流</returns>  
  134. public static MemoryStream ResizeImage(Stream originalImageStream, int newWidth, int newHeight)  
  135. {  
  136.     MemoryStream newImageStream = null;  
  137.  
  138.     System.Drawing.Image newImage = Globals.GetThumbNailImage(System.Drawing.Image.FromStream(originalImageStream), newWidth, newHeight);  
  139.     if (newImage != null)  
  140.     {  
  141.         newImageStream = new MemoryStream();  
  142.         newImage.Save(newImageStream, ImageFormat.Jpeg);  
  143.     }  
  144.  
  145.     return newImageStream;  
  146. }  
  147. /// <summary>  
  148. /// 將一個內存流保存爲磁盤文件。  
  149. /// </summary>  
  150. /// <param name="stream">內存流</param>  
  151. /// <param name="newFile">目標磁盤文件地址</param>  
  152. public static void SaveStreamToFile(Stream stream, string newFile)  
  153. {  
  154.     if (stream == null || stream.Length == 0 || string.IsNullOrEmpty(newFile))  
  155.     {  
  156.         return;  
  157.     }  
  158.  
  159.     byte[] buffer = new byte[stream.Length];  
  160.     stream.Position = 0;  
  161.     stream.Read(buffer, 0, buffer.Length);  
  162.     FileStream fileStream = new FileStream(newFile, FileMode.OpenOrCreate, FileAccess.Write);  
  163.     fileStream.Write(buffer, 0, buffer.Length);  
  164.     fileStream.Flush();  
  165.     fileStream.Close();  
  166.     fileStream.Dispose();  
  167. }  
  168. /// <summary>  
  169. /// 對一個指定的圖片加上圖片水印效果。  
  170. /// </summary>  
  171. /// <param name="p_w_picpathFile">圖片文件地址</param>  
  172. /// <param name="waterImage">水印圖片(Image對象)</param>  
  173. public static void CreateImageWaterMark(string p_w_picpathFile, System.Drawing.Image waterImage)  
  174. {  
  175.     if (string.IsNullOrEmpty(p_w_picpathFile) || !File.Exists(p_w_picpathFile) || waterImage == null)  
  176.     {  
  177.         return;  
  178.     }  
  179.  
  180.     System.Drawing.Image originalImage = System.Drawing.Image.FromFile(p_w_picpathFile);  
  181.  
  182.     if (originalImage.Width - 10 < waterImage.Width || originalImage.Height - 10 < waterImage.Height)  
  183.     {  
  184.         return;  
  185.     }  
  186.  
  187.     Graphics graphics = Graphics.FromImage(originalImage);  
  188.  
  189.     int x = originalImage.Width - waterImage.Width - 10;  
  190.     int y = originalImage.Height - waterImage.Height - 10;  
  191.     int width = waterImage.Width;  
  192.     int height = waterImage.Height;  
  193.  
  194.     graphics.DrawImage(waterImage, new Rectangle(x, y, width, height), 0, 0, width, height, GraphicsUnit.Pixel);  
  195.     graphics.Dispose();  
  196.  
  197.     MemoryStream stream = new MemoryStream();  
  198.     originalImage.Save(stream, ImageFormat.Jpeg);  
  199.     originalImage.Dispose();  
  200.  
  201.     System.Drawing.Image p_w_picpathWithWater = System.Drawing.Image.FromStream(stream);  
  202.  
  203.     p_w_picpathWithWater.Save(p_w_picpathFile);  
  204.     p_w_picpathWithWater.Dispose();  
  205. }  
  206. /// <summary>  
  207. /// 對一個指定的圖片加上文字水印效果。  
  208. /// </summary>  
  209. /// <param name="p_w_picpathFile">圖片文件地址</param>  
  210. /// <param name="waterText">水印文字內容</param>  
  211. public static void CreateTextWaterMark(string p_w_picpathFile, string waterText)  
  212. {  
  213.     if (string.IsNullOrEmpty(p_w_picpathFile) || string.IsNullOrEmpty(waterText) || !File.Exists(p_w_picpathFile))  
  214.     {  
  215.         return;  
  216.     }  
  217.  
  218.     System.Drawing.Image originalImage = System.Drawing.Image.FromFile(p_w_picpathFile);  
  219.  
  220.     Graphics graphics = Graphics.FromImage(originalImage);  
  221.  
  222.     graphics.SmoothingMode = SmoothingMode.HighQuality;  
  223.     graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;  
  224.     graphics.CompositingQuality = CompositingQuality.HighQuality;  
  225.     graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;  
  226.  
  227.     SolidBrush brush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));  
  228.     Font waterTextFont = new Font("Arial", 16, FontStyle.Regular);  
  229.     SizeF waterTextSize = graphics.MeasureString(waterText, waterTextFont);  
  230.  
  231.     float x = (float)originalImage.Width - waterTextSize.Width - 10F;  
  232.     float y = (float)originalImage.Height - waterTextSize.Height - 10F;  
  233.  
  234.     graphics.DrawString(waterText, waterTextFont, brush, x, y);  
  235.  
  236.     graphics.Dispose();  
  237.     brush.Dispose();  
  238.  
  239.     MemoryStream stream = new MemoryStream();  
  240.     originalImage.Save(stream, ImageFormat.Jpeg);  
  241.     originalImage.Dispose();  
  242.  
  243.     System.Drawing.Image p_w_picpathWithWater = System.Drawing.Image.FromStream(stream);  
  244.  
  245.     p_w_picpathWithWater.Save(p_w_picpathFile);  
  246.     p_w_picpathWithWater.Dispose();  
  247. }  
  248.  
  249. /// <summary>  
  250. /// 判斷上傳組件是否包含內容。  
  251. /// </summary>  
  252. /// <param name="fileUpload">ASP.NET 2.0標準上傳組件</param>  
  253. /// <returns>如果數據有效,則返回True,否則返回False</returns>  
  254. public static bool IsAttachmentValid(FileUpload fileUpload)  
  255. {  
  256.     if (fileUpload != null &&  
  257.         fileUpload.PostedFile != null &&  
  258.         !string.IsNullOrEmpty(fileUpload.PostedFile.FileName) &&  
  259.         fileUpload.PostedFile.ContentLength > 0)  
  260.     {  
  261.         return true;  
  262.     }  
  263.     return false;  
  264. }  
  265.   

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章