如何在UWP C#中裁剪SoftwareBitmap

时间:2017-10-03 12:17:26

标签: c# uwp hololens

我使用MediaCapture对象拍了一张照片并获得了一个SoftwareBitmap。 现在我想剪裁图像。 我该怎么做?

编辑 - 这是我到目前为止所尝试的内容:

    public async Task<SoftwareBitmap> CorpSoftwareBitmap(SoftwareBitmap original ,float scale)
    {
        int startIndex = 54;
        int M = original.PixelWidth;
        int N = original.PixelHeight;
        int m = (int)Math.Round(M * scale);
        int n = (int)Math.Round(N * scale);

        byte[] arrayOriginal;
        using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
        {
            BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId,ms);
            encoder.SetSoftwareBitmap(original);
            await encoder.FlushAsync();

            arrayOriginal = new byte[ms.Size];
            await ms.ReadAsync(arrayOriginal.AsBuffer(), (uint)ms.Size, InputStreamOptions.None);
        }

        byte[] arrayCropped;
        SoftwareBitmap cropped = new SoftwareBitmap(original.BitmapPixelFormat, m, n);
        using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
        {
            BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, ms);
            encoder.SetSoftwareBitmap(cropped);
            await encoder.FlushAsync();
            arrayCropped = new byte[ms.Size];
            await ms.ReadAsync(arrayCropped.AsBuffer(), (uint)ms.Size, InputStreamOptions.None);
        }

        int tmp = startIndex + 2 * (N - n) * M + 2 * (M - m);
        for (int i=0; i<n;i++)
        {
            Array.ConstrainedCopy(arrayOriginal, tmp + 4 * M * i, arrayCropped,startIndex + m * i, m);
        }

        cropped.CopyFromBuffer(arrayCropped.AsBuffer());


        //using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
        //{
        //    ms.GetInputStreamAt(0);
        //    BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, ms);
        //    encoder.SetSoftwareBitmap(cropped);
        //    await encoder.FlushAsync();
        //    await ms.WriteAsync(arrayCropped.AsBuffer());
        //}

        return cropped;
    }

它并没有将arrayCropped写入裁剪。两种方式都不起作用,我该如何解决?

谢谢, MOR

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

好的,这个有效:

public function test()
{

        if($this->form_validation->run('test') == TRUE) 
        {
                echo "working";
        }
$this->load->view('test1');
}