2

I try to load large image from a .RAW file into a System.Windows.Controls.Image by this code:

Dim pf As PixelFormat = PixelFormats.Gray16
Dim rawStride As Integer = Convert.ToInt32((Convert.ToInt32(RAWImage.ImageSize.Width) * pf.BitsPerPixel + 7) \ 8)
bitmap = BitmapSource.Create(Convert.ToInt32(RAWImage.ImageSize.Width), Convert.ToInt32(RAWImage.ImageSize.Width), 96, 96, _
                                                         pf, Nothing, ImagePixelvalues, rawStride)
MainPictureBox.Source = bitmap

where ImagePixelvalues is uint16(). My problem is about memory leakage caused by bitmapsource. if I load for example 100 times RAW image in the Image Control, Windows Task Manager Performance shows that the Physical memory of the system increasing an after a while. This means that previous bitmapsourceare not released and GC cannot collect them. Please let me know your idea about this How can i release bitmapsource?

user3104119
  • 33
  • 1
  • 6
  • How do you know that BitmapSource is responsible for increasing memory consumption? It could as well be your ImagePixelvalues. You should consider using a memory profiler. – Clemens Oct 28 '15 at 12:16
  • How are you releasing the image? Are you trying to force GC, or did you try any trick (ex: setprocessworkingsetsize)? – Caveman Oct 28 '15 at 12:37
  • @Clemens: I null the ImagePixelValue but the problem still remain. – user3104119 Oct 28 '15 at 12:47
  • @Caveman: I used GC.Collection() but there is no thing happened I do not any thing about setprocessworkingsetsize!!! – user3104119 Oct 28 '15 at 12:49
  • There are similar problems here but no one can solved it: http://stackoverflow.com/questions/1714841/image-loading-memory-leak-with-c-sharp?rq=1 http://stackoverflow.com/questions/1684489/how-do-you-make-sure-wpf-releases-large-bitmapsource-from-memory?rq=1 http://stackoverflow.com/questions/2428146/memory-leak-while-asynchronously-loading-bitmapsource-images?rq=1 – user3104119 Oct 28 '15 at 12:50
  • @Clemens,Caveman: I build up another test. I assign a window with this Image as child to a parent window, When I close child physical memory does not released this test may be describe the problem more !! – user3104119 Oct 28 '15 at 12:56

1 Answers1

0

When you're done using each bitmap, you could try

   bitmap.Dispose
David Wilson
  • 4,264
  • 3
  • 16
  • 30
  • Dear David Bitmap Class has no Dispose method and this is my problem I cannot dispose bitmap :D – user3104119 Nov 03 '15 at 12:12
  • I meant the bitmap variable you defined – David Wilson Nov 03 '15 at 12:59
  • I use bitmapsource of WPF, I confused about your solution I do not define a new bitmap class – user3104119 Nov 03 '15 at 13:06
  • This line.. MainPictureBox.Source = bitmap.. Bitmap is a variable isn't it? – David Wilson Nov 03 '15 at 13:09
  • yes sure: `Dim bitmap As BitmapSource = BitmapSource.Create(Convert.ToInt32(ImageSize.Width), Convert.ToInt32(ImageSize.Height), 96, 96, _ pf, Nothing, PixelValues, rawStride)` – user3104119 Nov 04 '15 at 07:53
  • you should be able to dispose that then after setting it to nothing - Something else I read about by the way was to turn off bitmap image caching - https://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapcacheoption(v=vs.110).aspx - though I don't know if this also affects bitmapsource.. I see nobody else has been able to answer your question yet either :-/ – David Wilson Nov 04 '15 at 21:07