码迷,mamicode.com
首页 > Web开发 > 详细

[转].net 缩略图方法

时间:2014-06-07 06:26:52      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   class   blog   code   

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

 

bubuko.com,布布扣
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;
            }
}
bubuko.com,布布扣

 

[转].net 缩略图方法,布布扣,bubuko.com

[转].net 缩略图方法

标签:des   c   style   class   blog   code   

原文地址:http://www.cnblogs.com/freeliver54/p/3770603.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!