Link to home
Start Free TrialLog in
Avatar of mem10

asked on

How would this be handled in memory?

Let's say that Image1, Image2, and Image3 are Image controls in Visual Basic 6 and I do the following:

Image1.Picture = LoadPicture("somePic.bmp")

Image2.Picture = Image1.Picture
Image3.Picture = Image1.Picture

At this point, I believe, Image2 and Image3 don't have separate copies of "somePic.bmp" because they both refer to Image1's copy of it. Now what if I change the picture that is in Image1, like so:

Image1.Picture = LoadPicture("newPic.bmp")

What happens to Image2 and Image3 memory-wise? They both refered to the picture that was originally in Image1, but that picture has changed. Do Image2 and Image3 suddenly each get a separate copy in GDI space of the original picture and, thus, more memory gets used up?
Avatar of osmodean

The image control gets the binary data of the picture by copying it and saving it in the control.
This means that Image1, Image2 and Image3 all have a different picture and changing one of them does not change the others.
There is no reference copied.

Your original synopsis was incorrect. Every image control loaded retains its own memory, meaning, image 1,2, 3 all are consuming equal amounts of memory, if you change the picture of say image1, the memory usage will only adjust according to the difference of image size between the original and new picture.

Avatar of mem10


According to msdn, it appears that Image2 and Image3 do share with Image1.  At 

it says the following:

  "You can share the same picture between multiple picture     controls, image controls, and forms. If you use code like this  you only maintain one copy of the picture:

Picture = LoadPicture("C:\Windows\Chess.gif")
Image1.Picture = Picture ' Use the same picture.
Picture1.Picture = Picture ' Use the same picture.  "

So, in the code above, the Picture property of the form is set to "C:\Windows\Chess.gif" and then apparently Image1 and Picture1 share with it.
Avatar of mugman21

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial