C# 如何在uwpc中裁剪软件位图#

C# 如何在uwpc中裁剪软件位图#,c#,uwp,hololens,C#,Uwp,Hololens,我用MediaCapture对象拍摄了一张照片,得到了一个软件位图。 现在我要裁剪图像。 我该怎么做 编辑这是我到目前为止尝试的: public async Task<SoftwareBitmap> CorpSoftwareBitmap(SoftwareBitmap original ,float scale) { int startIndex = 54; int M = original.PixelWidth; int

我用MediaCapture对象拍摄了一张照片,得到了一个软件位图。 现在我要裁剪图像。 我该怎么做

编辑这是我到目前为止尝试的:

    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;
    }
公共异步任务CorpSoftwareBitmap(软件位图原始,浮动比例)
{
int startIndex=54;
int M=原始像素宽度;
int N=原始像素高度;
int m=(int)数学圆(m*标度);
int n=(int)数学四舍五入(n*scale);
字节[]数组原始;
使用(InMemoryRandomAccessStream ms=new InMemoryRandomAccessStream())
{
BitmapEncoder编码器=等待BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId,ms);
编码器.设置软件EBITMAP(原件);
等待编码器。FlushAsync();
arrayOriginal=新字节[ms.Size];
等待ms.ReadAsync(arrayOriginal.AsBuffer(),(uint)ms.Size,InputStreamOptions.None);
}
字节[]数组被删除;
软件位图裁剪=新的软件位图(原始.BitmapPixelFormat,m,n);
使用(InMemoryRandomAccessStream ms=new InMemoryRandomAccessStream())
{
BitmapEncoder编码器=等待BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId,ms);
编码器。设置软件EBITMAP(裁剪);
等待编码器。FlushAsync();
arrayCropped=新字节[ms.Size];
等待ms.ReadAsync(arrayCropped.AsBuffer(),(uint)ms.Size,InputStreamOptions.None);
}
int tmp=startIndex+2*(N-N)*M+2*(M-M);

对于(inti=0;i有一些现有的extend控件来执行此操作

例如:

源代码:

好的,这个可以工作:

    public async Task<SoftwareBitmap> CorpSoftwareBitmap(SoftwareBitmap original, float scale=0.5f)
    {
        int startIndex = 54;//Bitmap header length
        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,4 * m * (n-1-i), 4 * m);

        cropped.Dispose();
        cropped = SoftwareBitmap.CreateCopyFromBuffer(arrayCropped.AsBuffer(), BitmapPixelFormat.Bgra8, m, n);
        return cropped;
    }
公共异步任务CorpSoftwareBitmap(软件位图原始,浮动比例=0.5f)
{
int startIndex=54;//位图头长度
int M=原始像素宽度;
int N=原始像素高度;
int m=(int)数学圆(m*标度);
int n=(int)数学四舍五入(n*scale);
字节[]数组原始;
使用(InMemoryRandomAccessStream ms=new InMemoryRandomAccessStream())
{
BitmapEncoder编码器=等待BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId,ms);
编码器.设置软件EBITMAP(原件);
等待编码器。FlushAsync();
arrayOriginal=新字节[ms.Size];
等待ms.ReadAsync(arrayOriginal.AsBuffer(),(uint)ms.Size,InputStreamOptions.None);
}
字节[]数组被删除;
软件位图裁剪=新的软件位图(原始.BitmapPixelFormat,m,n);
使用(InMemoryRandomAccessStream ms=new InMemoryRandomAccessStream())
{
BitmapEncoder编码器=等待BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId,ms);
编码器。设置软件EBITMAP(裁剪);
等待编码器。FlushAsync();
arrayCropped=新字节[ms.Size];
等待ms.ReadAsync(arrayCropped.AsBuffer(),(uint)ms.Size,InputStreamOptions.None);
}
int tmp=startIndex+2*(N-N)*M+2*(M-M);
对于(int i=0;i
到目前为止你都试了些什么?对不起,我是新来的lol。更新了问题。如果有人能提高效率,那就更好了这就是你想要的: