關於asp.net上傳圖片自動產生縮圖

來源:互聯網
上載者:User

 /// <summary>
    /// 產生縮圖
    /// </summary>
    /// <param name="fileName">原檔案</param>
    /// <param name="width">寬度</param>
    /// <param name="height">高度</param>
    private void CreateThumbnailImage(string fileName, string smallPath, int width, int height)
    {
        //著作權資訊
        string strAdd = "www.wander.com";
        //產生的縮圖的名字=年月日+檔案大小(防止檔案名稱相同)
        string newFileName = smallPath;

        System.Drawing.Image image, newImage;
        //載入原映像
        image = System.Drawing.Image.FromFile(Server.MapPath(fileName));
        //回調
        System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(callBack);
        //產生縮圖
        newImage = image.GetThumbnailImage(width, height, callb, new System.IntPtr());

        AddInfo(newImage, strAdd, newFileName, 16);

        image.Dispose();
        newImage.Dispose();

        Image1.ImageUrl = newFileName;
    }

    /// <summary>
    /// 添加著作權資訊
    /// </summary>
    /// <param name="image"></param>
    /// <param name="strAdd"></param>
    /// <param name="newFileName"></param>
    /// <param name="fontSize"></param>
    private void AddInfo(System.Drawing.Image image, string strAdd, string newFileName, int fontSize)
    {
        Response.Clear();
        Bitmap b = new Bitmap(image);
        Graphics g = Graphics.FromImage(b);
        g.DrawString(strAdd, new Font("宋體", fontSize), new SolidBrush(Color.Red), image.Width / 2 - 80, image.Height - 30);
        b.Save(Server.MapPath(newFileName), System.Drawing.Imaging.ImageFormat.Gif);
    }

    private bool callBack()
    {
        return true;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (loadFile.PostedFile != null)
        {
            //判斷是不是影像檔
            if (loadFile.PostedFile.ContentType.ToLower().IndexOf("image") < 0)
            {
                //檔案類型錯誤
            }
            else
            {
                string ext = loadFile.PostedFile.FileName.Substring(loadFile.PostedFile.FileName.LastIndexOf("."));
                //產生新的原檔案名稱 年月日+檔案大小+副檔名
                string path = "Uploads\\" + System.DateTime.Now.Date.ToShortDateString() + loadFile.PostedFile.ContentLength.ToString() + ext;
                //加上著作權資訊的檔案
                string newPath = "Uploads\\" + System.DateTime.Now.Date.ToShortDateString() + loadFile.PostedFile.ContentLength.ToString() + "new" + ext;
                //縮圖檔案名稱
                string newFileName = "uploads\\" + System.DateTime.Now.Date.ToShortDateString() + loadFile.PostedFile.ContentLength.ToString() + "small.gif";
                //上傳原圖
                loadFile.PostedFile.SaveAs(Server.MapPath(path));
                //產生縮圖
                CreateThumbnailImage(path, newFileName, 200, 300);

                //給原圖加上著作權資訊
                System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(path));
                AddInfo(image, "www.wander.com", newPath, 20);
                image.Dispose();
            }
        }

    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.