1 #region getThumImage 生成缩略图 2 /// <summary> 3 /// 生成缩略图 4 /// </summary> 5 /// <param name="sourceFile">原始图片文件</param> 6 /// <param name="quality">质量压缩比</param> 7 /// <param name="multiple">收缩倍数</param> 8 /// <param name="outputFile">输出文件名</param> 9 /// <param name="DPI">水平分辨率、垂直分辨率</param> 10 /// <returns>成功返回true,失败则返回false</returns> 11 public bool getThumImage(String sourceFile, long quality, int multiple, String outputFile, int DPI) 12 { 13 try 14 { 15 long imageQuality = quality; 16 Bitmap sourceImage = new Bitmap(sourceFile); 17 ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg"); 18 System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality; 19 EncoderParameters myEncoderParameters = new EncoderParameters(1); 20 EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, imageQuality); 21 myEncoderParameters.Param[0] = myEncoderParameter; 22 float xWidth = sourceImage.Width; 23 float yWidth = sourceImage.Height; 24 Bitmap newImage = new Bitmap((int)(xWidth / multiple), (int)(yWidth / multiple)); 25 newImage.SetResolution(DPI, DPI); 26 Graphics g = Graphics.FromImage(newImage); 27 28 g.DrawImage(sourceImage, 0, 0, xWidth / multiple, yWidth / multiple); 29 sourceImage.Dispose(); 30 g.Dispose(); 31 newImage.Save(outputFile, myImageCodecInfo, myEncoderParameters); 32 return true; 33 } 34 catch 35 { 36 return false; 37 } 38 } 39 #endregion getThumImage
改进后代码
1 #region getThumImage 生成缩略图 2 /// <summary> 3 /// 生成缩略图 4 /// </summary> 5 /// <param name="sourceFile">原始图片文件</param> 6 /// <param name="quality">质量压缩比</param> 7 /// <param name="multiple">收缩倍数</param> 8 /// <param name="outputFile">输出文件名</param> 9 /// <param name="DPI">水平分辨率、垂直分辨率</param> 10 /// <returns>成功返回true,失败则返回false</returns> 11 public bool getThumImage(String sourceFile, long quality, int multiple, String outputFile, int DPI) 12 { 13 try 14 { 15 long imageQuality = quality; 16 #region 用流方式来读取图片,避开内存不足问题。 17 FileStream fileStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);//打开文件 18 byte[] bytes = new byte[fileStream.Length];//读取文件的byte[] 19 fileStream.Read(bytes, 0, bytes.Length); 20 fileStream.Close(); 21 Stream stream = new MemoryStream(bytes);//把byte[]转换成Stream 22 stream.Read(bytes, 0, bytes.Length); 23 stream.Seek(0, SeekOrigin.Begin);//设置当前流的位置为流的开始 24 Bitmap sourceImage = new Bitmap((Image)new Bitmap(stream)); 25 #endregion 26 ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg"); 27 System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality; 28 EncoderParameters myEncoderParameters = new EncoderParameters(1); 29 EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, imageQuality); 30 myEncoderParameters.Param[0] = myEncoderParameter; 31 float xWidth = sourceImage.Width; 32 float yWidth = sourceImage.Height; 33 Bitmap newImage = new Bitmap((int)(xWidth / multiple), (int)(yWidth / multiple)); 34 newImage.SetResolution(DPI, DPI); 35 Graphics g = Graphics.FromImage(newImage); 36 37 g.DrawImage(sourceImage, 0, 0, xWidth / multiple, yWidth / multiple); 38 sourceImage.Dispose(); 39 g.Dispose(); 40 newImage.Save(outputFile, myImageCodecInfo, myEncoderParameters); 41 return true; 42 } 43 catch (Exception ex) 44 { 45 GC.Collect(); 46 string message = ex.Message; 47 MessageBox.Show("Error: " + message); 48 return false; 49 } 50 finally 51 { 52 GC.Collect(); 53 } 54 } 55 #endregion getThumImage
主要改进是在下面这段用流的方式来读取文件,主要可以绕过内存不足的报错
1 #region 用流方式来读取图片,避开内存不足问题。 2 FileStream fileStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);//打开文件 3 byte[] bytes = new byte[fileStream.Length];//读取文件的byte[] 4 fileStream.Read(bytes, 0, bytes.Length); 5 fileStream.Close(); 6 Stream stream = new MemoryStream(bytes);//把byte[]转换成Stream 7 stream.Read(bytes, 0, bytes.Length); 8 stream.Seek(0, SeekOrigin.Begin);//设置当前流的位置为流的开始 9 Bitmap sourceImage = new Bitmap((Image)new Bitmap(stream)); 10 #endregion
标签:multiple,高分辨率,bytes,Bitmap,sourceImage,new,byte,内存不足 From: https://www.cnblogs.com/AlbertSmith/p/17254473.html