Asp.net(C#)給圖片加上浮水印效果和按比例產生高品質縮圖

來源:互聯網
上載者:User
Asp.net(C#)給圖片加上浮水印效果     private void Btn_Upload_Click(object sender, System.EventArgs e)        {            if(UploadFile.PostedFile.FileName.Trim()!="")            {                //上傳檔案                string extension = Path.GetExtension(UploadFile.PostedFile.FileName).ToUpper();                string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();                string path = Server.MapPath(".") + "/UploadFile/" + fileName + extension;                UploadFile.PostedFile.SaveAs(path);                //加文字浮水印,注意,這裡的代碼和以下加圖片浮水印的代碼不能共存                System.Drawing.Image image = System.Drawing.Image.FromFile(path);                Graphics g = Graphics.FromImage(image);                g.DrawImage(image, 0, 0, image.Width, image.Height);                Font f = new Font("Verdana", 32);                Brush b = new SolidBrush(Color.White);                string addText = AddText.Value.Trim();                g.DrawString(addText, f, b, 10, 10);                g.Dispose();                //加圖片浮水印                System.Drawing.Image image = System.Drawing.Image.FromFile(path);                System.Drawing.Image copyImage = System.Drawing.Image.FromFile( Server.MapPath(".") + "/Alex.gif");                Graphics g = Graphics.FromImage(image);                g.DrawImage(copyImage, new Rectangle(image.Width-copyImage.Width, image.Height-copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);                g.Dispose();                //儲存加浮水印過後的圖片,刪除原始圖片                string newPath = Server.MapPath(".") + "/UploadFile/" + fileName + "_new" + extension;                image.Save(newPath);                image.Dispose();                if(File.Exists(path))                {                    File.Delete(path);                }                Response.Redirect(newPath);            }        }//產生縮圖#region //產生縮圖        /**//// <summary>        /// 產生縮圖        /// </summary>        /// <param name="str_originalImagePath">源圖路徑(實體路徑)</param>        /// <param name="str_ThumbnailPath">縮圖路徑(實體路徑)</param>        /// <param name="i_width">縮圖寬度</param>        /// <param name="i_height">縮圖高度</param>        /// <param name="str_mode">產生縮圖的模式</param>        public static void MakeThumbnail(string str_originalImagePath,string str_ThumbnailPath,int i_width,int i_height,string str_mode)        {            System.Drawing.Image img_OriginalImage = Image.FromFile(str_originalImagePath);            int i_ToWidth = i_width;            int i_ToHeight = i_height;            int x = 0;            int y = 0;            int i_OriginalWidth = img_OriginalImage.Width;            int i_OriginalHeight = img_OriginalImage.Height;            switch(str_mode)            {                case "HW": //按照指定的高度和寬度進行縮放(可能變形)                    break;                case "W" : //指定寬度,高度按照比例縮放                    i_ToHeight = img_OriginalImage.Height*i_width/img_OriginalImage.Width;                    break;                case "H" : //指定高度,寬度按照比例縮放                    i_ToWidth = img_OriginalImage.Width*i_Height/img_OriginalImage.Height;                    break;                case "CUT" : //按照指定的高度和寬度剪裁(不變形)                    if ((double)img_OriginalImage.Width/(double)img_OriginalImage.Height > (double)i_ToWidth/(double)i_ToHeight)                    {                        i_OriginalHeight = img_OriginalImage.Height;                        i_OriginalWidth = img_OriginalImage.Height*i_ToWidth/i_ToHeight;                        y = 0;                        x = (img_OriginalImage.Width-i_OriginalWidth)/2;                    }                    else                    {                        i_OriginalWidth = img_OriginalImage.Width;                        i_OriginalHeight = img_OriginalImage.Width*i_Height/i_ToWidth;                        x = 0;                        y = (img_OriginalImage.Height-i_OriginalHeight)/2;                    }                    break;                default:                    break;            }            //建立一個BMP圖片            Image img_BitMap = new System.Drawing.Bitmap(i_ToWidth,i_ToHeight);            //建立一個畫板            System.Drawing.Graphics gp = new System.Drawing.Graphics.FromImage(img_BitMap);            //設定高品質插值法            gp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;            //設定高品質、低速度呈現平滑程度            gp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;            //清空畫布並以透明背景色填充            gp.Clear(Color.Transparent);            //指定位置並按大小繪製原圖片的指定部分            gp.DrawImage(img_OriginalImage,new Rectangle(0,0,i_ToWidth,i_ToHeight),new Rectangle(x,y,i_OrignalWidth,i_OriginalHeight),GraphicsUnit.Pixel);            try            {                //以JPG格式儲存圖片                img_BitMap.Save(str_ThumbnailPath,System.Drawing.Imaging.ImageFormat.Jpeg);            }            catch(Exception e)            {                throw e;            }            finally            {                img_OriginalImage.Dispose();                img_BitMap.Dispose();                gp.Dispose();            }        }        #endregion
相關文章

聯繫我們

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