Search code examples
c#windows-phone-8loading-image

Loading an image file in WP 8 application from directory


i am developing a WP 8 application and i want to load an image which is currently on my computer drive. Here is my Code

  try
        {
            using (FileStream fileStream = File.OpenRead("\\TiltFilter\\FilterEffects\\Assets\\AlignmentGrid.png")) 
            {
                MemoryStream memStream = new MemoryStream();
                memStream.SetLength(fileStream.Length);
                fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
            }
        }
        catch (Exception e)
        {
            string str = e.Message;
        }

It gives me exception that of Type

System.Io.DirectoryNotFoundexceptionand the message isCould not find a part of the path 'C:\TiltFilter\FilterEffects\Assets\AlignmentGrid.png'.

Can some body help me that how i can load the image in memorystream on WP8

Thanks


Solution

  • You need to add an image to your project as Content and use GetResourceStream to access a stream of an image:

    var resource = App.GetResourceStream(new Uri("Assets/AlignmentGrid.png", UriKind.Relative));
    var buffer = new byte[resource.Stream.Length];
    resource.Stream.Read(buffer, 0, buffer.Length);