-1

通过以下方式,我可以阅读。

但是没有 dispose 方法,所以我以后无法删除该文件。

所以下面的方法失败了。

我想不出一个合适的解决方案。

在 C# 4.5 WPF 应用程序中无法识别位图类。

谢谢你

    DirectoryInfo dInfo = new DirectoryInfo(@"C:\pokemon_files\images\");
    FileInfo[] subFiles = dInfo.GetFiles();

    BitmapImage myImg;
    foreach (var vrImage in subFiles)
    {
        string srFilePath = vrImage.FullName;
        System.Uri myUri = new Uri(srFilePath);
        myImg = new BitmapImage(myUri);

        if (myImg.Width < 50)
        {
            File.Delete(srFilePath);
            continue;
        }
     }
4

1 回答 1

1

我假设您得到的错误是由于尝试删除位图当前正在使用的文件引起的(我不记得异常名称)。

有一个解决方案,那就是:制作一个字节流。

byte[] imageData;

using(var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
using(var binaryReader = new BinaryReader(fileStream))
{
    imageData = binaryReader.ReadBytes((int)fileStream.Length);
}

var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = new MemoryStream(imageData);
bitmap.EndInit();

//Now you can check the width & height, the file stream should be closed so you can
//delete the file.

[编辑] 如果您不想通过 读取字节,如果您想从文件中读取所有字节,BinaryReader总是有这个解决方案。

于 2013-03-27T11:22:09.267 回答