windows phone 8.1的自定义Xaml裁剪控制
本文关键字:Xaml 裁剪 控制 自定义 phone windows | 更新日期: 2023-09-27 18:07:16
我正在编写一个Windows Phone 8.1应用程序,要求我裁剪图像以产生另一个方形图像(250 × 250)。我需要xaml和示例代码后面的代码,我可以用来做裁剪。网上找到的示例是针对旧的 WP8的,它们不是很有帮助。任何帮助都将不胜感激。
我确实找到了一个解决方案,我看到了一篇文章,它有两个部分。第1部分和第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;
}