圖片加水印和生成縮略圖C#代碼

jopen 9年前發布 | 1K 次閱讀 C#

一: 圖片加水印處理

     //獲得圖片的名稱
    string name = context.Request["name"];
    string filePath = context.Server.MapPath("Upload/" + name);
    //首先獲得要進行水印處理的圖片
    if (!string.IsNullOrEmpty(name))
    {
        using (Image image = Bitmap.FromFile(filePath))
        {
            //準備一個畫筆來在圖片上作畫
            using (Graphics g = Graphics.FromImage(image))
            {
                //作畫
                g.DrawString("str", new Font("微軟雅黑", 20), Brushes.Red, new PointF(1, 1));
                image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
            }
        }
    }

二:生成縮略圖

HttpPostedFile hpFile= context.Request.Files[0]; if (hpFile.ContentLength > 0) { string filePath = context.Server.MapPath("Upload/" + hpFile.FileName); //判斷文件是否為圖片文件 if (hpFile.ContentType.IndexOf("image") > -1) { //從輸入流中獲得圖片數據 using (Image img = Image.FromStream(hpFile.InputStream)) { //準備一張畫板來存儲小圖 using (Bitmap thumbImg = new Bitmap(120, 80)) { //利用畫筆將大圖繪制在小圖中 using (Graphics g = Graphics.FromImage(thumbImg)) { //用于指定最終畫的圖的大小 g.DrawImage(img, new Rectangle(0, 0, thumbImg.Width, thumbImg.Height), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel); //用于指定畫大圖中的那一部分

                        //將小圖也保存在相應的路徑中
                        thumbImg.Save(context.Server.MapPath("Upload/thumb_" + hpFile.FileName));
                        context.Response.Write("保存成功!");
                    }
                }
            }
        }


        //保存大圖
        hpFile.SaveAs(filePath);
    }</pre> 


 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!