为什么图像的流媒体源不起作用? [英] Why is streaming source of an image not working?

查看:131
本文介绍了为什么图像的流媒体源不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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!

这篇关于为什么图像的流媒体源不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆