public static class ImageFun
{
#region 圖片
public static EncoderParameters GetEncoderParas(long picquality)
{
EncoderParameters eps = new EncoderParameters(1);
Encoder ec = Encoder.Quality;
EncoderParameter ep = new EncoderParameter(ec, picquality);
eps.Param[0] = ep;
return eps;
}
public static ImageCodecInfo GetJpegCodecInfo()
{
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;
}
}
return jpegICIinfo;
}
public static System.Drawing.Image GetImage(string path)
{
FileStream fs = new FileStream(path, FileMode.Open);
Image result = Image.FromStream(fs);
fs.Close();
return result;
}
public static Bitmap GetImageFromBase64(string base64string)
{
byte[] b = Convert.FromBase64String(base64string);
MemoryStream ms = new MemoryStream(b);
Bitmap bitmap = new Bitmap(ms);
return bitmap;
}
public static string GetBase64FromImage(string imagefile)
{
string strbaser64 = "";
try
{
Bitmap bmp = new Bitmap(imagefile);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length);
ms.Close();
strbaser64 = Convert.ToBase64String(arr);
}
catch (Exception)
{
return "";
}
return strbaser64;
}
/// <summary>
/// 按照比例縮小圖片
/// </summary>
/// <param name="srcImage">要縮小的圖片</param>
/// <param name="percent">縮小比例</param>
/// <returns>縮小后的結(jié)果</returns>
public static Bitmap PercentImage(Image srcImage, double percent)
{
// 縮小后的高度
int newH = int.Parse(Math.Round(srcImage.Height * percent).ToString());
// 縮小后的寬度
int newW = int.Parse(Math.Round(srcImage.Width * percent).ToString());
try
{
// 要保存到的圖片
Bitmap b = new Bitmap(newW, newH);
Graphics g = Graphics.FromImage(b);
// 插值算法的質(zhì)量
g.InterpolationMode = InterpolationMode.Default;
g.DrawImage(srcImage, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, srcImage.Width, srcImage.Height), GraphicsUnit.Pixel);
g.Dispose();
return b;
}
catch (Exception)
{
return null;
}
}
/// <summary>
/// 按照指定大小縮放圖片
/// </summary>
/// <param name="srcImage"></param>
/// <param name="iWidth"></param>
/// <param name="iHeight"></param>
/// <returns></returns>
public static Bitmap SizeImage(Image srcImage, int iWidth, int iHeight)
{
try
{
// 要保存到的圖片
Bitmap b = new Bitmap(iWidth, iHeight);
Graphics g = Graphics.FromImage(b);
// 插值算法的質(zhì)量
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(srcImage, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(0, 0, srcImage.Width, srcImage.Height), GraphicsUnit.Pixel);
g.Dispose();
return b;
}
catch (Exception)
{
return null;
}
}
/// <summary>
/// jpeg圖片壓縮
/// </summary>
/// <param name="sFile"></param>
/// <param name="outPath"></param>
/// <param name="flag"></param>
/// <returns></returns>
public static bool GetPicThumbnail(string sFile, string outPath, int flag)
{
if (File.Exists(outPath)) File.Delete(outPath);
System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile);
ImageFormat tFormat = iSource.RawFormat;
//以下代碼為保存圖片時(shí),設(shè)置壓縮質(zhì)量
EncoderParameters ep = new EncoderParameters();
long[] qy = new long[1];
qy[0] = flag;//設(shè)置壓縮的比例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;
}
}
if (jpegICIinfo != null)
{
iSource.Save(outPath, jpegICIinfo, ep);//dFile是壓縮后的新路徑
}
else
{
iSource.Save(outPath, tFormat);
}
return true;
}
catch
{
return false;
}
finally
{
iSource.Dispose();
}
}
#endregion
}