【问题标题】:Use StorageFile to get Bitmap Dimensions before using it to read image?使用 StorageFile 在使用它读取图像之前获取位图尺寸?
【发布时间】:2015-09-27 17:59:28
【问题描述】:

好的,我正在尝试分析 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 };
    }

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-29
      • 1970-01-01
      • 2013-07-19
      • 2018-07-31
      • 1970-01-01
      相关资源
      最近更新 更多