图片上传,并自动生成缩略图!
代码如下:
/// <summary>
/// 上传单个图片。生成缩略图,jpg格式。
/// 调用方法:protected System.Web.UI.HtmlControls.HtmlInputFile FilePath;
/// 类名.uploadPhoto(FilePath.PostedFile.InputStream);
/// </summary>
/// <param name="source"></param>
public static string uploadPhoto(System.IO.Stream source)
{
string photoGuid=System.Guid.NewGuid().ToString();
//string sourcePic=System.Web.HttpContext.Current.Server.MapPath("/Upload/Photo/" + photoGuid + ".jpg");
string path =System.Web.HttpContext.Current.Server.MapPath("/");
string sourcePic= path + System.Configuration.ConfigurationSettings.AppSettings["photoPath"] + photoGuid + ".jpg";
string thumbPic=path +System.Configuration.ConfigurationSettings.AppSettings["thumbPath"] + photoGuid + ".jpg";
// int thumbWidth=120;
// int thumbHeight=160;
int thumbWidth=114;
int thumbHeight=154;
/// <summary>
/// 上传单个图片。生成缩略图,jpg格式。
/// 调用方法:protected System.Web.UI.HtmlControls.HtmlInputFile FilePath;
/// 类名.uploadPhoto(FilePath.PostedFile.InputStream);
/// </summary>
/// <param name="source"></param>
public static string uploadPhoto(System.IO.Stream source)
{
string photoGuid=System.Guid.NewGuid().ToString();
//string sourcePic=System.Web.HttpContext.Current.Server.MapPath("/Upload/Photo/" + photoGuid + ".jpg");
string path =System.Web.HttpContext.Current.Server.MapPath("/");
string sourcePic= path + System.Configuration.ConfigurationSettings.AppSettings["photoPath"] + photoGuid + ".jpg";
string thumbPic=path +System.Configuration.ConfigurationSettings.AppSettings["thumbPath"] + photoGuid + ".jpg";
// int thumbWidth=120;
// int thumbHeight=160;
int thumbWidth=114;
int thumbHeight=154;
//生成图片id
Image image = Image.FromStream(source);
if(image.Height>600 || image.Width >600)
Compress(image,sourcePic);
else
{
FileStream srcfs = new FileStream(sourcePic,FileMode.Create);
image.Save(srcfs, ImageFormat.Jpeg);
srcfs.Close();
}
Image image = Image.FromStream(source);
if(image.Height>600 || image.Width >600)
Compress(image,sourcePic);
else
{
FileStream srcfs = new FileStream(sourcePic,FileMode.Create);
image.Save(srcfs, ImageFormat.Jpeg);
srcfs.Close();
}
//生成缩略图
int tw = thumbWidth;
int th = thumbHeight;
if(image.Height / th > image.Width / tw)
{
tw = image.Width * th / image.Height;
}
else
{
th = image.Height * tw / image.Width;
}
Image thumbImage = Image.FromHbitmap(new Bitmap(thumbWidth,thumbHeight,PixelFormat.Format32bppRgb).GetHbitmap());
Graphics g = Graphics.FromImage(thumbImage);
SolidBrush brush = new SolidBrush(Color.White);
g.FillRectangle(brush, 0, 0, thumbWidth, thumbHeight);
g.DrawImage(image, (thumbWidth - tw)/2, (thumbHeight -th)/2, tw, th);
g.Dispose();
FileStream thumbfs= new FileStream(thumbPic,FileMode.Create);
thumbImage.Save(thumbfs,ImageFormat.Jpeg);
thumbfs.Close();
image.Dispose();
return(photoGuid);
}
public static void Compress(Image sourceImage, string filename)
{
int targetWidth = 600;
int targetHeight = 600;
int tw = thumbWidth;
int th = thumbHeight;
if(image.Height / th > image.Width / tw)
{
tw = image.Width * th / image.Height;
}
else
{
th = image.Height * tw / image.Width;
}
Image thumbImage = Image.FromHbitmap(new Bitmap(thumbWidth,thumbHeight,PixelFormat.Format32bppRgb).GetHbitmap());
Graphics g = Graphics.FromImage(thumbImage);
SolidBrush brush = new SolidBrush(Color.White);
g.FillRectangle(brush, 0, 0, thumbWidth, thumbHeight);
g.DrawImage(image, (thumbWidth - tw)/2, (thumbHeight -th)/2, tw, th);
g.Dispose();
FileStream thumbfs= new FileStream(thumbPic,FileMode.Create);
thumbImage.Save(thumbfs,ImageFormat.Jpeg);
thumbfs.Close();
image.Dispose();
return(photoGuid);
}
public static void Compress(Image sourceImage, string filename)
{
int targetWidth = 600;
int targetHeight = 600;
if(sourceImage.Height <= 600 && sourceImage.Width <= 600)
{
targetHeight = sourceImage.Height;
targetWidth = sourceImage.Width;
}
else if(sourceImage.Height > sourceImage.Width)
{
targetWidth = sourceImage.Width * 600 / sourceImage.Height;
}
else
{
targetHeight = sourceImage.Height * 600 / sourceImage.Width;
}
{
targetHeight = sourceImage.Height;
targetWidth = sourceImage.Width;
}
else if(sourceImage.Height > sourceImage.Width)
{
targetWidth = sourceImage.Width * 600 / sourceImage.Height;
}
else
{
targetHeight = sourceImage.Height * 600 / sourceImage.Width;
}
// 缩放图片
Image targetImage = Image.FromHbitmap(new Bitmap(targetWidth, targetHeight, PixelFormat.Format32bppRgb).GetHbitmap());
Graphics g = Graphics.FromImage(targetImage);
g.DrawImage(sourceImage, 0, 0, targetWidth, targetHeight);
g.Dispose();
// 设置输出格式
EncoderParameters encParams = new EncoderParameters(1);
encParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L);
ImageCodecInfo codeInfo = null;
ImageCodecInfo[] codeInfos = ImageCodecInfo.GetImageEncoders();
foreach(ImageCodecInfo info in codeInfos)
{
if(info.MimeType.Equals("image/jpeg"))
{
codeInfo = info;
break;
}
}
if(codeInfo != null)
{
targetImage.Save(filename, codeInfo, encParams);
}
targetImage.Dispose();
}
Web.Config中的设置如下:
<appSettings>
<add key="photoPath" value="\upload\photo\"/>
<add key="thumbPath" value="\upload\thumb\"/>
<add key="otherFilePath" value="\upload\other\"/>
Image targetImage = Image.FromHbitmap(new Bitmap(targetWidth, targetHeight, PixelFormat.Format32bppRgb).GetHbitmap());
Graphics g = Graphics.FromImage(targetImage);
g.DrawImage(sourceImage, 0, 0, targetWidth, targetHeight);
g.Dispose();
// 设置输出格式
EncoderParameters encParams = new EncoderParameters(1);
encParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L);
ImageCodecInfo codeInfo = null;
ImageCodecInfo[] codeInfos = ImageCodecInfo.GetImageEncoders();
foreach(ImageCodecInfo info in codeInfos)
{
if(info.MimeType.Equals("image/jpeg"))
{
codeInfo = info;
break;
}
}
if(codeInfo != null)
{
targetImage.Save(filename, codeInfo, encParams);
}
targetImage.Dispose();
}
Web.Config中的设置如下:
<appSettings>
<add key="photoPath" value="\upload\photo\"/>
<add key="thumbPath" value="\upload\thumb\"/>
<add key="otherFilePath" value="\upload\other\"/>
</appSettings>