2012-04-04 111 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()