/// <summary>
/// 通过图片存储的物理路径以指定的宽高压缩图片并保存到指定的路径
/// </summary>
/// <param name="imgPath">图片物理路径</param>
/// <param name="savePath">图片保存路径</param>
/// <param name="intWidth">压缩的宽度</param>
/// <param name="intHeight">压缩的高度</param>
/// <returns></returns>
public void CompressImageByPath(string imgPath, string savePath, int imgWidth, int imgHeight)
{
FileStream fs = System.IO.File.OpenRead(imgPath);
byte[] bigImg = new byte[fs.Length];
fs.Read(bigImg, 0, bigImg.Length);
fs.Close();
if (bigImg != null && bigImg.Count() != 0)
{
using (System.IO.MemoryStream stream = new System.IO.MemoryStream(bigImg))
{
System.Drawing.Image big = System.Drawing.Image.FromStream(stream);
if (((float)big.Width / big.Height) < ((float)imgWidth / imgHeight))
{
imgHeight = big.Height * imgWidth / big.Width;
}
else if (((float)big.Width / big.Height) > ((float)imgWidth / imgHeight))
{
imgWidth = big.Width * imgHeight / big.Height;
}
else if ((float)(big.Width / big.Height) == (imgWidth / imgHeight) && ((float)big.Height * imgWidth / big.Width) > ((float)big.Width * imgHeight / big.Height))
{
imgHeight = big.Height * imgWidth / big.Width;
}
else
{
imgWidth = big.Width * imgHeight / big.Height;
}
System.Drawing.Size size = new Size(imgHeight, imgHeight);
System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Transparent);
g.DrawImage(big, new System.Drawing.Rectangle(0, 0, size.Width, size.Height), new System.Drawing.Rectangle(0, 0, big.Width, big.Height), System.Drawing.GraphicsUnit.Pixel);
bitmap.Save(savePath, ImageFormat.Jpeg);
bitmap.Dispose();
big.Dispose();
g.Dispose();
}
}
}
/// <summary>
/// 以指定的宽高压缩图片并保存到指定的路径
/// </summary>
/// <param name="big">图片</param>
/// <param name="savePath">图片保存路径</param>
/// <param name="intWidth">压缩的宽度</param>
/// <param name="intHeight">压缩的高度</param>
/// <returns></returns>
public void CompressImage(Image big, string savePath, int imgWidth, int imgHeight)
{
if (((float)big.Width / big.Height) < ((float)imgWidth / imgHeight))
{
imgHeight = big.Height * imgWidth / big.Width;
}
else if (((float)big.Width / big.Height) > ((float)imgWidth / imgHeight))
{
imgWidth = big.Width * imgHeight / big.Height;
}
else if ((float)(big.Width / big.Height) == (imgWidth / imgHeight) && ((float)big.Height * imgWidth / big.Width) > ((float)big.Width * imgHeight / big.Height))
{
imgHeight = big.Height * imgWidth / big.Width;
}
else
{
imgWidth = big.Width * imgHeight / big.Height;
}
System.Drawing.Size size = new Size(imgWidth, imgHeight);
System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Transparent);
g.DrawImage(big, new System.Drawing.Rectangle(0, 0, size.Width, size.Height), new System.Drawing.Rectangle(0, 0, big.Width, big.Height), System.Drawing.GraphicsUnit.Pixel);
bitmap.Save(savePath, ImageFormat.Jpeg);
bitmap.Dispose();
g.Dispose();
}
/// 通过图片存储的物理路径以指定的宽高压缩图片并保存到指定的路径
/// </summary>
/// <param name="imgPath">图片物理路径</param>
/// <param name="savePath">图片保存路径</param>
/// <param name="intWidth">压缩的宽度</param>
/// <param name="intHeight">压缩的高度</param>
/// <returns></returns>
public void CompressImageByPath(string imgPath, string savePath, int imgWidth, int imgHeight)
{
FileStream fs = System.IO.File.OpenRead(imgPath);
byte[] bigImg = new byte[fs.Length];
fs.Read(bigImg, 0, bigImg.Length);
fs.Close();
if (bigImg != null && bigImg.Count() != 0)
{
using (System.IO.MemoryStream stream = new System.IO.MemoryStream(bigImg))
{
System.Drawing.Image big = System.Drawing.Image.FromStream(stream);
if (((float)big.Width / big.Height) < ((float)imgWidth / imgHeight))
{
imgHeight = big.Height * imgWidth / big.Width;
}
else if (((float)big.Width / big.Height) > ((float)imgWidth / imgHeight))
{
imgWidth = big.Width * imgHeight / big.Height;
}
else if ((float)(big.Width / big.Height) == (imgWidth / imgHeight) && ((float)big.Height * imgWidth / big.Width) > ((float)big.Width * imgHeight / big.Height))
{
imgHeight = big.Height * imgWidth / big.Width;
}
else
{
imgWidth = big.Width * imgHeight / big.Height;
}
System.Drawing.Size size = new Size(imgHeight, imgHeight);
System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Transparent);
g.DrawImage(big, new System.Drawing.Rectangle(0, 0, size.Width, size.Height), new System.Drawing.Rectangle(0, 0, big.Width, big.Height), System.Drawing.GraphicsUnit.Pixel);
bitmap.Save(savePath, ImageFormat.Jpeg);
bitmap.Dispose();
big.Dispose();
g.Dispose();
}
}
}
/// <summary>
/// 以指定的宽高压缩图片并保存到指定的路径
/// </summary>
/// <param name="big">图片</param>
/// <param name="savePath">图片保存路径</param>
/// <param name="intWidth">压缩的宽度</param>
/// <param name="intHeight">压缩的高度</param>
/// <returns></returns>
public void CompressImage(Image big, string savePath, int imgWidth, int imgHeight)
{
if (((float)big.Width / big.Height) < ((float)imgWidth / imgHeight))
{
imgHeight = big.Height * imgWidth / big.Width;
}
else if (((float)big.Width / big.Height) > ((float)imgWidth / imgHeight))
{
imgWidth = big.Width * imgHeight / big.Height;
}
else if ((float)(big.Width / big.Height) == (imgWidth / imgHeight) && ((float)big.Height * imgWidth / big.Width) > ((float)big.Width * imgHeight / big.Height))
{
imgHeight = big.Height * imgWidth / big.Width;
}
else
{
imgWidth = big.Width * imgHeight / big.Height;
}
System.Drawing.Size size = new Size(imgWidth, imgHeight);
System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Transparent);
g.DrawImage(big, new System.Drawing.Rectangle(0, 0, size.Width, size.Height), new System.Drawing.Rectangle(0, 0, big.Width, big.Height), System.Drawing.GraphicsUnit.Pixel);
bitmap.Save(savePath, ImageFormat.Jpeg);
bitmap.Dispose();
g.Dispose();
}