0

我正在使用以下函数来加载位图图像

private BitmapImage fileNameBitMap(string filePath)
{
    if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
    {
        try
        {
            BitmapImage bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.CacheOption = BitmapCacheOption.OnLoad;


            bitmap.DecodePixelWidth = 200;
            bitmap.UriSource = new Uri(filePath);
            bitmap.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;

            bitmap.EndInit();
            image = bitmap;
            return bitmap;
        }
        catch
        {
            return null;
        }
    }
    return null;
}

当我调试 bitmap.SourceStream 时,我发现它等于 null,我还发现抛出了 FormatNotSupportedException。在将 BitmapImage 转换为字节 [] 的过程中,我需要将其存储在流中。

4

1 回答 1

1

您应该简单地将图像文件的内容读入byte[]

var bytes = File.ReadAllBytes(filePath);
于 2013-07-24T15:47:56.310 回答