|
|
@ -1,13 +1,66 @@
|
|
|
|
using System;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
|
|
|
using System.Drawing.Imaging;
|
|
|
|
using System.IO;
|
|
|
|
using System.IO;
|
|
|
|
using System.Text;
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
using Encoder = System.Drawing.Imaging.Encoder;
|
|
|
|
|
|
|
|
|
|
|
|
namespace SlnMesnac.Common
|
|
|
|
namespace SlnMesnac.Common
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public static class FileHelper
|
|
|
|
public static class FileHelper
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
#region
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
///quality: 0-100 ,越小压缩程度也越高,但是图片质量越低
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <param name="imageData"></param>
|
|
|
|
|
|
|
|
/// <param name="quality"></param>
|
|
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
|
|
public static byte[] CompressImageData(byte[] imageData, long quality)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
using (MemoryStream memoryStream = new MemoryStream(imageData))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
using (Image image = Image.FromStream(memoryStream))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
using (MemoryStream compressedStream = new MemoryStream())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Encoder myEncoder = Encoder.Quality;
|
|
|
|
|
|
|
|
EncoderParameters myEncoderParameters = new EncoderParameters(1);
|
|
|
|
|
|
|
|
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, quality);
|
|
|
|
|
|
|
|
myEncoderParameters.Param[0] = myEncoderParameter;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
image.Save(compressedStream, jpgEncoder, myEncoderParameters);
|
|
|
|
|
|
|
|
return compressedStream.ToArray();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Console.WriteLine($"压缩图片出错:{ex.Message}");
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static ImageCodecInfo GetEncoder(ImageFormat format)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (ImageCodecInfo codec in codecs)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (codec.FormatID == format.Guid)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return codec;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// 查找指定目录下的文件
|
|
|
|
/// 查找指定目录下的文件
|
|
|
|