天天看點

上傳圖檔并生成縮略圖的一個方法

  aspx前台頁面簡單拖個檔案上傳控件和一個按鈕

...

 <div>

          <input id="File1" runat="server" type="file" />

          <asp:Button ID="btnUpload" runat="server" Text="上傳圖檔并生成縮略圖" OnClick="btnUpload_Click" />

    </div>

以下是背景一個比較重要的用于生成縮略圖的方法

    /// <summary>

    /// asp.net上傳圖檔并生成縮略圖

    /// </summary>

    /// <param name="upImage">HtmlInputFile控件</param>

    /// <param name="sSavePath">儲存的路徑,些為相對伺服器路徑的下的檔案夾</param>

    /// <param name="sThumbExtension">縮略圖的thumb</param>

    /// <param name="intThumbWidth">生成縮略圖的寬度</param>

    /// <param name="intThumbHeight">生成縮略圖的高度</param>

    /// <returns>縮略圖名稱</returns>

    public string UpLoadImage(HtmlInputFile upImage, string sSavePath, string sThumbExtension, int intThumbWidth, int intThumbHeight)

    {

        string sThumbFile = "";

        string sFilename = "";

        if (upImage.PostedFile != null)

        {

            HttpPostedFile myFile = upImage.PostedFile;

            int nFileLen = myFile.ContentLength;

            if (nFileLen == 0)

                return "沒有選擇上傳圖檔";

            //擷取upImage選擇檔案的擴充名

            string extendName = System.IO.Path.GetExtension(myFile.FileName).ToLower();

            //判斷是否為圖檔格式

            if (extendName != ".jpg" && extendName != ".jpge" && extendName != ".gif" && extendName != ".bmp" && extendName != ".png")

                return "圖檔格式不正确";

            byte[] myData = new Byte[nFileLen];

            myFile.InputStream.Read(myData, 0, nFileLen);

            sFilename = System.IO.Path.GetFileName(myFile.FileName);

            int file_append = 0;

            //檢查目前檔案夾下是否有同名圖檔,有則在檔案名+1

            while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename)))

            {

                file_append++;

                sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)

                + file_append.ToString() + extendName;

            }

            System.IO.FileStream newFile

            = new System.IO.FileStream(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename),

            System.IO.FileMode.Create, System.IO.FileAccess.Write);

            newFile.Write(myData, 0, myData.Length);

            newFile.Close();

            //以上為上傳原圖

            try

                //原圖加載

                using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename)))

                {

                    //原圖寬度和高度

                    int width = sourceImage.Width;

                    int height = sourceImage.Height;

                    int smallWidth;

                    int smallHeight;

                    //擷取第一張繪制圖的大小,(比較 原圖的寬/縮略圖的寬 和 原圖的高/縮略圖的高)

                    if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight)

                    {

                        smallWidth = intThumbWidth;

                        smallHeight = intThumbWidth * height / width;

                    }

                    else

                        smallWidth = intThumbHeight * width / height;

                        smallHeight = intThumbHeight;

                    //判斷縮略圖在目前檔案夾下是否同名稱檔案存在

                    file_append = 0;

                    sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + extendName;

                    while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sThumbFile)))

                        file_append++;

                        sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) +

                        file_append.ToString() + extendName;

                    //縮略圖儲存的絕對路徑

                    string smallImagePath = System.Web.HttpContext.Current.Server.MapPath(sSavePath) + sThumbFile;

                    //建立一個圖闆,以最小等比例壓縮大小繪制原圖

                    using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(smallWidth, smallHeight))

                        //繪制中間圖

                        using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))

                        {

                            //高清,平滑

                            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

                            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                            g.Clear(Color.Black);

                            g.DrawImage(

                            sourceImage,

                            new System.Drawing.Rectangle(0, 0, smallWidth, smallHeight),

                            new System.Drawing.Rectangle(0, 0, width, height),

                            System.Drawing.GraphicsUnit.Pixel

                            );

                        }

                        //建立一個圖闆,以縮略圖大小繪制中間圖

                        using (System.Drawing.Image bitmap1 = new System.Drawing.Bitmap(intThumbWidth, intThumbHeight))

                            //繪制縮略圖

                            using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap1))

                            {

                                //高清,平滑

                                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

                                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                                g.Clear(Color.Black);

                                int lwidth = (smallWidth - intThumbWidth) / 2;

                                int bheight = (smallHeight - intThumbHeight) / 2;

                                g.DrawImage(bitmap, new Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel);

                                g.Dispose();

                                bitmap1.Save(smallImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);

                            }

                }

            catch

                //出錯則删除

                System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename));

            //傳回縮略圖名稱

            return sThumbFile;

        }

        return "沒有選擇圖檔";

    }

以上是一個通用的方法,然後在按鈕中調用該方法即可:

protected void btnUpload_Click(object sender, EventArgs e)

        string a = this.UpLoadImage(this.File1, "UpLoad/", "thumb_", 118, 118);

        Response.Write("<script>alert('上傳并生成縮略圖成功...')</script>");

繼續閱讀