2016-09-16 183 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; 
    }