Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pictures: make them transparent, resize

Status
Not open for further replies.

Fursten

Programmer
Dec 27, 2000
403
PT
HI,

If we use the coolbar control and put in it a picture as the background, the picture will be resized to fill all the control. However If we put a picture in a pictureBox that doesn´t happen...Anyone knows why?

I would like the picture to fill all the pictureBox while it is being resized...

Maybe I have to change the picture with a program... any ideia?

By the way, does anyone ever used any program to make pictures transparent?

Thank you
 
The Image control has a stretch property that will automatically stretch the picture to fit the control, but the PictureBox doesn't have this. To stretch the image, use the StretchBlt API:

Public Declare Function StretchBlt Lib "gdi32" Alias "StretchBlt" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long

Notes on the arguments:
hdc and hSrcDC are the Device Context handles of the source and destination pictures (Picture1.hDC, Form1.hDC, etc.)
dwRop is the raster operation, a direct copy is vbSrcCopy.


As far as transparent, do you want the entire image to fade into the background or to have a transparent backcolor? For a transparent backcolor (only the image is drawn, not the whole square), you'll need to do a couple things, and it gets a little complicated:

First create a "mask" bitmap for each "sprite" you want to make. The mask is usually a white background with a black silhouette of the image, kind of like a negative of the real image.

Second, you'll have to use different raster operations in the StretchBlt (or BitBlt, or the Picture1.PaintPicture method) to create the sprite.

The easiest way I've found to do this is to have your source bitmaps (mask and origional) stored into pictureboxes on their own form that never gets displayed to the user (this increases the size of the exe, but it's fast). You should also have a buffer picture box where you'll build the image before vbScrCopy'ing the result to the destination picturebox. Say you have picMask, picImage, picBuffer, and picDest. picMask and picImage contain the mask and origional image, respectively, on the hidden form. If the mask image is white with a black silhouette, you can use these raster operations to create a sprite on the buffer:

vbMergePaint from picMask.hDC to picBuffer.hDC
vbSrcAnd from picImage.hDC to picBuffer.hDC (at the same x,y coordinates, same Height/Width, etc.)
At this point you should have your sprite build on the buffer, you can just source copy from the buffer to the destination:
vbSrcCopy from picBuffer.hDC to picDest.hDC

This is 3 separate calls to StretchBlt to make your sprite and copy the result to the UI.

I've left out the x, y, Width, and Height arguments, those should be the easy ones.

A couple other raster operations you may play with are vbPatInvert and vbSrcPaint. Others can be found on various websites, but what you have here should do the trick.

-Hope that helps!

-Mike

Difference between a madman and a genius:
A madman uses his genius destructively,
A genius uses his madness constructively.
 
Oops, I forgot a minor detail:

Before you build your sprite on the buffer, you should take a picture of the destination area:

vbSrcCopy from picDest to picBuffer 'copy destination area to the buffer

vbMergePaint from picMask to picBuffer 'sprite, step 1

vbSrcAnd from picImage to picBuffer 'sprite, step 2

vbSrcCopy from picBuffer to picDest 'copy end result to destination.

For animated sprites, you'll need 2 clean pictures before you start, but that's another thread :)

-Mike Difference between a madman and a genius:
A madman uses his genius destructively,
A genius uses his madness constructively.
 
HOLD ON THERE!
Your details of tranparency are a tad inaccurate. Make a gif, set is transparency and then load it in an image. Your done,
Brad,
Free mp3 player,games and more.
 
Brad,

Mike's details are not inaccurate at all. They are a generic solution for providing transparency in an image.

Your own solution, however, has some issues. Firstly, gifs are limited to 256 colours; secondly, what if I have already got a bunch of non-gif images that I want to make transparent? Am I expected to convert every single one to a gif? Thirdly, and this is probably a minor point, Microsoft do not have a redistributable licence for reading and writing gifs. In other words, you can use the various controls to handle gifs in your own applications, but you shouldn't do so in applications that you are writing for redistribution without first checking and
Of course, the downside to Mike's solution is that it can be a bit daunting, especially to people unfamiliar with masking techniques, bitblitting, device contexts, the Win32 API, etc. Wouldn't it be great if you could somehow do all this purely using VB and it's built-in controls? Well you can. And here's an example of how to do it. The only requirement is that you add a reference to the MS Common Controls. Drop an image control and a comand button onto the form.

[tt]
Option Explicit

' strPicture contains name of picture to load. Can be of any image format supported by normal LoadPicture
' TransparentColor is RGB value of colour in the image that you want to be transparent
' WorkForm is simply a form that will temporarily host a (non-visible) ListImage control
Private Function TransparentLoadPicture(strPicture As String, TransparentColor As Long, WorkForm As Form) As StdPicture
Dim ImageBuffer As ImageList

' Dynamically add a ListImage control to our working form
Set ImageBuffer = Controls.Add("MSComctlLib.ImageListCtrl", "myIList", WorkForm)

' Set up the colour that is to be transparant
ImageBuffer.UseMaskColor = True
ImageBuffer.MaskColor = TransparentColor

' Set up and extract the transparant image
ImageBuffer.ListImages.Add , , LoadPicture(strPicture)
Set TransparentLoadPicture = ImageBuffer.ListImages(1).ExtractIcon

' Clean up
Set ImageBuffer = Nothing

End Function

Private Sub Command1_Click()
Image1 = TransparentLoadPicture("your_graphics_file", RGB(0, 0, 0), Form1)
End Sub
[/tt]
 
I wasn't critizing any1, I didn't mean anything I just know that after some 3 months of vb I just now mastered bitbit and what not, so I though I would share my solution.

And unless this project is huge companies would never mind some one using there file format if your not making money directly from the file (ie an imaging program)

Again wasn't out to hurt any1.

MOO? Brad,
Free mp3 player,games and more.
 
Brad,

Wasn't trying to suggest you were trying to hurt anyone, just clarifying some of the potential drawbacks in using gifs.

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top