c# - 使用StorageFile来获取位图尺寸,然后再使用它来读取图像?

标签 c# mvvm bitmapimage win-universal-app writeablebitmap

好的,我正在尝试做一些工作来分析WPF应用程序中图像的启发式。当用户选择图像文件位置时,我想在代码背后打开图像,检查图像的颜色,然后在应用程序中输出它们。但是,要正确检查图像的像素,我需要获取尺寸以将其加载到可以获取像素颜色的WriteableBitmap中。

当我第二次打开文件流时,该应用程序将挂起。这是代码:

    private static async Task<Dictionary<Color, int>> GetColorCountsAsync(string path)
    {
        var colorCounts = new Dictionary<Color, int>();

        var absolutePath = string.Format(@"{0}\{1}",Directory.GetCurrentDirectory(),path);

        var dimension = await GetBitmapDimensions(absolutePath); //Method below - opens via StorageFile

        var file = await StorageFile.GetFileFromPathAsync(absolutePath); //Hangs forever
        using (var stream = await file.OpenStreamForReadAsync().ConfigureAwait(false))
        {
            var pixelWidth = dimension.Width;
            var pixelHeight = dimension.Height;
            var bitmap = new WriteableBitmap(pixelWidth, pixelHeight);

            bitmap.SetSource(stream.AsRandomAccessStream());

            using (var buffer = bitmap.PixelBuffer.AsStream())
            {
                var pixels = new byte[4 * pixelWidth * pixelHeight];
                buffer.Read(pixels, 0, pixels.Length);

                for (var y = 0; y < pixelHeight; y++)
                {
                    for (var x = 0; x < pixelWidth; x++)
                    {
                        var index = ((y * pixelWidth) + x) * 4;

                        var alpha = pixels[index + 4];
                        var red = pixels[index + 2];
                        var green = pixels[index + 1];
                        var blue = pixels[index + 0];
                        var color = Color.FromArgb(alpha, red, green, blue);

                        if (!colorCounts.ContainsKey(color))
                        {
                            colorCounts.Add(color, 0);
                        }
                        colorCounts[color] = colorCounts[color] + 1;
                    }
                }
            }
        }

        return colorCounts;
    }
    private static async Task<Dimension> GetBitmapDimensions(string absolutePath)
    {
        var file = await StorageFile.GetFileFromPathAsync(absolutePath);
        var bitmapImage = new BitmapImage();
        using (var fileStream = await file.OpenAsync(FileAccessMode.Read))
        {
            bitmapImage.SetSource(fileStream);
        }

        return new Dimension { Height = bitmapImage.PixelHeight, Width = bitmapImage.PixelWidth };
    }

我无法关闭位图图像也无法处理它-是什么导致应用程序冻结?

最佳答案

您不需要正确尺寸的位图,因为SetStream()是扩展方法。它只需要一个对象,就这样调用它:

  var bitmap = new WriteableBitmap(1,1).SetSource(stream.AsRandomAccessStream());

关于c# - 使用StorageFile来获取位图尺寸,然后再使用它来读取图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32811082/

相关文章:

c# - 多层应用程序中的依赖注入(inject)

WPF + MVVM - 访问用户控件中的对象/属性

android - 如何将绘画图像转换为位图

c# - 在 BackgroundWorker 中加载图片

c# - 用于图表应用程序的 html5/JavaScript 或 WPF/C#

c# - 我是否必须包含所有 System.Collections.Immutable 依赖项?

c# - UWP:如何将参数传递给导航页面的ViewModel?

android - 如何在ImageView中显示位图后获取位图的大小

c# - 如何从 .NET Core 上的 WCF SOAP 客户端记录 http 级流?

c# - c# 中是否有等效于 php array_merge 的函数