程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#圖片切割、圖片壓縮、縮略圖生成代碼匯總

C#圖片切割、圖片壓縮、縮略圖生成代碼匯總

編輯:關於C語言

本文為大家整理了C#圖片切割、圖片壓縮、縮略圖生成的實現代碼,大家可以收藏,方便以後使用,具體內容如下

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 /// 圖片切割函數 /// </summary> /// <param name="sourceFile">原始圖片文件</param> /// <param name="xNum">在X軸上的切割數量</param> /// <param name="yNum">在Y軸上的切割數量</param> /// <param name="quality">質量壓縮比</param> /// <param name="outputFile">輸出文件名,不帶後綴</param> /// <returns>成功返回true,失敗則返回false</returns> public static bool imageCut(String sourceFile, int xNum, int yNum, long quality, String outputFile) { try { long imageQuality = quality; Bitmap sourceImage = new Bitmap(sourceFile); ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg"); Encoder myEncoder = Encoder.Quality; EncoderParameters myEncoderParameters = new EncoderParameters(1); EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, imageQuality); myEncoderParameters.Param[0] = myEncoderParameter; float xWidth = sourceImage.Width / xNum; float yWidth = sourceImage.Height / yNum; String outputImage = ""; for (int countY = 0; countY < yNum; countY++) for (int countX = 0; countX < xNum; countX++) { RectangleF cloneRect = new RectangleF(countX * xWidth, countY * yWidth, xWidth, yWidth); Bitmap newImage = sourceImage.Clone(cloneRect, PixelFormat.Format24bppRgb); outputImage = outputFile + countX + countY + ".jpg"; newImage.Save(outputImage, myImageCodecInfo, myEncoderParameters); } return true; } catch { return false; } } #endregion imageCut imageCompress #region imageCompress /**//// <summary> /// 圖片壓縮函數 /// </summary> /// <param name="sourceFile">原始圖片文件</param> /// <param name="quality">質量壓縮比</param> /// <param name="ouputFile">輸出文件名,請用 .jpg 後綴 </param> /// <returns>成功返回true,失敗則返回false</returns> public static bool imageCompress(String sourceFile,long quality,String outputFile) { try { long imageQuality = quality; Bitmap sourceImage = new Bitmap(sourceFile); ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg"); Encoder myEncoder = Encoder.Quality; EncoderParameters myEncoderParameters = new EncoderParameters(1); EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, imageQuality); myEncoderParameters.Param[0] = myEncoderParameter; sourceImage.Save(outputFile, myImageCodecInfo, myEncoderParameters); return true; } catch { return false; } } #endregion imageCompress getThumImage#region getThumImage /**//// <summary> /// 生成縮略圖 /// </summary> /// <param name="sourceFile">原始圖片文件</param> /// <param name="quality">質量壓縮比</param> /// <param name="multiple">收縮倍數</param> /// <param name="outputFile">輸出文件名</param> /// <returns>成功返回true,失敗則返回false</returns> public static bool getThumImage(String sourceFile, long quality, int multiple, String outputFile) { try { long imageQuality = quality; Bitmap sourceImage = new Bitmap(sourceFile); ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg"); Encoder myEncoder = Encoder.Quality; EncoderParameters myEncoderParameters = new EncoderParameters(1); EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, imageQuality); myEncoderParameters.Param[0] = myEncoderParameter; float xWidth = sourceImage.Width; float yWidth = sourceImage.Height; Bitmap newImage = new Bitmap((int)(xWidth /multiple), (int)(yWidth /multiple)); Graphics g = Graphics.FromImage(newImage); g.DrawImage(sourceImage, 0, 0, xWidth / multiple, yWidth / multiple); g.Dispose(); newImage.Save(outputFile,myImageCodecInfo,myEncoderParameters); return true; } catch { return false; } } #endregion getThumImage ImageCodecInfo#region ImageCodecInfo /**//// <summary> /// 獲取圖片編碼信息 /// </summary> private static ImageCodecInfo GetEncoderInfo(String mimeType) { int j; ImageCodecInfo[] encoders; encoders = ImageCodecInfo.GetImageEncoders(); for (j = 0; j < encoders.Length; ++j) { if (encoders[j].MimeType == mimeType) return encoders[j]; } return null; }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved