Here you go:
I started with the TransparentGifCreator VB code from BobPowell.net
http://www.bobpowell.net/giftransparency.htm
I added the following to Bob's code:
'Native API Declarations
Public Declare Auto Function CreatePalette Lib "gdi32.dll" (ByVal
lpLogPalette As IntPtr) As IntPtr
Public Declare Auto Function SetClipboardData Lib "user32.dll" (ByVal
wFormat As Integer, ByVal hPal As IntPtr) As IntPtr
Public Declare Auto Function GetClipboardData Lib "user32.dll" (ByVal
wFormat As Integer) As IntPtr
Public Declare Auto Function EmptyClipboard Lib "user32.dll" () As Boolean
Public Declare Auto Function OpenClipboard Lib "user32.dll" (ByVal hWnd As
IntPtr) As Boolean
Public Declare Auto Function IsClipboardFormatAvailable Lib "user32.dll"
(ByVal wFormat As Integer) As Boolean
Public Declare Auto Function CloseClipboard Lib "user32.dll" () As Boolean
Public Declare Auto Function GetObject Lib "gdi32.dll" (ByVal hObject As
IntPtr, ByVal nCount As Integer, ByRef nEntries As Integer) As Integer
Public Declare Auto Function GetPaletteEntries Lib "gdi32.dll" ( _
ByVal hPal As IntPtr, _
ByVal iStartIndex As Integer, _
ByVal nEntries As Integer, _
ByVal lppe As IntPtr) As Integer
' Structure Declarations
<StructLayout(LayoutKind.Sequential)Public Structure LOGPALETTE
Public palVersion As UShort
Public palNumEntries As UShort
Public palPalEntry() As PALETTEENTRY
End Structure
<StructLayout(LayoutKind.Sequential)Public Structure PALETTEENTRY
Public peRed As Byte
Public peGreen As Byte
Public peBlue As Byte
Public peFlags As Byte
End Structure
' Enum
Public Enum CLIPFORMAT
CF_TEXT = 1
CF_BITMAP = 2
CF_METAFILEPICT = 3
CF_SYLK = 4
CF_DIF = 5
CF_TIFF = 6
CF_OEMTEXT = 7
CF_DIB = 8
CF_PALETTE = 9
CF_PENDATA = 10
CF_RIFF = 11
CF_WAVE = 12
CF_UNICODETEXT = 13
CF_ENHMETAFILE = 14
CF_HDROP = 15
CF_LOCALE = 16
CF_MAX = 17
CF_OWNERDISPLAY = &H80
CF_DSPTEXT = &H81
CF_DSPBITMAP = &H82
CF_DSPMETAFILEPICT = &H83
CF_DSPENHMETAFILE = &H8E
End Enum
1) Add two additional buttons to the form.
One labeled Copy the other labeled Paste
2) Enter the code below to handle the button events
Friend WithEvents Button4 As System.Windows.Forms.Button 'timer1_Tick
' Copy palette to clipboard
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button4.Click
Dim lp As New LOGPALETTE
Dim pe(255) As PALETTEENTRY
lp.palNumEntries = cp.Entries.Length
lp.palVersion = &H300
For i As Integer = 0 To lp.palNumEntries - 1
pe(i).peBlue = cp.Entries(i).B
pe(i).peGreen = cp.Entries(i).G
pe(i).peRed = cp.Entries(i).R
pe(i).peFlags = 0
Next i
lp.palPalEntry = pe
Dim cb As Integer = Marshal.SizeOf(GetType(UShort)) * 2 +
Marshal.SizeOf(GetType(PALETTEENTRY)) * lp.palNumEntries
Dim pLp As IntPtr = Marshal.AllocHGlobal(cb)
Dim ptr As IntPtr = pLp
If 0 <pLp Then
Dim s(1) As Short
s(0) = lp.palVersion
s(1) = lp.palNumEntries
Marshal.Copy(s, 0, ptr, s.Length)
ptr = CType((ptr.ToInt32() + Marshal.SizeOf(GetType(Short))
* 2), IntPtr)
For i As Integer = 0 To lp.palNumEntries - 1
Marshal.StructureToPtr(lp.palPalEntry(i), ptr, True)
ptr = CType(ptr.ToInt32() +
Marshal.SizeOf(GetType(PALETTEENTRY)), IntPtr)
Next
End If
Dim hpal As New IntPtr
hpal = CreatePalette(pLp)
If 0 <hpal Then
If True = OpenClipboard(Me.Handle) Then
EmptyClipboard()
SetClipboardData(CLIPFORMAT.CF_PALETTE, hpal)
CloseClipboard()
End If
End If
Marshal.FreeHGlobal(pLp)
End Sub
Friend WithEvents Button5 As System.Windows.Forms.Button
' Paste palette from clipboard
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button5.Click
Dim hPal As IntPtr
Dim nEntries As Integer
If True = OpenClipboard(Me.Handle) Then
If True = IsClipboardFormatAvailable(CLIPFORMAT.CF_PALETTE)
Then
hPal = GetClipboardData(CLIPFORMAT.CF_PALETTE)
End If
CloseClipboard()
End If
Dim lp As IntPtr
Dim nRetrieved As Integer
Dim pe(256) As PALETTEENTRY
If 0 <hPal Then
GetObject(hPal, Marshal.SizeOf(GetType(UShort)), nEntries)
If nEntries 0 Then
lp =
Marshal.AllocHGlobal(Marshal.SizeOf(GetType(PALETT EENTRY)) * nEntries)
End If
nRetrieved = GetPaletteEntries(hPal, 0, nEntries, lp)
If nRetrieved 0 And 0 <lp Then
Dim ptr As IntPtr = lp
For i As Integer = 0 To nEntries - 1
pe(i) = Marshal.PtrToStructure(ptr,
GetType(PALETTEENTRY))
ptr = CType(ptr.ToInt32() +
Marshal.SizeOf(GetType(PALETTEENTRY)), IntPtr)
' assign palette entry to managed palette
Dim clr As Color = Color.FromArgb(255, pe(i).peRed,
pe(i).peGreen, pe(i).peBlue)
cp.Entries(i) = clr
Next
End If
End If
Marshal.FreeHGlobal(lp)
Me.Invalidate()
End Sub