有一张图片是1.jpg. widht:100  height:200
现在我想得到这张图片的x坐标10 ,y坐标40的位置,长为40,宽为70的图片区域,并在<img src='1.jpg' ></img>上显示出截取后的图片 ,请问如何实现

解决方案 »

  1.   

    http://topic.csdn.net/t/20050413/11/3932437.html
      

  2.   

    ///   <summary>  
            ///   从图片中截取部分生成新图  
            ///   </summary>  
            ///   <param   name="sFromFilePath">原始图片</param>  
            ///   <param   name="saveFilePath">生成新图</param>  
            ///   <param   name="width">截取图片宽度</param>  
            ///   <param   name="height">截取图片高度</param>  
            ///   <param   name="spaceX">截图图片X坐标</param>  
            ///   <param   name="spaceY">截取图片Y坐标</param>  
            public static void CaptureImage(string sFromFilePath, string saveFilePath, int width, int height, int spaceX, int spaceY)
            {
                //载入底图  
                Image fromImage = Image.FromFile(sFromFilePath);
                int x = 0;   //截取X坐标  
                int y = 0;   //截取Y坐标  
                //原图宽与生成图片宽   之差      
                //当小于0(即原图宽小于要生成的图)时,新图宽度为较小者   即原图宽度   X坐标则为0    
                //当大于0(即原图宽大于要生成的图)时,新图宽度为设置值   即width         X坐标则为   sX与spaceX之间较小者  
                //Y方向同理  
                int sX = fromImage.Width - width;
                int sY = fromImage.Height - height;
                if (sX > 0)
                {
                    x = sX > spaceX ? spaceX : sX;
                }
                else
                {
                    width = fromImage.Width;
                }
                if (sY > 0)
                {
                    y = sY > spaceY ? spaceY : sY;
                }
                else
                {
                    height = fromImage.Height;
                }            //创建新图位图  
                Bitmap bitmap = new Bitmap(width, height);
                //创建作图区域  
                Graphics graphic = Graphics.FromImage(bitmap);
                //截取原图相应区域写入作图区  
                graphic.DrawImage(fromImage, 0, 0, new Rectangle(x, y, width, height), GraphicsUnit.Pixel);
                //从作图区生成新图  
                Image saveImage = Image.FromHbitmap(bitmap.GetHbitmap());
                //保存图象  
                saveImage.Save(saveFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                //释放资源  
                saveImage.Dispose();
                bitmap.Dispose();
                graphic.Dispose();
            }
    重绘
      

  3.   

        protected void Page_Load(object sender, EventArgs e)
        {
            string path = Server.MapPath("images/p002.jpg");
            DrawImage(path, 10, 40, 40, 70);
        }
        public void DrawImage(string path, int x, int y, int width, int height)
        {
            string savepath = "images/thu_p002.jpg";
            System.Drawing.Image image = System.Drawing.Image.FromFile(path);
            Bitmap bitmap = new Bitmap(width, height);
            if (image.Width - width > 0)
                x = x > (image.Width - width) ? image.Width - width : x;
            else
                width = image.Width;
            if (image.Height - height > 0)
                y = y > (image.Height - height) ? image.Height - height : y;
            else
                height = image.Height;
            Graphics g = Graphics.FromImage(bitmap);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.DrawImage(image, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel);
            bitmap.Save(Server.MapPath(savepath));
            bitmap.Dispose();
            image.Dispose();
            g.Dispose();
            Image1.ImageUrl = savepath;
        }