通过以下方式,我能够阅读。
但是没有处理方法,所以我以后无法删除该文件。
所以下面的方法失败了。
我无法找到合适的解决方案。
在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;
}
}
答案 0 :(得分: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
读取字节,那么如果您想要从文件中读取所有字节,那么总是this solution。