简体   繁体   English

Windows Phone 8.1的自定义Xaml裁剪控件

[英]Custom Xaml crop control for windows phone 8.1

I am writing a Windows Phone 8.1 app that requires me to crop an image to produce another square image (250 by 250) . 我正在编写Windows Phone 8.1应用,要求我裁剪图像以生成另一个正方形图像(250 x 250) I need xaml and code behind sample code that I can be use to do the cropping. 我需要xaml和示例代码后面的代码 ,可以用来进行裁剪。 The examples found online are for the old WP8 and they are not very helpful.Any assistance will be appreciated. 在网上找到的示例是针对 WP8的,它们并不是很有帮助,将为您提供任何帮助。

I did find a solution, there is this article I came across and it has two parts. 我确实找到了解决方案,我遇到了这篇文章,它分为两部分。 Part 1 and Part 2 proved helpful in finding a solution with minimal modifications such as using WriteableBitmapEx methods for cropping instead. 事实证明, 第1部分第2部分有助于找到一种修改最少的解决方案,例如使用WriteableBitmapEx方法进行裁剪。 I hope this will help someone in future and save them the pain I went through. 我希望这会在将来对某人有所帮助,并减轻他们遭受的痛苦。

Here is also some code I used to center crop my image 这也是我用来集中裁剪图像的一些代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM