[轉].net 縮圖方法

來源:互聯網
上載者:User

標籤:des   c   style   class   blog   code   

本文轉自: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;            }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.