2012-04-04 180 views
0

我正在使用c#编写asp.net。我必须通过拍摄该图像的一部分来调整图像。 我想从下面的图像中裁剪一部分图像。使用c在asp.net中裁剪图像#

enter image description here

谁能帮我请。

+0

你尝试过这工作正常?你为什么不尝试谷歌搜索? http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing – 2012-04-04 13:39:02

回答

1

我已经通过Jquery获取了图像部分的坐标。

jQuery(function($) { 
     $('#target').Jcrop({ 
      onChange: showCoords, 
      onSelect: showCoords, 
      onRelease: clearCoords 
     }); 
    }); 
    function showCoords(c) { 
     $('#xaxis').val(c.x); 
     $('#yaxis').val(c.y); 
     $('#x2').val(c.x2); 
     $('#y2').val(c.y2); 
     $('#xwidth').val(c.w); 
     $('#div_width').val(c.w); 
     $('#yheight').val(c.h); 
     $('#div_height').val(c.h); 
    }; 
    function clearCoords() { 
     $('#coords input').val('0'); 
     $('#yheight').css({ color: 'red' }); 
     window.setTimeout(function() { 
      $('#yheight').css({ color: 'inherit' }); 
     }, 500); 
    }; 

然后我用在C#中这些坐标即可裁剪图片像

String savedFileName = uploadProfileImage(profileImageName, new System.Drawing.Rectangle(Int32.Parse(xaxis), Int32.Parse(yaxis), Int32.Parse(xwidth), Int32.Parse(yheight))); 

public String uploadProfileImage(string profileImageName, System.Drawing.Rectangle rectangle) 
    { 
     try 
     { 
      String retFileName = ""; 
      if (profileImageName != null || profileImageName != "") 
      { 
       GenerateCroppedThumbNail(profileImageName, rectangle); 
      } 
      return retFileName; 
     } 
     catch (Exception) 
     { 
      return String.Empty; 
     } 
    } 

+0

GenerateCroppedThumbNail做什么?为什么你不把它看作是你的答案的一部分,因为它似乎是最重要的部分 – link64 2014-02-21 01:18:24

0

如果你在服务器上这样做,我建议使用a server-safe wrapper而不是直接使用System.Drawing,so you don't have to worry about avoiding the 29+ pitfalls and bugs

ImageResizing.Net library offers both automatic and manual cropping

自动

new ImageJob(source,dest,new 
ResizeSettings("width=200;height=200;mode=crop;anchor=middlecenter")).Build(); 

手册(按百分比)

new ImageJob(source,dest,new 
ResizeSettings("crop=20,20,80,80;cropxunits=100;cropyunits=100")).Build(); 

手册(源图像坐标)

new ImageJob(source,dest,new 
ResizeSettings("crop=200,200,1000,1000;")).Build()