Results 1 to 8 of 8

Thread: Picturebox w/ transparency

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2008
    Posts
    52

    Picturebox w/ transparency

    Hey everyone,
    I know this topic has been brought up in the past, but I'm having some trouble, and while I think I know why, I want to get your thoughts on it and maybe find a workaround besides redrawing graphics pixel by pixel for my tile/map editor.

    Here's the setup:
    I have a pic box (container) with a picture box (canvas). I want to do 2 layers of graphics inside of this pic box (canvas). Working on the lower layer isn't so much of an issue, but getting the graphics to be drawn over the "bottom layer" and having transparency is giving me some trouble.

    I tried using TransparentBlt, but I think what I need and its requirements don't mesh well. Right now I have the tiles loaded on an MDI form pic box, and I just paintpicture the tile into its own picbox (as a preview). When I draw it on the canvas pic box, I need the transparency to work. If I use TransparentBlt, I have to turn off the auto redraw, which overwrites the tile outline grid (line objects rather than drawn directly onto the pic box as it seems that's faster than using the .line method).

    So, okay, I'm seeing that I need auto redraw. So I make a temporary container, hidden, but then I can't get paintpicture the image. After reading up on it, it seems because that auto redraw is off, there's no buffer for the picture data, and if it's off screen or hidden, the data gets (more or less) destroyed. (my thought was to draw the lower layer into this temporary pic box, then transparentblt the higher layer, then copy the entire thing and paste it onto the canvas pic box)

    Since this doesn't seem to work as I thought, I can only come up with the idea that I have to redraw each pixel and omit the color index, but when working with meta tiles that are up to 64x64 pixels in size, that could become incredibly slow to redraw the pixels by hand.

    I hope I explained that clearly, if not let me know.

    What are my options to be able to do this?

    Also, it seems that I can only have a max height and width of a picturebox at 2048 pixels (doesn't seem to larger than this). Any ideas on why that is?

    Thanks!

  2. #2
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Picturebox w/ transparency

    Try this windowless control which supports both key-color and per-pixel alpha transparency (and both simultaneously if needed) - https://github.com/wqweto/AlphaBlendImage

    cheers,
    </wqw>

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2008
    Posts
    52

    Re: Picturebox w/ transparency

    This looks like a nice control! I'll have to play around with it.

    I think I did find a solution (closed the browser on accident so I don't have the link), but it was posted by/created by Vegard Fiksdal, and uses a method of creating a mask on the fly using some GDI API's.

    I'll post an update if this solution works or not.

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2008
    Posts
    52

    Re: Picturebox w/ transparency

    Okay, so this example is what I was looking for (found it through my history): https://www.codeguru.com/visual-basi...-transparency/

    It allows the AutoRedraw to the destination to be set to true, can be done in a hidden pic box, and can be copied out to another destination.

    Thanks for the link to the custom control. Supporting PNG's is a very nice feature.

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Picturebox w/ transparency

    Quote Originally Posted by lightsock View Post
    ...
    Also, it seems that I can only have a max height and width of a picturebox at 2048 pixels (doesn't seem to larger than this). Any ideas on why that is?
    ...
    Did you try setting the Picturebox's scalemode to pixels, and set the container of the picturebox scalemode to pixels?
    In some cases it seems like the size of the picturebox is limited to a signed 16-bit value, which would be 32,767 max positive value. And if the size is in twips, and your twips are 15 per pixel, then 32768 / 15 = 2188, so 2048 fits in that range.

    If you make sure your container is in pixels and the picturebox is in pixels, you might be able to be as large as 32,000 pixels in size. Of course the larger the size, the slower the updates.

    I'm not sure why you have to turn off AutoRedraw when you use transparent blt. It should be bliting to the memory buffer fine. The only issue is because you used the API to draw in the memory buffer, VB doesn't know about it and won't refresh the screen automatically, so once you're done drawing and you want to see the result, you have to issue a refresh on the picturebox so VB will show the current state of the AutoRedraw buffer on the screen.

    p.s. You can also do a transparent drawing by drawing the same image twice, once over a black background and once over a white background. You can then use the AND and OR rasterops to bitblt or PaintPicture the two images to a third image.

    An example that uses that technique, and also used transparent blt for three levels of overlay can be found in this post.
    The lower picturebox has a background image. Over that is drawn a grid using VB Line commands, and a transparent image is drawn using the above mentioned method.
    Those drawings are also continually rotating, demonstrating dynamic drawing and rotation, which was an example for another post.

    To have something to draw transparently, you drag your mouse on either picturebox above the viewing picturebox.

    The example was modified in this post to draw an "annotation layer" above the dynamically updating image, to demonstrate how a user could draw on the picturebox that is being updated by the code at the same time.

    To draw in the "annotation" layer, you drag with the mouse in the viewing window.
    Last edited by passel; Sep 14th, 2021 at 04:00 PM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Picturebox w/ transparency

    Quote Originally Posted by passel View Post
    In some cases it seems like the size of the picturebox is limited to a signed 16-bit value, which would be 32,767 max positive value. And if the size is in twips, and your twips are 15 per pixel, then 32768 / 15 = 2188, so 2048 fits in that range.
    I don't think so.

    I just did something similar and used images of 3648x2736, it all worked fine. Everything had a ScaleMode in twips. For AlphaBlend() calls I had to convert to pixels of course.

    The client dimensions of a PictureBox, Form, UserControl, etc. are all Single, so there isn't any "16-bit limit."

    Sounds more like an arithmetic flaw or some other bug.

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Picturebox w/ transparency

    I should retract that. There is a limit and for all I know it might be 65,535. I didn't go that high.

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Picturebox w/ transparency

    I don't know. They were singles in VB3 as well, but I've had the issue at some point. It could have been from back then, so might have been prehistoric memory.
    Perhaps I was trying to draw some lines that were greater than 32767 pixels long. I don't know. I guess I'll just stop guessing.
    I assume it wouldn't be a Service Pack issue.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width