为什么图像的流媒体源不起作用?

问题描述:

我使用以下代码来传输图像源:

I am using the following code to stream an image source:

        BitmapImage Art3 = new BitmapImage();
        using (FileStream stream = File.OpenRead("c:\\temp\\Album.jpg"))
        {
            Art3.BeginInit();
            Art3.StreamSource = stream;
            Art3.EndInit();
        }
        artwork.Source = Art3;

artwork是应该显示图像的XAML对象。该代码不应该锁定图像,它不会将其锁定好,但也不显示它,默认图像变为无...我的猜测是我没有正确使用流,并且我的图像变为空。帮助?

"artwork" is the XAML object where image is supposed to be shown. The code is supposed not to lock up the image, it doesn't lock it up alright, but doesn't show it either and the default image becomes "nothing"... My guess is that I am not properly using the stream, and that my image becomes null. Help?

更新:

我现在使用以下代码,朋友向我建议:

I am now using the following code which a friend suggested to me:

        BitmapImage Art3 = new BitmapImage();

        FileStream f = File.OpenRead("c:\\temp\\Album.jpg");

        MemoryStream ms = new MemoryStream();
        f.CopyTo(ms);
        f.Close();

        Art3.BeginInit();
        Art3.StreamSource = ms;
        Art3.EndInit();   

        artwork.Source = Art3;

由于某些奇怪的原因,此代码返回以下错误:

For some strange reason, this code returns the following error:


图像无法解码。图片标题可能已损坏。

The image cannot be decoded. The image header might be corrupted.

我做错了什么?我确定我尝试加载的图像没有损坏。

What am I doing wrong? I am sure the image I am trying to load is not corrupt.

我设法通过使用以下代码解决了问题:

I managed to solve the problem by using the following code:

        BitmapImage Art3 = new BitmapImage();

        FileStream f = File.OpenRead("c:\\temp\\Album.jpg");

        MemoryStream ms = new MemoryStream();
        f.CopyTo(ms);
        ms.Seek(0, SeekOrigin.Begin);
        f.Close();

        Art3.BeginInit();
        Art3.StreamSource = ms;
        Art3.EndInit();   

        artwork.Source = Art3; 

感谢所有试图帮助我的人!

Thanks everyone who tried to help me!