1. 程式人生 > >Asp.net給圖片加上水印效果

Asp.net給圖片加上水印效果


    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