Dynamic loading of images in WPF

后端 未结 5 1890
执念已碎
执念已碎 2020-12-04 08:28

I have a strange issue with WPF, I was loading images from the disk at runtime and adding them to a StackView container. However, the images were not displayed. After some

5条回答
  •  攒了一身酷
    2020-12-04 09:03

    This is strange behavior and although I am unable to say why this is occurring, I can recommend some options.

    First, an observation. If you include the image as Content in VS and copy it to the output directory, your code works. If the image is marked as None in VS and you copy it over, it doesn't work.

    Solution 1: FileStream

    The BitmapImage object accepts a UriSource or StreamSource as a parameter. Let's use StreamSource instead.

            FileStream stream = new FileStream("picture.png", FileMode.Open, FileAccess.Read);
            Image i = new Image();
            BitmapImage src = new BitmapImage();
            src.BeginInit();
            src.StreamSource = stream;
            src.EndInit();
            i.Source = src;
            i.Stretch = Stretch.Uniform;
            panel.Children.Add(i);
    

    The problem: stream stays open. If you close it at the end of this method, the image will not show up. This means that the file stays write-locked on the system.

    Solution 2: MemoryStream

    This is basically solution 1 but you read the file into a memory stream and pass that memory stream as the argument.

            MemoryStream ms = new MemoryStream();
            FileStream stream = new FileStream("picture.png", FileMode.Open, FileAccess.Read);
            ms.SetLength(stream.Length);
            stream.Read(ms.GetBuffer(), 0, (int)stream.Length);
    
            ms.Flush();
            stream.Close();
    
            Image i = new Image();
            BitmapImage src = new BitmapImage();
            src.BeginInit();
            src.StreamSource = ms;
            src.EndInit();
            i.Source = src;
            i.Stretch = Stretch.Uniform;
            panel.Children.Add(i);
    

    Now you are able to modify the file on the system, if that is something you require.

提交回复
热议问题