0

I wrote a Windows application that loads thumbnail images from .jpg files on disk. The thumbnail pictureboxes are 100x128 pixels. The original images can be from 4kb to 2 mb in size. On my development machine it works fine, but the target machine is a Microsoft Surface with much less memory.

Here is the line that I use to load the images:

Dim imgSrc As Image = New Bitmap(Image.FromFile(<filename>), New Size(120, 128))

Thinking that this would rescale the image to a smaller size, but it apparently doesn't.

In Debug mode, when I look at the image's properties, the size is as specified in the line above, and the resolution is 96, values that are definitely smaller than the original image's properties.

Is this the wrong way to do it? Do I need to be clearing memory, or something?

EDIT: I should have checked this first, but both my development machine and the Surface show to have 8g of ram.

Ken White
  • 123,280
  • 14
  • 225
  • 444
Bill Norman
  • 883
  • 11
  • 29
  • Do you dispose your thumbnails at all? – GSerg Apr 21 '21 at 19:19
  • If you want to resize an Image like that (which will of course distort the original Image), then assign `Image.FromFile()` to a local Image object, create a new Bitmap from it, then dispose of the original object. -- Not clear what is the destination of `imgSrc`, but you'll have to dispose of that, too, at some point. – Jimi Apr 21 '21 at 19:22
  • @GSerg No, I don't, and this is all a little new to me so I don't know how to do that. – Bill Norman Apr 21 '21 at 19:33
  • @Jimi The destination is a picturebox. I added a line `imgSrc.Dispose` after the picture box loads it, but it throws an error in an entirely different part of the code. – Bill Norman Apr 21 '21 at 19:34
  • Nope, dispose of the Image you get from `Image.FormFile()` after you have created a `New Bitmap()` from it. After that, before you assign a new Image to a PictureBox, just `[PictureBox].Image?.Dipose() [PictureBox].Image = [A New Image]`. If you never dispose of the Images until the Container (Form/Window) is closed, assign the Bitmap you load to a `List(Of class)` that handles them. Otherwise, dispose of all the Images that still exist when the Container is about to close. -- Keep the Diagnostic Tools pane opened and do this in long sequences to see the difference. – Jimi Apr 21 '21 at 19:39
  • Since you're loading these pictures in a PictureBox, why not use `PictureBox.Load` and use the appropriate SizeMode? – David Apr 21 '21 at 20:38
  • @David I didn't think of that. Thanks! I'll try it out and let you know. I may no be able to get to it today. My Monday came on Wednesday this week. – Bill Norman Apr 21 '21 at 20:47
  • @David That loads the full sized Bitmap in memory: a 2MB JPEG may become a 20MB byte array (or more). What's needed here, is to carefully dispose of the Images loaded and, possibly, implement a method that resizes the Images proportionally. Better if the whole thing is handled using a specialized class. – Jimi Apr 21 '21 at 21:16
  • I tried it the way @David suggested, and it worked, but still with the error. What I did differently, though, is that I placed that part of the code in a try-catch block and left the Catch part blank, so it ignores the error. It seemed to load all of the picture boxes properly, and lets the program continue. (The previous method I was using wouldn't load all of the picture boxes.) I don't know if that's the appropriate way to handle it or not, but it works. We'll have to see how it does for a while. – Bill Norman Apr 22 '21 at 12:49
  • This is a program I wrote for my wife to use with a student. She tried it today and it worked fine. I'm still not sure what was causing the problem. – Bill Norman Apr 22 '21 at 20:17

0 Answers0