using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class PicCompress
{
public byte[] GetPictureData(string imagepath)
{
FileStream file = new FileStream(imagepath, FileMode.Open);
byte[] by = new byte[file.Length];
file.Read(by, 0, by.Length);
file.Close();
return by;
}
public byte[] GetPictureData(Image imgPhoto)
{
//将Image转换成流数据,并保存为byte[]
MemoryStream mstream = new MemoryStream();
imgPhoto.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] byData = new Byte[mstream.Length];
mstream.Position = 0;
mstream.Read(byData, 0, byData.Length);
mstream.Close();
return byData;
}
/// <summary>
/// 无损压缩图片
/// </summary>
/// <param name="sFile">原图片</param>
/// <param name="dFile">压缩后保存位置</param>
/// <param name="dHeight">高度</param>
/// <param name="dWidth">宽度</param>
/// <param name="flag">压缩质量 1-100</param>
/// <returns></returns>
public bool CompressImage(Image iSource, string dFile, int dHeight, int dWidth, int flag)
{
ImageFormat tFormat = iSource.RawFormat;
int sW = 0, sH = 0;
//按比例缩放
Size tem_size = new Size(iSource.Width, iSource.Height);
if (tem_size.Width > dHeight || tem_size.Width > dWidth)
{
if ((tem_size.Width * dHeight) > (tem_size.Height * dWidth))
{
sW = dWidth;
sH = (dWidth * tem_size.Height) / tem_size.Width;
}
else
{
sH = dHeight;
sW = (tem_size.Width * dHeight) / tem_size.Height;
}
}
else
{
sW = tem_size.Width;
sH = tem_size.Height;
}
Bitmap ob = new Bitmap(sW, sH);
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, sW, sH), 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;
}
}
if (jpegICIinfo != null)
{
ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径
}
else
{
ob.Save(dFile, tFormat);
}
return true;
}
catch
{
return false;
}
finally
{
iSource.Dispose();
ob.Dispose();
}
}
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
string aplpath = Application.StartupPath.Replace("\\", "/") + "/PIC/";
openFileDialog1.Filter = "所有文件(*.*)|*.*|BMP文件(*.bmp)|*.bmp|PNG文件(*.png)|*.png|JPG文件(*.jpg)|*.jpg|GIF文件(*.gif)|*.gif";
DialogResult r = openFileDialog1.ShowDialog();
if (r == DialogResult.OK)
{
string Frompic = openFileDialog1.FileName.Replace("\\", "/");
PicFullname = (aplpath + openFileDialog1.SafeFileName).Replace("\\", "/");
if (Frompic != PicFullname)
{
if (Directory.Exists(aplpath) == false)
{
Directory.CreateDirectory(aplpath);
}
}
// labpath.Text = Frompic;
PicCompress pc = new PicCompress();
FileStream fs = new FileStream(Frompic, FileMode.Open, FileAccess.Read);
Image img = Image.FromStream(fs);
pc.CompressImage(img, PicFullname, 400, 400, 50);
if (DB_Function.IsAdmin == 3)
{
pictureBox2.ImageLocation = PicFullname;
}
else
{
pictureBox1.ImageLocation = PicFullname;
}
}
}
来源:https://blog.csdn.net/fanwenhu/article/details/100030728