上传图片生成缩略图

上传图片 生成不同大小的缩略图

 

Code
/// <summary>
/// 上传图片
/// </summary>
/// <param name="Path"></param>
/// <param name="filePic"></param>
/// <returns></returns>
public static string[] UploadPic(string Path, FileUpload filePic)
{
//原图
System.Drawing.Image original_image = null;
System.Drawing.Bitmap final_image
= null;
System.Drawing.Graphics graphic
= null;
MemoryStream ms
= null;
//文件类型
string preveiw = Path+"preview/";
string display = Path + "display/";
string print = Path + "print/";
string[] uploadPath = new string[3];
string filename;
try
{
#region 判断目录是否存在
if (System.IO.Directory.Exists(HttpContext.Current.Request.PhysicalApplicationPath + preveiw) == false)
{
System.IO.Directory.CreateDirectory(HttpContext.Current.Request.PhysicalApplicationPath
+ preveiw);
}
if (System.IO.Directory.Exists(HttpContext.Current.Request.PhysicalApplicationPath + display) == false)
{
System.IO.Directory.CreateDirectory(HttpContext.Current.Request.PhysicalApplicationPath
+ display);
}
if (System.IO.Directory.Exists(HttpContext.Current.Request.PhysicalApplicationPath + print) == false)
{
System.IO.Directory.CreateDirectory(HttpContext.Current.Request.PhysicalApplicationPath
+ print);
}
#endregion

HttpPostedFile image_upload
= filePic.PostedFile;

original_image
= System.Drawing.Image.FromStream(image_upload.InputStream);

#region 计算缩略各种图宽、高
//preview
int preview_width = original_image.Width / 4;
int preview_height = original_image.Height / 4;
//display
int r=Convert.ToInt32(Math.Max(original_image.Width / 200, original_image.Height / 200));
r
= (r == 0 ? 1 : r);
int display_width = original_image.Width / r;
int display_height = original_image.Height / r;
#endregion

#region 上传图片
//preview
final_image = new System.Drawing.Bitmap(preview_width, preview_height);
graphic
= System.Drawing.Graphics.FromImage(final_image);
graphic.FillRectangle(
new System.Drawing.SolidBrush(System.Drawing.Color.Black), new System.Drawing.Rectangle(0, 0, preview_width, preview_height));
graphic.InterpolationMode
= System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphic.DrawImage(original_image,
0, 0, preview_width, preview_height);

filename
= FileName;
final_image.Save(System.IO.Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, preveiw
+ filename), System.Drawing.Imaging.ImageFormat.Jpeg);
final_image
= null;
graphic
= null;
uploadPath[
0] = preveiw + filename;
//display
final_image = new System.Drawing.Bitmap(display_width, display_height);
graphic
= System.Drawing.Graphics.FromImage(final_image);
graphic.FillRectangle(
new System.Drawing.SolidBrush(System.Drawing.Color.Black), new System.Drawing.Rectangle(0, 0, display_width, display_height));
graphic.InterpolationMode
= System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphic.DrawImage(original_image,
0, 0, display_width, display_height);

final_image.Save(System.IO.Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, display
+ filename), System.Drawing.Imaging.ImageFormat.Jpeg);
final_image
= null;
graphic
= null;
uploadPath[
1] = display + filename;

//print
final_image = new System.Drawing.Bitmap(original_image.Width, original_image.Height);
graphic
= System.Drawing.Graphics.FromImage(final_image);
graphic.FillRectangle(
new System.Drawing.SolidBrush(System.Drawing.Color.Black), new System.Drawing.Rectangle(0, 0, original_image.Width, original_image.Height));
graphic.InterpolationMode
= System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphic.DrawImage(original_image,
0, 0,original_image.Width,original_image.Height);

final_image.Save(System.IO.Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, print
+filename), System.Drawing.Imaging.ImageFormat.Jpeg);
final_image
= null;
graphic
= null;
uploadPath[
2] = print + filename;

#endregion

return uploadPath;
}
catch (Exception ex)
{
System.Console.Error.Write(ex.Message);
return null;
}
}

 

 

posted @ 2008-11-13 14:19  jinweida  阅读(440)  评论(0编辑  收藏  举报