2016-09-16 185 views
2

自定義的XAML作物控制我寫一個的Windows Phone 8.1應用程序,需要我作物(250 250)產生另一個正方形圖像的圖像。我需要xaml和示例代碼後面的代碼,我可以使用它來裁剪。在網上找到的例子是舊的 WP8,他們不是非常有用。任何幫助將不勝感激。Windows Phone的8.1 ​​

+0

請分享你的代碼迄今嘗試過或者你發現不適合你 –

+0

1. [裁切圖像(HTTP工作實例鏈接: //bsubramanyamraju.blogspot.co.ke/2014/03/windowsphone-8-crop-image-area-with.html)是wp8的舊帖子,不適用於wp8.1。 [用矩形裁剪圖像](http://bsubramanyamraju.blogspot.co.ke/2014/11/windowsphone-image-crop-with-rectangle.html)也適用於wp8。 –

+0

我唯一需要的是一個正方形(250像素),我可以在圖像上拖動,然後裁剪使用正方形選擇的圖像部分。我一直在嘗試自定義代碼,但沒有成功。我希望微軟會更加嚴肅並提供這樣的基本控制。 –

回答

0

我確實找到了一個解決方案,這篇文章是我碰到的,它有兩個部分。 Part 1Part 2被證明有助於通過最小的修改找到解決方案,例如使用WriteableBitmapEx方法進行修剪。我希望這將有助於未來的人,併爲他們挽救我所經歷的痛苦。

這裏也是一些代碼,我曾經居中裁剪我的形象

public static WriteableBitmap centerCropImage(WriteableBitmap image) 
    { 


     int originalWidth = image.PixelWidth; 
     int originalHeight = image.PixelHeight; 

     //Getting the new width 
     int newWidth = originalWidth > originalHeight? originalHeight : originalWidth; 

     //Calculating the cropping points 
     int cropStartX, cropStartY; 
     if(originalWidth > originalHeight){ 
      cropStartX = (originalWidth - newWidth)/2; 
      cropStartY = 0; 
     } 
     else{ 
      cropStartY = (originalHeight - newWidth)/2; 
      cropStartX = 0; 
     } 

     //Then use the following values to get the cropped image 

     var cropped = image.Crop(new Rect(cropStartX, cropStartY, newWidth, newWidth)); 

     //Then resize the new square image to 250 by 250 px 
     var resized = WriteableBitmapExtensions.Resize(cropped, 250, 250, WriteableBitmapExtensions.Interpolation.NearestNeighbor); 

     return resized; 
    }