当前位置:  开发笔记 > 编程语言 > 正文

UWP:你如何降低所选图像的质量?

如何解决《UWP:你如何降低所选图像的质量?》经验,为你挑选了1个好方法。

我有一些代码,允许用户使用文件选择器选择图像,我将图像转换为字节数组,以便可以序列化.查看图像集时,如果我有太多,则应用程序崩溃并且不会加载.当我使用视觉工作室进行调试时,它会运行罚款但需要很长时间来加载图像.我相信它会超时,因为它需要很长时间才能加载.获取存储文件后.如何降低BitmapImage的质量,以便在序列化时加载时间不长?

 private async void AddPic_OnClick(object sender, RoutedEventArgs e)
    {
        ImagePath = string.Empty;
        FileOpenPicker openPicker = new FileOpenPicker();
        openPicker.ViewMode = PickerViewMode.Thumbnail;
        openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;

        openPicker.FileTypeFilter.Clear();
        openPicker.FileTypeFilter.Add(".bmp");
        openPicker.FileTypeFilter.Add(".jpg");
        openPicker.FileTypeFilter.Add(".jpeg");
        openPicker.FileTypeFilter.Add(".png");

        // Launch file open picker and caller app is suspended and may be terminated
        var file = await openPicker.PickSingleFileAsync();

        if (file != null)
        {
            var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
            var image = new BitmapImage();
            image.SetSource(stream);
            xamlimg.Source = image;
            _myClass.ImageBytes = await App.DataModel.GetBytesAsync(file);

        }
    }

其他课程详情:

public BitmapImage Image
    {
        get
        {
            if (ImageBytes == null)
                return null;
            if (_Image == null)
                _Image = new BitmapImage();
            using (var stream = new InMemoryRandomAccessStream())
            {
                stream.WriteAsync(ImageBytes.AsBuffer()).Completed = (i, j) =>
                {
                    stream.Seek(0);
                    _Image.SetSource(stream);
                };
            }
            return _Image;
        }
    }

    [JsonProperty]
    public byte[] ImageBytes { get; set; }

public async Task GetBytesAsync(StorageFile file)
    {
        byte[] fileBytes = null;
        if (file == null) return null;
        using (var stream = await file.OpenReadAsync())
        {
            fileBytes = new byte[stream.Size];
            using (var reader = new DataReader(stream))
            {
                await reader.LoadAsync((uint) stream.Size);
                reader.ReadBytes(fileBytes);
            }
        }
        return fileBytes;
    }

Igor Ralic.. 6

您可以对其进行解码并将其缩小为更小.因此,一旦从文件选择器获得结果,您就可以:

using (IRandomAccessStream fileStream = await result.OpenAsync(FileAccessMode.Read))
{
    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);
    using (var encoderStream = new InMemoryRandomAccessStream())
    {
        BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(encoderStream, decoder);
        var newHeight = decoder.PixelHeight / 2;
        var newWidth = decoder.PixelWidth / 2;
        encoder.BitmapTransform.ScaledHeight = newHeight;
        encoder.BitmapTransform.ScaledWidth = newWidth;

        await encoder.FlushAsync();

        byte[] pixels = new byte[newWidth * newHeight * 4];

        await encoderStream.ReadAsync(pixels.AsBuffer(), (uint)pixels.Length, InputStreamOptions.None);
}

然后,字节数组像素应该更小以进行序列化,但图像质量会更差.



1> Igor Ralic..:

您可以对其进行解码并将其缩小为更小.因此,一旦从文件选择器获得结果,您就可以:

using (IRandomAccessStream fileStream = await result.OpenAsync(FileAccessMode.Read))
{
    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);
    using (var encoderStream = new InMemoryRandomAccessStream())
    {
        BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(encoderStream, decoder);
        var newHeight = decoder.PixelHeight / 2;
        var newWidth = decoder.PixelWidth / 2;
        encoder.BitmapTransform.ScaledHeight = newHeight;
        encoder.BitmapTransform.ScaledWidth = newWidth;

        await encoder.FlushAsync();

        byte[] pixels = new byte[newWidth * newHeight * 4];

        await encoderStream.ReadAsync(pixels.AsBuffer(), (uint)pixels.Length, InputStreamOptions.None);
}

然后,字节数组像素应该更小以进行序列化,但图像质量会更差.


@bunkerdive因为每个像素都是RGBA,每个像素都用一个字节表示.
推荐阅读
oDavid_仔o_880
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有