BitmapSource 是唯一可以在图像源中使用的类型吗?
我们可以使用Bitmapsource对象作为Image控件的内容,但是如果我只有Bitmap对象,我可以直接使用它吗,如果我使用以下方法将Bitmap转换为Bitmapsouce:
Bitmap bitmap = imageObjToBeConvert;
IntPtr HBitmap = bitmap.GetHbitmap();
result = Imaging.CreateBitmapSourceFromHBitmap
(HBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
DeleteObject(HBitmap);
bitmap.Dispose();
需要一些时间。 有没有办法直接使用Bitmap?
而且我发现BitmapSource似乎不能直接释放,是否有一个示例方法可以在Bitmapsouce不再使用后立即释放内存。
We can use Bitmapsource object as the content of a Image control, However if I only have Bitmap object, can I use it directly, if I convert Bitmap to Bitmapsouce using the following method:
Bitmap bitmap = imageObjToBeConvert;
IntPtr HBitmap = bitmap.GetHbitmap();
result = Imaging.CreateBitmapSourceFromHBitmap
(HBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
DeleteObject(HBitmap);
bitmap.Dispose();
It take some time. Is there any way to use Bitmap directly?
And I found BitmapSource seems can't be released directly, Is there a sample method to release the memory immediately, after the Bitmapsouce is not used again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,您不能直接使用
Bitmap
。 这是因为System.Drawing.Bitmap
是 GDI+ 位图,而System.Windows.Media.Imaging
中的BitmapImage
等类是 D3D/DirectX 。关于内存使用情况,您无法直接释放它,但通常会在后台进行某种缓存。 您可以通过多种方式加载
BitmapImage
,以便它忽略缓存。No, you cannot use
Bitmap
directly. This is becauseSystem.Drawing.Bitmap
is a GDI+ bitmap, while classes likeBitmapImage
inSystem.Windows.Media.Imaging
are D3D/DirectX.Regarding memory usage, you can't release it directly, but there is usually some sort of caching that goes in the background. There are ways that you can load a
BitmapImage
so that it ignores the cache.所有这些类都继承自 BitMapSource,因此它们中的任何一个都可以在该上下文中使用。
All of these classes inherit from BitMapSource, so any of them could be used in that context.