#region GetPicThumbnail
/// <summary>
/// 无损压缩图片
/// </summary>
/// <param name="sm">文件流</param>
/// <param name="flag">压缩质量 1-100</param>
/// <returns></returns>
public static byte[] GetPicThumbnail(Stream sm, int flag)
{
Image iSource = Image.FromStream(sm);
//获取图片 EXIF信息 如果图片旋转了 在给转回来
var dic = ReadExif(iSource);
if (dic.Count > 0 && dic["Orientation"] == "6")
{
iSource.RotateFlip(RotateFlipType.Rotate90FlipX);
}
ImageFormat tFormat = iSource.RawFormat;
int sW, sH;
//按比例缩放
Size temsize = new Size(iSource.Width, iSource.Height);
int dWidth = temsize.Width;
int dHeight = temsize.Height;
Bitmap ob = new Bitmap(dWidth, dHeight);
Graphics g = Graphics.FromImage(ob);
g.Clear(Color.WhiteSmoke);
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(iSource, new Rectangle(0, 0, iSource.Width, iSource.Height), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
g.Dispose();
//以下代码为保存图片时,设置压缩质量
EncoderParameters ep = new EncoderParameters();
long[] qy = new long[1];
qy[0] = flag;//设置压缩的比例1-100
EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[0] = eParam;
try
{
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICIinfo = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICIinfo = arrayICI[x];
break;
}
}
byte[] bytes;
if (jpegICIinfo != null)
{
MemoryStream ms = new MemoryStream();
ob.Save(ms, jpegICIinfo, ep);//dFile是压缩后的新路径
bytes = ms.GetBuffer(); //byte[] bytes= ms.ToArray(); 这两句都可以,至于区别么,下面有解释
ms.Close();
}
else
{
MemoryStream ms = new MemoryStream();
ob.Save(ms, tFormat);
bytes = ms.GetBuffer(); //byte[] bytes= ms.ToArray(); 这两句都可以,至于区别么,下面有解释
ms.Close();
}
return bytes;
}
catch
{
return null;
}
finally
{
iSource.Dispose();
ob.Dispose();
}
}
/// <summary>
/// 获取图片EXIF信息
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
private static Dictionary<string, string> ReadExif(Image image)
{
var exif = new Dictionary<string, string>();
var properties = image.PropertyItems;
foreach (var property in properties)
{
switch (property.Id)
{
case 0x010E:
exif["ImageTitle"] = ASCIIToString(property.Value);
break;
case 0x010F:
exif["Make"] = ASCIIToString(property.Value);
break;
case 0x0110:
exif["Model"] = ASCIIToString(property.Value);
break;
case 0x0112:
exif["Orientation"] = ShortToString(property.Value, 0);
break;
case 0x011A:
exif["XResolution"] = RationalToSingle(property.Value, 0);
break;
case 0x011B:
exif["YResolution"] = RationalToSingle(property.Value, 0);
break;
case 0x0128:
exif["ResolutionUnit"] = ShortToString(property.Value, 0);
break;
case 0x0131:
exif["Software"] = ASCIIToString(property.Value);
break;
case 0x0132:
exif["DateTime"] = ASCIIToString(property.Value);
break;
//GPS
case 0x0002:
exif["GPSLatitude"] = string.Format("{0}°{1}′{2}″",
RationalToSingle(property.Value, 0),
RationalToSingle(property.Value, 8),
RationalToSingle(property.Value, 16)
);
break;
case 0x0004:
exif["GPSLongitude"] = string.Format("{0}°{1}′{2}″",
RationalToSingle(property.Value, 0),
RationalToSingle(property.Value, 8),
RationalToSingle(property.Value, 16)
);
break;
case 0x0006:
exif["GPSAltitude"] = RationalToSingle(property.Value, 0);
break;
}
}
return exif;
}
#region tostring方法
static string ByteToString(byte[] b, int startindex)
{
if (startindex + 1 <= b.Length)
return ((char)b[startindex]).ToString();
else
return string.Empty;
}
static string ShortToString(byte[] b, int startindex)
{
if (startindex + 2 <= b.Length)
return BitConverter.ToInt16(b, startindex).ToString();
else
return string.Empty;
}
static string RationalToSingle(byte[] b, int startindex)
{
if (startindex + 8 <= b.Length)
return (BitConverter.ToSingle(b, startindex) / BitConverter.ToSingle(b, startindex + 4)).ToString();
else
return string.Empty;
}
static string ASCIIToString(byte[] b)
{
return Encoding.ASCII.GetString(b);
}
#endregion
#endregion
网友评论