Hi all!

I'm playing around a bit with my old Visual Basic 6.0. I'm working with images, and this rather strange error came up:

First I isolate a small piece of a larger picture to a smaller picturebox using PaintPicture: picBoxSprite.PaintPicture picBoxResource.Picture, 0, 0, 64, 64, (X * 64), (Y * 64), 64, 64 This works perfectly fine, and the smaller picture of 64x64 pixels is displayed like a charm.

Next, I want to copy this new picture from my picBoxSprite object (PictureBox) to one of my stretchable Image-objects. So I write the following line: imgDirt.Picture = picBoxSprite.Image This is where my problem occurs; nothing happens! imgDirt remains blank. If I pre-load a random bitmap into picBoxSprite before running the program, the copy operation works perfectly fine.

Why is this? And what can I do to make it work?

Thank you for your time!

Emil Olofsson

Dani AI

Generated

Short answer and why it looked like "nothing happened": PaintPicture draws into the control's in-memory bitmap/backing image (the PictureBox "Image"/hBitmap when you use AutoRedraw), not directly into the control's persistent StdPicture that other controls use. The Image buffer holds the pixels you see, but other controls (and SavePicture) expect a real Picture object — so you must promote the drawn image into the Picture property before copying. See the VB Helper example and the VB6 control notes for the differences between PictureBox and Image. (VB Helper example) (vb-helper.com)

Practical fix (minimal, reliable workflow): make the destination PictureBox use AutoRedraw, PaintPicture into it, then move the backing bitmap into the Picture property so it becomes a StdPicture you can assign to an Image control or save to disk. For example:

picSprite.AutoRedraw = True
' perform the PaintPicture drawing into picSprite here
Set picSprite.Picture = picSprite.Image
imgDirt.Picture = picSprite.Picture

Promoting Image into Picture makes the image persistent and copyable. See the how‑to example for this pattern. (vb-helper.com)

Alternatives and cautions: as noted, persisting to a file (SavePicture then LoadPicture) is an easy workaround when you need a file-backed StdPicture. For high-performance pixel work you can use Win32 BitBlt on device contexts, but that is more complex. Be careful: AutoRedraw allocates a persistent bitmap and can hit memory limits (Error 480) on constrained systems — use Image controls or the save/load trick if memory is a problem. (learn.microsoft.com)

This is the same root cause behind 's suggestion and what reported fixed the issue.

Recommended Answers

All 3 Replies

The problem is that the picture box and image controls MUST have a file reference as the starting point. You can't "hopscotch" passing blocks of pixels from one control to the other.

So, what happens if you pre-load your picture box picBoxSprite with a file is that it then has a filename reference for the image control to grab onto. If you just copy some pixels in from another picture box (picBoxResource), then the image control has no filename to grab onto. Therefore, it doesn't work.

You can get around this by either creating a temporary file with the image in your picture box using the SavePicture function, then using the LoadPicture function to splash it onto your image control.

Of course, if you really wanted to dig deep, you could just expose all the graphics methods in the Windows API and do it directly. Unless you have a specific reason to do this, I wouldn't recommend it.

Good luck!

I thought all Picture objects were bitmaps loaded into memory, and that you could copy and paste them however you wanted. I was wrong :)

Your solution worked like a charm, thanks a lot!

I thought all Picture objects were bitmaps loaded into memory, and that you could copy and paste them however you wanted. I was wrong :)

Actually, they are. You just can't manipulate them in the context of these controls. You have to get the handle to the device context in memory. Then you can move them around all you like using the Bit Block Transfer (BitBlt :icon_cheesygrin:) Windows API call.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.