2012-01-10 193 views
2

现在出现裁剪部分:显示图像的不同部分,并显示正确的宽度,高度。不裁剪选定区域

区要裁剪

enter image description here

耕地面积 enter image description here

继承人我的js代码

 jQuery(document).ready(function() { 

      jQuery('#imgCrop').Jcrop({ 

       onSelect: storeCoords, 
       onChange: storeCoords 


      }); 

     }); 

     function storeCoords(c) { 

    jQuery('#X').val(c.x); 

    jQuery('#Y').val(c.y); 

    jQuery('#W').val(c.w); 
    jQuery('#H').val(c.h); 

    }; 

protected void btnCrop_Click(object sender, EventArgs e) 
{ 

    string ImageName = Session["WorkingImage"].ToString(); 

    int w = Convert.ToInt32(W.Value); 

    int h = Convert.ToInt32(H.Value); 

    int x = Convert.ToInt32(X.Value); 

    int y = Convert.ToInt32(Y.Value); 



    byte[] CropImage = Crop(path + ImageName, w, h, x, y); 

    using (MemoryStream ms = new MemoryStream(CropImage, 0, CropImage.Length)) 
    { 

     ms.Write(CropImage, 0, CropImage.Length); 

     using (SD.Image CroppedImage = SD.Image.FromStream(ms, true)) 
     { 

      string SaveTo = path + "crop" + ImageName; 

      CroppedImage.Save(SaveTo, CroppedImage.RawFormat); 

      pnlCrop.Visible = false; 

      pnlCropped.Visible = true; 

      imgCropped.ImageUrl = "images/crop" + ImageName; 

     } 

    } 

} 
static byte[] Crop(string Img, int Width, int Height, int X, int Y) 
{ 

    try 
    { 

     using (SD.Image OriginalImage = SD.Image.FromFile(Img)) 
     { 

      using (SD.Bitmap bmp = new SD.Bitmap(Width, Height)) 
      { 

       bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution); 

       using (SD.Graphics Graphic = SD.Graphics.FromImage(bmp)) 
       { 

        Graphic.SmoothingMode = SmoothingMode.AntiAlias; 

        Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic; 

        Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality; 

        Graphic.DrawImage(OriginalImage, new SD.Rectangle(0, 0, Width, Height), X, Y, Width, Height, SD.GraphicsUnit.Pixel); 

        MemoryStream ms = new MemoryStream(); 

        bmp.Save(ms, OriginalImage.RawFormat); 

        return ms.GetBuffer(); 

       } 

      } 

     } 

    } 

    catch (Exception Ex) 
    { 

     throw (Ex); 

    } 

} 

}

+0

你将不得不更好地解释这一点。你有什么问题?你有什么尝试?你想做什么? – j08691 2012-01-10 01:49:03

+0

我试图裁剪图像。但选择部分不起作用。如果你看附件,叶子是选定的部分,但它显示了图像的一部分。我试着改变这行代码中的一些参数。 Graphic.DrawImage(OriginalImage,new SD.Rectangle(0,0,Width,Height),X,Y,Width,Height,SD.GraphicsUnit.Pixel – user575219 2012-01-10 06:36:17

回答

1

我有一个类似的问题一次我不记得确切,但现在我认为你有特别是如果你不显示图像的原始大小设置在jCrop的boxWidth选项(图像实际上是1024×768,但你的风格你栽跟头到350x350)

你也可能必须为jCrop设置aspectRatio,即使它是01,因为它需要根据图像大小重新计算尺寸。 也确保使用最新版本0.9.9(截至本文),并检查里面的JavaScript,如果它实际上是0.9.9,因为有些时候他没有正确更新链接..它让我感到...

我认为你的服务器代码是好的 - 但你需要检查在隐藏字段中设置的数字是什么,以便你可以调试问题。