GetPic(picstream, ref RealWidth, ref RealHeight, ref t);
FileStream fs = File.Create(filepath + "\\Photos\\" + filename);
fs.Write(t, 0, t.Length);
fs.Flush();
fs.Close();
fs.Dispose();
GetReduceImage(filepath + "\\Photos\\" + filename, filepath + "\\Photos\\", sltName, "0");
/// <summary>
/// 获取照片的实际宽度,实际高度等
/// </summary>
/// <param name="picstream">照片流</param>
/// <param name="RealWidth">实际宽度</param>
/// <param name="RealHeight">实际高度</param>
/// <param name="t"></param>
public void GetPic(Stream picstream, ref int RealWidth, ref int RealHeight, ref byte[] t)
{
t = new byte[picstream.Length];
picstream.Read(t, 0, (int)picstream.Length);
Image image = new Bitmap(picstream);
picstream.Flush();
picstream.Close();
picstream.Dispose();
RealWidth = image.Width;
RealHeight = image.Height;
if (image != null)
{
image.Dispose();
image = null;
}
}
/// <summary>
/// 生成压缩图
/// </summary>
/// <param name="objectPath">源文件路径</param>
/// <param name="targetFilePath">压缩文件路径</param>
/// <param name="tName">压缩文件名称</param>
public void GetReduceImage(string objectPath, string targetFilePath, string tName, string flag)
{
Image ResourceImage = Image.FromFile(objectPath);
GetThumbWandH(ResourceImage.Width, ResourceImage.Height, flag); //获取压缩的宽和高
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(ThumbW, ThumbH);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(ResourceImage, new System.Drawing.Rectangle(0, 0, ThumbW, ThumbH),
new System.Drawing.Rectangle(0, 0, ResourceImage.Width, ResourceImage.Height),
System.Drawing.GraphicsUnit.Pixel);
try
{
bitmap.Save(@targetFilePath + tName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
ResourceImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
public void GetThumbWandH(int Fwith, int Fheight, string bs)
{
if (bs == "1")
{
ThumbW = 50;
ThumbH = 50;
return;
}
if (Fwith >= Fheight && Fwith > StandW)
{
ThumbW = StandW;
ThumbH = StandW * Fheight / Fwith;
}
else if ((Fwith > Fheight && Fheight > StandH) || (Fwith <= Fheight && Fheight > StandH))
{
ThumbW = StandH * Fwith / Fheight;
ThumbH = StandH;
}
else
{
ThumbW = Fwith;
ThumbH = Fheight;
}
if (ThumbW <= StandW && ThumbH <= StandH)
{
return;
}
GetThumbWandH(ThumbW, ThumbH, bs);
}