2011-05-07 276 views
2

有沒有人知道如何在沒有任何圖書館的情況下在silverlight中裁剪圖像。Silverlight圖像裁剪

我有孩子窗口和子窗口裏面我有一個圖像和這個圖像中心一個rectange在那裏,所以我可以平移圖像到周圍的直腸,並選擇圖像的特定部分,這個選定的部分我想裁剪。

此外,我正在使用WriteableBitmap,並嘗試裁剪,如果我錯了,如果正確的話,這將無法正常工作。

sheetRectangle.Children是圖像。

  foreach (ucPicRect item in sheetRectangle.Children) 
      { 
       WriteableBitmap obj = new WriteableBitmap(item.imgCell.Source as BitmapSource); 
       obj.Crop(0,0,400,400); 
       obj.Invalidate(); 
       item.imgCell.Effect = dlgcwEditPhoto.imgEdit.Effect; 
       item.imgCell.Source = obj;// dlgcwEditPhoto.imgEdit.Source; 

      } 

謝謝...!

回答

0

您可以使用此效用函數以裁剪圖像

public static WriteableBitmap cropImage(Image image, double[] coordonnee) 
    { 
     Image cloneImage = new Image(); 
     cloneImage.Source = image.Source; 
     RectangleGeometry myRec = new RectangleGeometry(); 
     myRec.Rect = new Rect(coordonnee[0], coordonnee[1], coordonnee[2], coordonnee[3]); 
     cloneImage.Clip = myRec; 
     TranslateTransform t = new TranslateTransform(); 
     t.X = -coordonnee[0]; 
     t.Y = -coordonnee[1]; 
     WriteableBitmap wb = new WriteableBitmap(cloneImage, t); 
     wb.Invalidate(); 
     return wb; 

    } 

好運!