none
DirectoryNotFoundException occured when creating a BitmapFrame

    Question

  • I Have the following code (put in a UnitTestMethod):


     [TestMethod]
     public void TestMethod1()
     {
       var image = new BitmapImage();
       image.BeginInit();
       image.UriSource = new Uri("/UnitTestProject1;component/Pic.PNG", UriKind.RelativeOrAbsolute);
       image.EndInit();
    
       using (var stream = new MemoryStream())
       {
          var encoder = new PngBitmapEncoder();
          encoder.Frames.Add(BitmapFrame.Create(image)); // <== An exception of type 'System.IO.DirectoryNotFoundException' occurred 
                                                                   // in PresentationCore.dll but was not handled in user code
                                                                   // Additional information: Could not find a part of the path 
                                                                   // 'D:\UnitTestProject1;component\Pic.PNG'.
    
          encoder.Save(stream);
          var buffer = stream.ToArray();
       }
    }

    BitmapFrame.Create caused a DirectoryNotFoundException. The questions are:

     Why?And what can I do to solve the issue?

    BTW: Of cause there is a PNG Image with the name Pic.PNG inside my UnitTest project with Build Action "Resource".



    Friday, February 12, 2016 1:25 PM