2012-04-04 4 views
0

Ich arbeite auf asp.net mit C#. Ich muss ein Bild anpassen, indem ich einen Teil dieses Bildes nehme. Ich möchte einen Teil des Bildes aus der Mitte wie in der folgenden Abbildung zuschneiden.Crop ein Bild in asp.net mit C#

enter image description here

kann mir jemand helfen, bitte.

+0

Was haben Sie versucht? Warum versuchst du nicht eine Google-Suche? http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing –

Antwort

1

Ich habe dies getan, indem ich die Koordinaten des Bildteils von Jquery bekommen habe.

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); 
    }; 

Dann habe ich diese Koordinaten zu Ernte Bild in C# wie

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; 
     } 
    } 

Das funktioniert gut

+0

Was macht GenerateCroppedThumbNail? Warum zeigst du das nicht als Teil deiner Antwort an, da es der wichtigste Teil zu sein scheint – link64

0

Wenn Sie es auf dem Server tun, empfehle ich die Verwendung von a server-safe wrapper anstelle von System.Drawing direkt, so you don't have to worry about avoiding the 29+ pitfalls and bugs.

My ImageResizing.Net library offers both automatic and manual cropping

Automatische

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

Manual (durch Prozentsätze)

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

Manual (in Quellbildkoordinaten)

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