none
截取图片效果。储存相对路径 RRS feed

  • 问题

  • 我实现了图片截取效果,但是如何获得截取后图片的名字。并且储存数据库那?

    下面贴出代码:
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace ZoomImageDemo
    {
        public partial class UPPhoto : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (Page.IsPostBack) return;
                if (!string.IsNullOrEmpty(Request.QueryString["Picurl"]))
                {
                    //ImageDrag.ImageUrl = Request.QueryString["Picurl"];
                    ImageIcon.ImageUrl = Request.QueryString["Picurl"];
                    Page.ClientScript.RegisterStartupScript(typeof(UPPhoto), "step2", "<script type='text/javascript'>Step2();</script>");
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(typeof(UPPhoto), "step1", "<script type='text/javascript'>Step1();</script>");
                }
            }
            private const string savepath = "User/UserHeadImage" + "/";

            protected void btnUpload_Click(object sender, EventArgs e)
            {
                if (fuPhoto.PostedFile != null && fuPhoto.PostedFile.ContentLength > 0)
                {

                    string ext = System.IO.Path.GetExtension(fuPhoto.PostedFile.FileName).ToLower();
                    if (ext != ".jpg" && ext != ".jepg" && ext != ".bmp" && ext != ".gif")
                    {
                        return; //notice user change file type
                    }
                    string filename = "xuanye_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ext;
                    string path = "~/UploadPhoto/" + filename;
                    fuPhoto.PostedFile.SaveAs(Server.MapPath(path));
                    Response.Redirect("~/UPPhoto.aspx?Picurl=" + Server.UrlEncode(path));
                }
                else
                {
                    //do some thing;notice user select file ;
                }
            }

            protected void btn_Image_Click(object sender, EventArgs e)
            {
                int imageWidth = Int32.Parse(txt_width.Text);
                int imageHeight = Int32.Parse(txt_height.Text);
                int cutTop = Int32.Parse(txt_top.Text);
                int cutLeft = Int32.Parse(txt_left.Text);
                int dropWidth = Int32.Parse(txt_DropWidth.Text);
                int dropHeight = Int32.Parse(txt_DropHeight.Text);

                string filename = CutPhotoHelp.SaveCutPic(Server.MapPath(ImageIcon.ImageUrl), Server.MapPath(savepath), 0, 0, dropWidth,
                                        dropHeight, cutLeft, cutTop, imageWidth, imageHeight);

                this.imgphoto.ImageUrl = savepath + filename;
                Page.ClientScript.RegisterStartupScript(typeof(UPPhoto), "step3", "<script type='text/javascript'>Step3();</script>");
            }
        }
    }

    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    namespace ZoomImageDemo
    {
        public class CutPhotoHelp
        {
            public static string SaveCutPic(string pPath, string pSavedPath, int pPartStartPointX, int pPartStartPointY, int pPartWidth, int pPartHeight, int pOrigStartPointX, int pOrigStartPointY, int imageWidth, int imageHeight)
            {
                 using (Image originalImg = Image.FromFile(pPath))
                 {
                     if (originalImg.Width == imageWidth && originalImg.Height == imageHeight)
                     {
                         return SaveCutPic(pPath, pSavedPath, pPartStartPointX, pPartStartPointY, pPartWidth, pPartHeight,
                                 pOrigStartPointX, pOrigStartPointY);
                         
                     }
                     string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";
                     string filePath = pSavedPath + "\\" + filename;

                     Bitmap thumimg =MakeThumbnail(originalImg, imageWidth, imageHeight);
                  
                     Bitmap partImg = new Bitmap(pPartWidth, pPartHeight);

                     Graphics graphics = Graphics.FromImage(partImg);
                     Rectangle destRect = new Rectangle(new Point(pPartStartPointX, pPartStartPointY), new Size(pPartWidth, pPartHeight));//目标位置
                     Rectangle origRect = new Rectangle(new Point(pOrigStartPointX, pOrigStartPointY), new Size(pPartWidth, pPartHeight));//原图位置(默认从原图中截取的图片大小等于目标图片的大小)

                     ///文字水印 
                     Graphics G = Graphics.FromImage(partImg);
                     //Font f = new Font("Lucida Grande", 6);
                     //Brush b = new SolidBrush(Color.Gray);
                     G.Clear(Color.White);
                     // 指定高质量的双三次插值法。执行预筛选以确保高质量的收缩。此模式可产生质量最高的转换图像。
                     G.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    // 指定高质量、低速度呈现。
                     G.SmoothingMode = SmoothingMode.HighQuality;

                     graphics.DrawImage(thumimg, destRect, origRect, GraphicsUnit.Pixel);
                     //G.DrawString("Xuanye", f, b, 0, 0);
                     G.Dispose();

                     originalImg.Dispose();
                     if (File.Exists(filePath))
                     {
                         File.SetAttributes(filePath, FileAttributes.Normal);
                         File.Delete(filePath);
                     }
                     partImg.Save(filePath, ImageFormat.Jpeg);

                     partImg.Dispose();
                     thumimg.Dispose();
                     return filename;
                 }
            }

            public static Bitmap MakeThumbnail(Image fromImg, int width, int height)
            {
                Bitmap bmp = new Bitmap(width, height);
                int ow = fromImg.Width;
                int oh = fromImg.Height;

                //新建一个画板
                Graphics g = Graphics.FromImage(bmp);
            
                //设置高质量插值法
                g.InterpolationMode = InterpolationMode.High;
                //设置高质量,低速度呈现平滑程度
                g.SmoothingMode =SmoothingMode.HighQuality;
                //清空画布并以透明背景色填充
                g.Clear(Color.Transparent);

                g.DrawImage(fromImg, new Rectangle(0, 0, width, height),
                    new Rectangle(0, 0, ow, oh),
                    GraphicsUnit.Pixel);

                return bmp;

           }

            public static string SaveCutPic(string pPath, string pSavedPath, int pPartStartPointX, int pPartStartPointY, int pPartWidth, int pPartHeight, int pOrigStartPointX, int pOrigStartPointY)
            {
               string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";
               string filePath = pSavedPath + "\\" + filename;

                using (Image originalImg = Image.FromFile(pPath))
                {
                    Bitmap partImg = new Bitmap(pPartWidth, pPartHeight);
                    Graphics graphics = Graphics.FromImage(partImg);
                    Rectangle destRect = new Rectangle(new Point(pPartStartPointX, pPartStartPointY), new Size(pPartWidth, pPartHeight));//目标位置
                    Rectangle origRect = new Rectangle(new Point(pOrigStartPointX, pOrigStartPointY), new Size(pPartWidth, pPartHeight));//原图位置(默认从原图中截取的图片大小等于目标图片的大小)
                   
                    ///注释 文字水印 
                    Graphics G = Graphics.FromImage(partImg);
                    //Font f = new Font("Lucida Grande", 6);
                    //Brush b = new SolidBrush(Color.Gray);
                    G.Clear(Color.White);
                    // 指定高质量的双三次插值法。执行预筛选以确保高质量的收缩。此模式可产生质量最高的转换图像。
                    G.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    // 指定高质量、低速度呈现。
                    G.SmoothingMode = SmoothingMode.HighQuality;

                    graphics.DrawImage(originalImg, destRect, origRect, GraphicsUnit.Pixel);
                    //G.DrawString("Xuanye", f, b, 0, 0);
                    G.Dispose();

                    originalImg.Dispose();
                    if (File.Exists(filePath))
                    {
                        File.SetAttributes(filePath, FileAttributes.Normal);
                        File.Delete(filePath);
                    }
                    partImg.Save(filePath, ImageFormat.Jpeg);
                    partImg.Dispose();
                }
                return filename;
            }
          
        }

    }

    2010年1月27日 7:48

答案

全部回复