在 WPF 中使用 BitmapSource 作为图像源
我试图通过每次事件确定源需要更新时更改源图像来更新 XAML 中的图像(下面的 _browserScreenshot)对象。 现在我有这个:
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;
}
然后我有一个事件处理程序,如下所示:
app.BitmapSource.Changed += new EventHandler(BitmapSource_Changed);
void BitmapSource_Changed(object sender, EventArgs e)
{
Window1._browserScreenshot.Source = app.GetScreen();
}
现在,每当此事件触发时,就会拍摄新的屏幕截图,并且应该更新图像(此处称为 _browserScreenshot)控件的源。 我不断收到有关更改 IsFrozen 属性的错误,但我不知道如何正确更改它并使其按照我想要的方式工作。 预先感谢大家。
I'm 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您很可能想要冻结该对象。 您遇到的问题是您希望每次都创建一个全新的 BitmapSource 并让垃圾收集器处理旧图像。
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.
以下行结果是我的问题:
bitmapSource.Freeze();
The following line turned out to be my problem:
bitmapSource.Freeze();