Solved

Kill One Color In Gif

Posted on 1998-12-03
2
161 Views
Last Modified: 2010-05-03
I got gif file loaded into a picture box.Then I click on the picture and get the color of the pixel I clicked on.I need to make transparent all parts of the picture having the same color.
How can I do it?  
0
Comment
Question by:mdanny
2 Comments
 
LVL 13

Accepted Solution

by:
Mirkwood earned 100 total points
ID: 1448107
Add this code to a module:

Public Type RECT
       Left As Long
       Top As Long
       Right As Long
       Bottom As Long
End Type  <Picture>

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
 

Start a new project, and to the form add a command button and a picture box to make transparent as shown in the illustration.
Set the picture box picture property to a bitmap that has various colours on white background, and it's ScaleMode property to 3 - Pixel.
Side Effects:None
returns: null

Add this following code to the form:

Private Sub Command1_Click()

       Dim R As RECT
       With R
       .Left = 0
       .Top = 0
       .Right = Picture1.ScaleWidth
       .Bottom = Picture1.ScaleHeight
End With

TransparentBlt Form1.hdc, Form1.hdc, Picture1.hdc, R, 20, 20, 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)
       '     DstDC- Device context into which image must be drawn transparently
       '     OutDstDC- Device context into image is actually drawn, even though
       '     it is made transparent in terms of DstDC
       '     Src- Device context of source to be made transparent in color TransColor
       '     SrcRect- Rectangular region within SrcDC to be made transparent in terms of
       '     DstDC, and drawn to OutDstDC
       'DstX, DstY - Coordinates in OutDstDC (and DstDC) where the transparent bitmap m
       '     ust go
       '     In most cases, OutDstDC and DstDC will be the same
       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
       '     create monochrome mask and inverse masks
       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)
       '     create keeper DCs and bitmaps
       ResultDstDC = CreateCompatibleDC(DstDC)
       ResultSrcDC = CreateCompatibleDC(DstDC)
       hResultDst = CreateCompatibleBitmap(DstDC, W, H)
       hResultSrc = CreateCompatibleBitmap(DstDC, W, H)
       hPrevDst = SelectObject(ResultDstDC, hResultDst)
       hPrevSrc = SelectObject(ResultSrcDC, hResultSrc)
       '     copy src to monochrome mask
       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)
       '     create inverse of mask
       nRet = BitBlt(MonoInvDC, 0, 0, W, H, MonoMaskDC, 0, 0, vbNotSrcCopy)
       '     get background
       nRet = BitBlt(ResultDstDC, 0, 0, W, H, DstDC, DstX, DstY, vbSrcCopy)
       '     AND with Monochrome mask
       nRet = BitBlt(ResultDstDC, 0, 0, W, H, MonoMaskDC, 0, 0, vbSrcAnd)
       '     get overlapper
       nRet = BitBlt(ResultSrcDC, 0, 0, W, H, SrcDC, SrcRect.Left, SrcRect.Top, vbSrcCopy)
       '     AND with inverse monochrome mask
       nRet = BitBlt(ResultSrcDC, 0, 0, W, H, MonoInvDC, 0, 0, vbSrcAnd)
       '     XOR these two
       nRet = BitBlt(ResultDstDC, 0, 0, W, H, ResultSrcDC, 0, 0, vbSrcInvert)
       '     output results
       nRet = BitBlt(OutDstDC, DstX, DstY, W, H, ResultDstDC, 0, 0, vbSrcCopy)
       '     clean up
       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

Run the project, and press the command button. The bitmap will be copied onto the form, and the white background will be transparent.
 
0
 
LVL 1

Author Comment

by:mdanny
ID: 1448108
Works great !! Thanks Mirkwood
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now