简体   繁体   English

UWP:如何调整图像大小

[英]UWP: How to resize an Image

I have a JPEG image stored in a Byte[] that I want to resize. 我有一个JPEG图像存储在Byte []中,我想调整大小。 This is my code: 这是我的代码:

public async Task<byte[]> ResizeImage(byte[] imageData, int reqWidth, int reqHeight, int quality)
{

    var memStream = new MemoryStream(imageData);

    IRandomAccessStream imageStream = memStream.AsRandomAccessStream();
    var decoder = await BitmapDecoder.CreateAsync(imageStream);
    if (decoder.PixelHeight > reqHeight || decoder.PixelWidth > reqWidth)
    {
        using (imageStream)
        {
            var resizedStream = new InMemoryRandomAccessStream();

            BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(resizedStream, decoder);
            double widthRatio = (double) reqWidth/decoder.PixelWidth;
            double heightRatio = (double) reqHeight/decoder.PixelHeight;

            double scaleRatio = Math.Min(widthRatio, heightRatio);

            if (reqWidth == 0)
                scaleRatio = heightRatio;

            if (reqHeight == 0)
                scaleRatio = widthRatio;

            uint aspectHeight = (uint) Math.Floor(decoder.PixelHeight*scaleRatio);
            uint aspectWidth = (uint) Math.Floor(decoder.PixelWidth*scaleRatio);

            encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Linear;

            encoder.BitmapTransform.ScaledHeight = aspectHeight;
            encoder.BitmapTransform.ScaledWidth = aspectWidth;

            await encoder.FlushAsync();
            resizedStream.Seek(0);
            var outBuffer = new byte[resizedStream.Size];
            uint x =  await resizedStream.WriteAsync(outBuffer.AsBuffer());
            return outBuffer;
        }
    }
    return imageData;
}

The problem is that outBuffer just contains zeros although the correct number of Bytes have been written. 问题是outBuffer只包含零,尽管已写入正确的字节数。

Just replace your line from this 只需从中更换您的线路即可

uint x =  await resizedStream.WriteAsync(outBuffer.AsBuffer());

to this code: 这段代码:

await resizedStream.ReadAsync(outBuffer.AsBuffer(), (uint)resizedStream.Size, InputStreamOptions.None);

I stumbled upon the same problem last week and ended up with a solution that looks like this. 我上周偶然发现了同样的问题,最终得到了一个看起来像这样的解决方案。

private async Task<StorageFile> RescaleImage(StorageFile sourceFile, StorageFile resizedImageFile,uint width,uint height)
{
    var imageStream = await sourceFile.OpenReadAsync();
    var decoder = await BitmapDecoder.CreateAsync(imageStream);
    using (var resizedStream = await resizedImageFile.OpenAsync(FileAccessMode.ReadWrite))
    {   
        var encoder = await BitmapEncoder.CreateForTranscodingAsync(resizedStream, decoder);
        encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Linear;
        encoder.BitmapTransform.ScaledWidth = width;
        encoder.BitmapTransform.ScaledHeight = height;
        await encoder.FlushAsync();
    }
    return resizedImageFile;
}

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

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