Results 1 to 1 of 1

Thread: Swapping Image Colors Quickly

  1. #1
    Fanatic Member technorobbo's Avatar
    Join Date
    Dec 08
    Location
    Chicago
    Posts
    864

    Swapping Image Colors Quickly

    If your graphic image is 256 color GIF you can instantly swap out values in the color table.
    Here's an example from one of my programs. I already knew that the colors I wanted to change in the table, but you can search the rgbtab array for the specific color and change a random color.

    Code:
    Option Explicit
    Private Type RGBQUAD
            rgbBlue As Byte
            rgbGreen As Byte
            rgbRed As Byte
            rgbReserved As Byte
    End Type
    Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
    Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
    Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
    Private Declare Function GetDIBColorTable Lib "gdi32" (ByVal hdc As Long, ByVal un1 As Long, ByVal un2 As Long, pRGBQuad As RGBQUAD) As Long
    Private Declare Function SetDIBColorTable Lib "gdi32" (ByVal hdc As Long, ByVal un1 As Long, ByVal un2 As Long, pcRGBQuad As RGBQUAD) As Long
    Dim rgbtab(0 To 255) As RGBQUAD
    
    Private Sub Form_Load()
    Set Image1.Picture = LoadPicture(App.Path & "\JessEyes.gif")
    End Sub
    
    Private Sub Option1_Click(Index As Integer)
    Dim h As Long, SHdc As Long
    Dim i As Integer, ret As Long
    SHdc = CreateCompatibleDC(0)
    SelectObject SHdc, Image1.Picture.Handle
    ret = GetDIBColorTable(SHdc, 0, 256, rgbtab(0))
    If ret = 0 Then Exit Sub
    h = Choose(Index, RGB(0, 0, 32), RGB(0, 32, 0), RGB(32, 32, 0)) '94
    CopyMemory rgbtab(255), h, 4
    h = Choose(Index, RGB(0, 0, 62), RGB(0, 62, 0), RGB(62, 62, 0)) '62
    CopyMemory rgbtab(253), h, 4
    h = Choose(Index, RGB(0, 0, 83), RGB(0, 83, 0), RGB(83, 83, 0)) '83
    CopyMemory rgbtab(249), h, 4
    h = Choose(Index, RGB(0, 0, 94), RGB(0, 94, 0), RGB(94, 94, 0)) '94
    CopyMemory rgbtab(247), h, 4
    h = Choose(Index, RGB(0, 0, 118), RGB(0, 118, 0), RGB(118, 118, 0)) '118
    CopyMemory rgbtab(241), h, 4
    SetDIBColorTable SHdc, 0, 256, rgbtab(0)
    DeleteDC SHdc
    Me.Refresh
    End Sub
    Attached Files Attached Files
    Last edited by technorobbo; Jul 10th, 2009 at 05:25 AM.
    Have Fun,

    TR
    _____________________________
    Check out my Alpha DogFighter2D Game Demo and Source code. Direct Download:http://home.comcast.net/~technorobbo/Alpha.zip or Read about it in the forum:http://www.vbforums.com/showthread.php?t=551700. Now in 3D!!! http://home.comcast.net/~technorobbo/AlPha3D.zip or read about it in the forum: http://www.vbforums.com/showthread.php?goto=newpost&t=552560 and IChessChat3D internet chess game

Posting Permissions

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