Link to home
Start Free TrialLog in
Avatar of Darrick
Darrick

asked on

Transparent Image?

Is it possible to draw an image with a transparent background in VB?  I am using VB 6.
Avatar of PaulHews
PaulHews
Flag of Canada image

The best way is to use Randy Birch's TransparentBlt function.  (There is an API that does this as well, but it is *only* available on W98, and W2000, and it has exactly the same capabilities.)

You will need to go to http://www.mvps.org/vbnet/code/bitmap/transparent.htm and copy/paste Randy Birch's TransparentBlt function and the declares into the module pane.  Then you can call the blit function to blit from a picture box to any other device context transparently.  Just pass the transparent color to the function to make those areas transparent.
Avatar of PeterDobson
PeterDobson

Avatar of Darrick

ASKER

Will this make the bitmap truly transparent(like a transparent GIF)?
It will appear transparent! Is this what you want or do you want to then save the image with its transparent bits?
Yes.  The function I provided is almost exactly the same as the one Peter provided.  You specify a background color and it draws the image with that color transparent.  You can draw over a background image or whatever you like.
Avatar of Darrick

ASKER

PeterDobson

I want to save the image as a transparent image, so that I can then change the background color at will and it will show through the transparent parts of the image.
If you want to save the non transparent .bmp as a new transparent .jpg, then this is tricky (by no means impossible but I'm not sure how). If simply changing the background colour at runtime is your quest then try this, it seems to work (although quite slow)

'make sure that the picture in picture1 has some white in it.

'module code

Option Explicit

Public Type RECT
  Left As Long
  Top As Long
  Right As Long
  Bottom As Long
End Type

Public Declare Function BitBlt Lib "gdi32" _
  (ByVal hDCDest As Long, ByVal XDest As Long, _
   ByVal YDest As Long, ByVal nWidth As Long, _
   ByVal nHeight As Long, ByVal hDCSrc As Long, _
   ByVal XSrc As Long, ByVal YSrc As Long, _
   ByVal dwRop As Long) As Long

Public Declare Function CreateBitmap Lib "gdi32" _
  (ByVal nWidth As Long, _
   ByVal nHeight As Long, _
   ByVal nPlanes As Long, _
   ByVal nBitCount As Long, _
   lpBits As Any) As Long

Public Declare Function SetBkColor Lib "gdi32" _
   (ByVal hdc As Long, ByVal crColor As Long) As Long

Public Declare Function SelectObject Lib "gdi32" _
   (ByVal hdc As Long, ByVal hObject As Long) As Long

Public Declare Function CreateCompatibleBitmap Lib "gdi32" _
   (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long

Public Declare Function CreateCompatibleDC Lib "gdi32" _
   (ByVal hdc As Long) As Long

Public Declare Function DeleteDC Lib "gdi32" _
   (ByVal hdc As Long) As Long

Public Declare Function DeleteObject Lib "gdi32" _
   (ByVal hObject As Long) As Long


'Form code

Option Explicit

Private Sub Command1_Click()
Dim R As RECT
With R
..Left = 0
..Top = 0
..Right = Picture1.ScaleWidth
..Bottom = Picture1.ScaleHeight
End With
TransparentBlt Picture1.hdc, Form1.hdc, Picture1.hdc, R, 0, 0, vbWhite

End Sub
Private Sub Command2_Click()
Dim R As RECT
With R
    Me.BackColor = vbBlack
    Picture1.BackColor = vbBlack
    TransparentBlt Picture1.hdc, Form1.hdc, Picture1.hdc, R, 0, 0, vbWhite

..Left = 0
..Top = 0
..Right = Picture1.ScaleWidth
..Bottom = Picture1.ScaleHeight
End With
TransparentBlt Picture1.hdc, Form1.hdc, Picture1.hdc, R, 0, 0, vbWhite
End Sub

Private Sub TransparentBlt(OutDstDC As Long, _
                           DstDC As Long, _
                           SrcDC As Long, _
                           SrcRect As RECT, _
                           DstX As Integer, _
                           DstY As Integer, TransColor As Long)

Dim nRet As Long, W As Integer, H As Integer
Dim MonoMaskDC As Long, hMonoMask As Long
Dim MonoInvDC As Long, hMonoInv As Long
Dim ResultDstDC As Long, hResultDst As Long
Dim ResultSrcDC As Long, hResultSrc As Long
Dim hPrevMask As Long, hPrevInv As Long
Dim hPrevSrc As Long, hPrevDst As Long
W = SrcRect.Right - SrcRect.Left + 1
H = SrcRect.Bottom - SrcRect.Top + 1
MonoMaskDC = CreateCompatibleDC(DstDC)
MonoInvDC = CreateCompatibleDC(DstDC)
hMonoMask = CreateBitmap(W, H, 1, 1, ByVal 0&)
hMonoInv = CreateBitmap(W, H, 1, 1, ByVal 0&)
hPrevMask = SelectObject(MonoMaskDC, hMonoMask)
hPrevInv = SelectObject(MonoInvDC, hMonoInv)
ResultDstDC = CreateCompatibleDC(DstDC)
ResultSrcDC = CreateCompatibleDC(DstDC)
hResultDst = CreateCompatibleBitmap(DstDC, W, H)
hResultSrc = CreateCompatibleBitmap(DstDC, W, H)
hPrevDst = SelectObject(ResultDstDC, hResultDst)
hPrevSrc = SelectObject(ResultSrcDC, hResultSrc)
Dim OldBC As Long
OldBC = SetBkColor(SrcDC, TransColor)
nRet = BitBlt(MonoMaskDC, 0, 0, W, H, SrcDC, _
SrcRect.Left, SrcRect.Top, vbSrcCopy)
TransColor = SetBkColor(SrcDC, OldBC)
nRet = BitBlt(MonoInvDC, 0, 0, W, H, _
MonoMaskDC, 0, 0, vbNotSrcCopy)
nRet = BitBlt(ResultDstDC, 0, 0, W, H, _
DstDC, DstX, DstY, vbSrcCopy)
nRet = BitBlt(ResultDstDC, 0, 0, W, H, _
MonoMaskDC, 0, 0, vbSrcAnd)
nRet = BitBlt(ResultSrcDC, 0, 0, W, H, SrcDC, _
SrcRect.Left, SrcRect.Top, vbSrcCopy)
nRet = BitBlt(ResultSrcDC, 0, 0, W, H, _
MonoInvDC, 0, 0, vbSrcAnd)
nRet = BitBlt(ResultDstDC, 0, 0, W, H, _
ResultSrcDC, 0, 0, vbSrcInvert)
nRet = BitBlt(OutDstDC, DstX, DstY, W, H, _
ResultDstDC, 0, 0, vbSrcCopy)
hMonoMask = SelectObject(MonoMaskDC, hPrevMask)
DeleteObject hMonoMask
hMonoInv = SelectObject(MonoInvDC, hPrevInv)
DeleteObject hMonoInv
hResultDst = SelectObject(ResultDstDC, hPrevDst)
DeleteObject hResultDst
hResultSrc = SelectObject(ResultSrcDC, hPrevSrc)
DeleteObject hResultSrc
DeleteDC MonoMaskDC
DeleteDC MonoInvDC
DeleteDC ResultDstDC
DeleteDC ResultSrcDC
End Sub


Private Sub Form_Load()
Picture1.BorderStyle = 0
End Sub


sorry,

Private Sub Command2_Click()
Dim R As RECT
With R
    Me.BackColor = vbBlack
    Picture1.BackColor = vbBlack
..Left = 0
..Top = 0
..Right = Picture1.ScaleWidth
..Bottom = Picture1.ScaleHeight
End With
TransparentBlt Picture1.hdc, Form1.hdc, Picture1.hdc, R, 0, 0, vbWhite
End Sub
ASKER CERTIFIED SOLUTION
Avatar of PaulHews
PaulHews
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can just use a mask to do this with any graphics format.  I paint bmp and gifs using BitBlt.  You have to have two pictures, one that is the original, and one that is the mask.  The first picture's background should be white, and the second picture's background should be black.  Have you tried this before?
Check out this site...maybe it will help:
http://www.vbhelp.net/pages/help/help15.html
Avatar of Darrick

ASKER

No, I am not familar with masks
Very useful technique.  I have to use it quite a bit but it does require that you create two pictures.  For instance, I paint bmp pictures of rivers on a map.  The first picture has the rivers in blue with a black background, and the second picture has the rivers in black with a white background.  This paints the picture onto the picturebox or form without showing the background (only the rivers are painted.
Plop this code into a new module:
Declare Function BitBlt Lib "gdi32" (ByVal hDestDC 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 dwRop As Long) As Long
Public Const SRCAND = &H8800C6
Public Const SRCCOPY = &HCC0020
Here is my code I have in my program that paints the rivers on my picturebox:

BitBlt picBack.hDC, X * 32, Y * 32, 32, 32, picRiverSprite.hDC, V * 32, Q, SRCAND
                    BitBlt picBack.hDC, X * 32, Y * 32, 32, 32, picRiverMask.hDC, V * 32, Q, SRCPAINT
????
What are lot mumbling on about.

VB supports transparent gifs. So why mess with all that BitBlt stuff when you just need to use a transparent gif!!

_________
holgrave.
I think we all know that gifs support transparency.  The question and comments from the user appears to be referring only to bmp.  Mumble mumble.
However, holgrave's comment does bring up an interesting point: Do the pictures HAVE to be BMP format?  There are a slew of free programs out there that will convert BMPs to GIF.  Only problem I have seen using GIFs is that some 3D video cards don't act well with GIFs (they end up invisible).
Avatar of Darrick

ASKER

holgrave

Can I create and save a transparent gif image from VB?  If so, how?
I am not aware of a way of CREATING a gif image in VB.  Tell us what it is exactly that you are trying to do.
Avatar of Darrick

ASKER

holgrave

Can I create and save a transparent gif image from VB?  If so, how?
Avatar of Darrick

ASKER

Vingame

How do I transfer a bitmap without transparency into a gif with transparency?
Okay, first of all, do you have an graphics programs?   Paintshop pro?  Corel Draw?
Avatar of Darrick

ASKER

I am creating hundreds of simple images in VB.  Currently I am saving them as bitmaps and then converting them to gifs.  Part of the images now  needs to be transparent and I am not sure how to proceed.
What I did was I went to Softseek.com and downloaded a free program that will convert BMP to GIF format.  Then I used PaintShop Pro to set a certain color transparent on the GIF picture.
Avatar of Darrick

ASKER

I need to be able to make the images transparent from VB.  It would take too long doing them individually with PaintShop Pro.
Most Graphics programs have a feature to create transparencies with GIF.  I believe.  Microsoft Photo Editor has it even.
Oh, hmmm, now I see.  Then perhaps we must go back to the BitBlt idea, and use masks.
>Can I create and save a transparent gif image from VB?  If so, how?

Third party tools are the only way you will find to create and save transparent gifs in VB.  .gif format is protected by copyright, so you will not find free libraries/source code for saving in this format.  Paint Shop Pro is a shareware program that you can use to create them.

Note BTW I am basically repeating what I said several hours ago.  Aren't you reading my comments Darrick?
>I am creating hundreds of simple images in VB.  Currently I am saving them as bitmaps and then converting them to gifs.  Part of the images now  needs to be transparent and I am not sure how to proceed.

What are you using them for webpage?  Or VB application?
Yes, knowing why you are doing this, and for what end goal would help us figure out some solutions.
Avatar of Darrick

ASKER

PaulHews

Yes, I have been reading your comments.

The images are being used in a webpage.
Why use VB to create these images instead of a graphics program?
What I did was I went to Softseek.com and downloaded a free program that will convert BMP to GIF format.  Then I used PaintShop Pro to set a certain color transparent on the GIF picture.
Sorry if I insulted anyone when I said you were mumbling ;-)

It was me that was mumbling afterall.

Umm and I missed Paul Hews's comment further up. He was right when about the example at www.mvps.org/vbnet/code/bitmap/transparent.htm it is the best example I have seen on the net.

Also there is an example at www.allapi.com.

Sorry, long day.
___________
holgrave.
Holgrave:
No worries...your comment just gave me the opportunity to be funny.
Avatar of Darrick

ASKER

Thanks