-1

With the way below i am able to read.

But there is no dispose method so i am not able to delete the file later.

So the below method is getting failed.

I could not come up with a proper solution.

Bitmap class is not recognized in C# 4.5 WPF application.

thank you

    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;
        }
     }
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342

1 Answers1

1

I assume the error you get is caused by trying to delete the file which is currently in use by the bitmap (I don't remember the exception name).

There is a solution to that, that is: making a byte stream.

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.

[EDIT] If you don't want to read the bytes by BinaryReader, there's always this solution if you want to read all bytes from the file.

Community
  • 1
  • 1
Patryk Ćwiek
  • 14,078
  • 3
  • 55
  • 76