更新SIlverlight 4.0下的WPF Image问题

发布于 2024-12-25 16:54:43 字数 1433 浏览 2 评论 0原文

当我的页面加载并按下 button1 时,我可以获得图像并可以看到它。

但当我第二次点击它时,它根本不起作用。我调试了它 button1_Click(...) 并且我确信 imageData != null

我真的不知道怎么了...请帮助我!

private void button1_Click(object button, RoutedEventArgs e)
{
    Guid sid = Guid.Parse("087462df-e4b6-484c-879e-cccc37b4c1f4");
    EntityQuery<Screenshot> screen = this._myDomainContext.GetScreenshotQuery(sid);
    this._myDomainContext.Load(screen).Completed += (sender, args) =>
    {
        try
        {
            byte[] imageData = (((LoadOperation<Screenshot>)sender).Entities.FirstOrDefault()).Screen;
            if (imageData != null)
            {
                BitmapImage img = Utilities.Graphics.GetImage(imageData);
                img.CreateOptions = BitmapCreateOptions.IgnoreImageCache;

                image1.Source = null;
                image1.Source = img;
            }
        }
        catch
        {
        }

    };
}

public static BitmapImage GetImage(byte[] rawImageBytes)
{
    BitmapImage imageSource = null;

    try
    {
        using (MemoryStream stream = new MemoryStream(rawImageBytes))
        {
            stream.Seek(0, SeekOrigin.Begin);
            BitmapImage b = new BitmapImage();
            b.SetSource(stream);
            imageSource = b;    
        }
    }
    catch  
    {
    }

    return imageSource;
}

When my page loads and I press button1 I can get the image and can see it.

But when I click it second time it doesn't work at all. I debugged it button1_Click(...) and I am sure that imageData != null.

I really can't figure out what's up... Please help me!

private void button1_Click(object button, RoutedEventArgs e)
{
    Guid sid = Guid.Parse("087462df-e4b6-484c-879e-cccc37b4c1f4");
    EntityQuery<Screenshot> screen = this._myDomainContext.GetScreenshotQuery(sid);
    this._myDomainContext.Load(screen).Completed += (sender, args) =>
    {
        try
        {
            byte[] imageData = (((LoadOperation<Screenshot>)sender).Entities.FirstOrDefault()).Screen;
            if (imageData != null)
            {
                BitmapImage img = Utilities.Graphics.GetImage(imageData);
                img.CreateOptions = BitmapCreateOptions.IgnoreImageCache;

                image1.Source = null;
                image1.Source = img;
            }
        }
        catch
        {
        }

    };
}

and

public static BitmapImage GetImage(byte[] rawImageBytes)
{
    BitmapImage imageSource = null;

    try
    {
        using (MemoryStream stream = new MemoryStream(rawImageBytes))
        {
            stream.Seek(0, SeekOrigin.Begin);
            BitmapImage b = new BitmapImage();
            b.SetSource(stream);
            imageSource = b;    
        }
    }
    catch  
    {
    }

    return imageSource;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲念泪 2025-01-01 16:54:43

尝试改变 Load 的过载:


this._myDomainContext.Load(screen, LoadBehavior.RefreshCurrent, true).Completed+= ...

Try changing the overload of Load:


this._myDomainContext.Load(screen, LoadBehavior.RefreshCurrent, true).Completed+= ...

临走之时 2025-01-01 16:54:43

我不完全确定您的问题的原因,但我对使用 BitmapImage 的代码有一些指导。

  1. 在将 image1.Source 属性分配给实际源之前,无需将其设置为 null
  2. 不要丢弃传递给 BitmapImage 的流,以防控件尝试从中读取数据。
  3. 我个人使用 BeginInit (...)StreamSourceEndInit(...) 使用 BitmapImage 时。

I'm not completely sure on the cause if your issue, but I have a few pointers on the code with regards to using BitmapImage's.

  1. There is no need to set the image1.Source property to null before assigning it with an actual source.
  2. Don't dispose the stream you pass to the BitmapImage in case the control is trying to read from it.
  3. I personally make use of BeginInit(...), StreamSource and EndInit(...) when working with BitmapImage's.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文