首页 > 代码库 > [转].net 缩略图方法

[转].net 缩略图方法

本文转自:http://www.cnblogs.com/promic/archive/2010/04/21/1717190.html

 

private static string strConnect = System.Configuration.ConfigurationManager.AppSettings["connStr"];
    //上传图像
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        ……
        string imgType;
        string fullFileName = this.txtPath.PostedFile.FileName;
        imgType = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1).ToLower();
        string UserDirectory = Session["UserID"].ToString();//用户ID为所创建文件夹的名字
        string UserPath = Server.MapPath("UpLoad").ToString() + "\\" + UserDirectory;
        //如果文件夹不存在则创建
        if (!Directory.Exists(UserPath))
        {
            Directory.CreateDirectory(UserPath);
        }
        string originalImagePath = UserPath + "\\" + "Original";
        if(!Directory.Exists(originalImagePath))
        {
            Directory.CreateDirectory(originalImagePath);
        }
        string thumbnailPath = UserPath + "\\" + "Thumbnail";
        if (!Directory.Exists(thumbnailPath))
        {
            Directory.CreateDirectory(thumbnailPath);
        }        
        // 获取上传图像的文件名
        string fileName = txtName.Text;
        ……
            if (imgType=="jpg"||imgType=="jpeg"||imgType=="bmp"||imgType=="png"||imgType=="icon")
            {
                try
                { 
                    //生成原图
                    byte[] oFileByte = new byte[this.txtPath.PostedFile.ContentLength];
                    Stream oStream = this.txtPath.PostedFile.InputStream;
                    System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);
                    int oWidth = oImage.Width;//原图宽度
                    int oHeight = oImage.Height;//原图高度
                    int tWidth = 100;//设置缩略图初始宽度
                    int tHeight = 100;//设置缩略图初始宽度

                    //按比例计算出缩略图的宽度和高度
                    if (oWidth >= oHeight)
                    {
                        tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oHeight)));
                    }
                    else
                    {
                        tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));
                    }
                    
                    //生成缩略图
                    Bitmap tImage = new Bitmap(tWidth,tHeight);
                    Graphics g = Graphics.FromImage(tImage);
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;//设置高质量插值法
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度
                    g.Clear(Color.Transparent);//清空画布并以透明背景色填充
                    g.DrawImage(oImage, new Rectangle(0, 0, tWidth, tHeight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel);
                                        
                    //this.txtPath.PostedFile.SaveAs(originalImagePath + "\\" + fileName + "." + imgType);
                    //string originalImageUrl = "UpLoad/" + UserDirectory + "/" + "Orginal/"+fileName + ".jpg" ;//得到服务端原图片的虚拟路径
                    // string thumbnailImageUrl = "UpLoad/" + UserDirectory + "/" + "Thumbnail/" + fileName + ".jpg";//得到服务端原图片的虚拟路径
                    //以JPG格式保存图像
                    //oImage.Save(originalImageUrl, System.Drawing.Imaging.ImageFormat.Jpeg);/*看看这里这两个保存方式有没有问题*/
                  //   tImage.Save(thumbnailImageUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
                    string oPath = originalImagePath + "\\" + fileName + ".jpg";
                    string tPath = thumbnailPath + "\\" + fileName + ".jpg";
                    //以JPG格式保存图像
                    oImage.Save(oPath, System.Drawing.Imaging.ImageFormat.Jpeg);
                    tImage.Save(tPath, System.Drawing.Imaging.ImageFormat.Jpeg);


                    lblMessage.Visible = true;
                    lblMessage.Text = "图像上传成功!";
                    txtName.Text = "";
                    txtDescription.Text = "";
                    //释放资源                    
                    oImage.Dispose();
                    g.Dispose();
                    tImage.Dispose();
                }
                catch (Exception ex)
                {
                    lblMessage.Visible = true;
                    lblMessage.Text = "由于网络原因,上载文件错误 " + ex.Message;
                }
            }
            else
            {
                Response.Write("<script language=‘javascript‘>alert(‘你选择的图像格式错误!‘);</script>");
                lblMessage.Visible = false;
            }
}