views:

2575

answers:

2

Hello Everyone,

I am trying to update an Image (_browserScreenshot below) object in XAML by changing the source image every time an event determines the source needs updating. Right now I have this:

public BitmapSource GetScreen()
        {
            Bitmap bitmap = new Bitmap(app.Browser.ClientRectangle.Width, app.Browser.ClientRectangle.Height);
            app.Browser.DrawToBitmap(bitmap, app.Browser.Bounds);

            BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

            bitmapSource.Freeze();

            bitmap.Dispose();
            bitmap = null;
            return bitmapSource;
        }

Then I have an event handler as shown:

app.BitmapSource.Changed += new EventHandler(BitmapSource_Changed);

void BitmapSource_Changed(object sender, EventArgs e)
        {
                Window1._browserScreenshot.Source = app.GetScreen();
        }

Now whenever this event fires a new screenshot is taken and the source of the Image (called _browserScreenshot here) control should be updated. I keep getting an error about changing the IsFrozen propery, but I can't figure out how to change this correctly and have this work the way I want it to. Thanks in advance everyone.

Bob

A: 

The following line turned out to be my problem:

bitmapSource.Freeze();

Beaker
A: 

In all likelyhood you want to Freeze the object. The problem you are having is that you want to create a completely new BitmapSource every time and let the garbage collector dispose of the old image.

Steven