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

时间:2015-12-18 18:56:07

标签: c# image uwp image-quality

我的代码允许用户使用文件选择器&我将图像转换为字节数组,以便可以序列化。查看图像集时,如果我有太多图像,应用程序崩溃&不会加载。当我使用视觉工作室进行调试时,它会运行罚款但需要很长时间来加载图像。我相信它会超时,因为它需要很长时间才能加载。获取存储文件后。如何降低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<byte[]> 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;
    }

1 个答案:

答案 0 :(得分: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);
}

对于序列化,字节数组像素应该更小,但图像质量会更差。