bitmapsource

Image loading memory leak with C#

一曲冷凌霜 提交于 2019-11-27 02:47:32
问题 I have a memory leak issue in my application which loads a large amount of images. I'm rather new to C#, and thought my days of memory leak issues were past. I can't figure out the problem - maybe I'm using some unmanaged modules which I'm not handle correctly? To illustrate my problem I've simplified the core of what causes the problem and moved this to a clean project. Note that this is all silly code which doesn't reflect the original application it came from. In the test application I

Why does BitmapSource.Create throw an ArgumentException?

白昼怎懂夜的黑 提交于 2019-11-26 16:39:35
问题 I'm trying to get an bitmap created from raw data to show in WPF, by using an Image and a BitmapSource: Int32[] data = new Int32[RenderHeight * RenderWidth]; for (Int32 i = 0; i < RenderHeight; i++) { for (Int32 j = 0; j < RenderWidth; j++) { Int32 index = j + (i * RenderHeight); if (i + j % 2 == 0) data[index] = 0xFF0000; else data[index] = 0x00FF00; } } BitmapSource source = BitmapSource.Create(RenderWidth, RenderHeight, 96.0, 96.0, PixelFormats.Bgr32, null, data, 0); RenderImage.Source =

Is there a good way to convert between BitmapSource and Bitmap?

霸气de小男生 提交于 2019-11-26 13:06:36
As far as I can tell the only way to convert from BitmapSource to Bitmap is through unsafe code... Like this (from Lesters WPF blog ): myBitmapSource.CopyPixels(bits, stride, 0); unsafe { fixed (byte* pBits = bits) { IntPtr ptr = new IntPtr(pBits); System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap( width, height, stride, System.Drawing.Imaging.PixelFormat.Format32bppPArgb,ptr); return bitmap; } } To do the reverse: System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty,

Is there a good way to convert between BitmapSource and Bitmap?

感情迁移 提交于 2019-11-26 03:38:31
问题 As far as I can tell the only way to convert from BitmapSource to Bitmap is through unsafe code... Like this (from Lesters WPF blog): myBitmapSource.CopyPixels(bits, stride, 0); unsafe { fixed (byte* pBits = bits) { IntPtr ptr = new IntPtr(pBits); System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap( width, height, stride, System.Drawing.Imaging.PixelFormat.Format32bppPArgb,ptr); return bitmap; } } To do the reverse: System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows

fast converting Bitmap to BitmapSource wpf

泪湿孤枕 提交于 2019-11-26 02:37:52
问题 I need to draw an image on the Image component at 30Hz. I use this code : public MainWindow() { InitializeComponent(); Messenger.Default.Register<Bitmap>(this, (bmp) => { ImageTarget.Dispatcher.BeginInvoke((Action)(() => { var hBitmap = bmp.GetHbitmap(); var drawable = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); DeleteObject(hBitmap); ImageTarget.Source = drawable; })); }); } The problem is, with