First off, this line will create a memory leak:
bOk = objAIL.Convert_To32bit(hImg2, hImg2)
You must create a separate variable to hold the result from the
conversion to 32-bits, otherwise the original is lost and is never
cleaned up... creating a memory leak.
-------
Second, in the following code... it is pointless to put the "bOK" for
the first line, because the second line always overwrites it:
bOk = objAIL.Convert_To32bit(hImg2, hImg32)
bOk = objAIL.Effect_SetTransparentColor(hImg2, TransparentColor,
TransparencyLevel)
Use "Call" instead or check for the return
-------
Third, you need to clean up the image that was originally passed in as
"hIMG_Destination" which was later transfered to "hImg" before
assigning the new value of "hTempImg" to the original parameter. This
can be done by adding the following line of code:
Call objAIL.IO_DestroyImage(hImg)
...right above this line of code:
hIMG_Destination = hTempImg
Failing to do this will create a second memory leak in your code for
the same reason as the first (image being stored in memory, but never
cleaned up).
--------
I altered your code slightly and this is what I came up with (seems to
work fine when I tested it:
==================================================
Option Explicit
Private objAIL As cAdvancedImagery
Private Sub Form_Load()
Dim hImgS As Long
Dim hImgD As Long
Dim vTempD As Variant
Set objAIL = New cAdvancedImagery
Call objAIL.IO_LoadImage("C:\Transparent.bmp", hImgS)
Call objAIL.IO_LoadImage("C:\TEST.BMP", hImgD)
vTempD = hImgD: hImgD = 0
MsgBox Effect_OverlayPictureEx(vTempD, hImgS, 0, 0, RGB(0, 255, 0),
0, 255)
hImgD = vTempD: vTempD = 0
Me.Visible = True
Me.AutoRedraw = True
Me.Move 0, 0
DoEvents
objAIL.Render_Normal hImgD, Me.hDC
Me.Refresh
objAIL.IO_DestroyImage hImgD
objAIL.IO_DestroyImage hImgS
Set objAIL = Nothing
End Sub
Public Function Effect_OverlayPictureEx(ByRef hIMG_Destination As
Variant, _
ByVal hIMG_Source As Long, _
ByVal Left As Long, _
ByVal Top As Long, _
Optional ByVal
TransparentColor As Long = vbMagenta, _
Optional ByVal
TransparencyLevel As Byte = 0, _
Optional ByVal AlphaBlend As
Byte = 255) As Boolean
Dim hDevCon As Long
Dim hTempImg As Long
Dim hNewBmp As Long
Dim hOldBmp As Long
Dim hImgD As Long
Dim hImgS As Long
Dim hImg32 As Long
If IsNumeric(hIMG_Destination) = True And IsNumeric(hIMG_Source) =
True Then
If hIMG_Destination <> 0 And hIMG_Source <> 0 Then
hImgD = CLng(hIMG_Destination)
hImgS = CLng(hIMG_Source)
Call objAIL.Convert_To32bit(hImgS, hImg32): hImgS = hImg32
If objAIL.Effect_SetTransparentColor(hImgS, TransparentColor,
TransparencyLevel) = True Then
Call objAIL.IO_CreateNew(hTempImg,
objAIL.Info_Width(hImgD), objAIL.Info_Height(hImgD), 32)
Call objAIL.Convert_ImageToBitmap(hTempImg, hNewBmp)
Call objAIL.IO_DestroyImage(hTempImg)
Call objAIL.MemoryDC_Create(hDevCon)
Call objAIL.MemoryDC_InsertBitmap(hDevCon, hNewBmp, hOldBmp)
objAIL.Render_Normal hImgD, hDevCon
If AlphaBlend < 255 Then
objAIL.Render_Blend hImgS, hDevCon, Left, Top, AlphaBlend
Else
objAIL.Render_Alpha hImgS, hDevCon, Left, Top
End If
Call objAIL.MemoryDC_InsertBitmap(hDevCon, hOldBmp, hNewBmp)
Call objAIL.MemoryDC_Destroy(hDevCon): hOldBmp = 0
Call objAIL.Convert_BitmapToImage(hNewBmp, hTempImg)
Call objAIL.IO_DestroyBitmap(hNewBmp)
Call objAIL.IO_DestroyImage(hImgD)
Call objAIL.IO_DestroyImage(hImgS)
hIMG_Destination = hTempImg
Effect_OverlayPictureEx = True
Else
Effect_OverlayPictureEx = False
End If
End If
End If
End Function
==================================================
Lemme know if this does the trick for you too.
- Kevin
--- In AIL_Development@yahoogroups.com, <michael.reep@...> wrote:
>
> I attempted to write a function (using the code you provided via
method 2) so I could get it to work from ASP.
>
> It seems to work ok, but the transparency is still not being
preserved in the Overlaying (Render_Alpha)
>
> I have tried almost every combination of image formats I can think
of, and used ones that already had transparencies set in their
pallette as well as ones using the alph channel, and they are ignored.
>
> Take a look and see if you can see something that I'm doing wrong:
>
> (I have the need to make the "white" color transparent, along with
some blending, so that's what I added the blending "alphablend" at the
end)
>
> Thanks
> -Michael
>
> Public Function Effect_OverlayPictureEx(ByRef hIMG_Destination As
Variant, ByVal hIMG_Source As Long, ByVal Left As Long, ByVal Top As
Long, Optional ByVal TransparentColor As Long = vbMagenta, Optional
ByVal TransparencyLevel As Byte = 0, Optional ByVal AlphaBlend As Byte
= 255) As Boolean
>
> Dim hDevCon As Long
> Dim hTempImg As Long
> Dim hNewBmp As Long
> Dim hOldBmp As Long
> Dim hImg As Long
> Dim hImg2 As Long
>
> hImg = CLng(hIMG_Destination)
> hImg2 = CLng(hIMG_Source)
>
> Dim bOk As Boolean
>
>
> If NumberIsValid(hImg) = True Then
>
> bOk = objAIL.Convert_To32bit(hImg2, hImg2)
> bOk = objAIL.Effect_SetTransparentColor(hImg2,
TransparentColor, TransparencyLevel)
>
> If bOk = True Then
>
> Call objAIL.IO_CreateNew(hTempImg,
objAIL.Info_Width(hImg), objAIL.Info_Height(hImg), 32)
>
>
>
>
> Call objAIL.Convert_ImageToBitmap(hTempImg, hNewBmp)
> Call objAIL.IO_DestroyImage(hTempImg)
> Call objAIL.MemoryDC_Create(hDevCon)
> Call objAIL.MemoryDC_InsertBitmap(hDevCon, hNewBmp,
hOldBmp)
> objAIL.Render_Normal hImg, hDevCon
>
> If AlphaBlend < 255 Then
> objAIL.Render_Blend hImg2, hDevCon, Left, Top,
AlphaBlend
> Else
> objAIL.Render_Alpha hImg2, hDevCon, Left, Top
> End If
>
>
> Call objAIL.MemoryDC_InsertBitmap(hDevCon, hOldBmp,
hNewBmp)
> Call objAIL.MemoryDC_Destroy(hDevCon): hOldBmp = 0
> Call objAIL.Convert_BitmapToImage(hNewBmp, hTempImg)
> Call objAIL.IO_DestroyBitmap(hNewBmp)
>
>
> hIMG_Destination = hTempImg
> Effect_OverlayPictureEx = True
>
>
>
> Else
> Effect_OverlayPictureEx = False
> End If
>
>
> End If
>
>
>
>
> End Function
>
> ----- Original Message -----
> From: Kevin Wilson <kevinwilson1997@...>
> Date: Tuesday, April 17, 2007 11:32 am
> Subject: [AIL_Development] Re: Effect_OverlayPicture
> To: AIL_Development@yahoogroups.com
>
>
> > There's 2 ways to do it. The first is to render directly to a Form /
> > PictureBox / Printer / PropertyBox like this:
> >
> > ---------------------------------------------
> > Dim objAIL As cAdvancedImagery
> > Dim hImg As Long
> > Dim hImg2 As Long
> >
> > ' Initialize AIL
> > Set objAIL = New cAdvancedImagery
> > ' Load the background image
> > Call objAIL.IO_LoadImage("c:\test.bmp", hImg)
> > ' Load the foreground transparent image
> > Call objAIL.IO_LoadImage("c:\test111.bmp", hImg2)
> > ' Convert the color WHITE to transparent on the foreground image
> > Call objAIL.Effect_SetTransparentColor(hImg2, vbWhite, 0, True)
> >
> > ' Make the form visible
> > Me.Visible = True
> > Me.AutoRedraw = True
> > ' Render the background image normally
> > objAIL.Render_Normal hImg, Me.hDC
> > Me.Refresh
> > ' Render the foreground image transparently
> > objAIL.Render_Alpha hImg2, Me.hDC
> > Me.Refresh
> >
> > ' Clean up
> > Call objAIL.IO_DestroyImage(hImg2)
> > Call objAIL.IO_DestroyImage(hImg)
> > Set objAIL = Nothing
> >
> > MsgBox "Success"
> >
> > ---------------------------------------------
> > The other is to combine them in memory like this:
> > ---------------------------------------------
> >
> > Dim objAIL As cAdvancedImagery
> > Dim hDevCon As Long
> > Dim hTempImg As Long
> > Dim hNewBmp As Long
> > Dim hOldBmp As Long
> > Dim hImg As Long
> > Dim hImg2 As Long
> >
> > ' Initialize AIL
> > Set objAIL = New cAdvancedImagery
> > ' Load the background image
> > Call objAIL.IO_LoadImage("c:\test.bmp", hImg)
> > ' Load the foreground transparent image
> > Call objAIL.IO_LoadImage("c:\test111.bmp", hImg2)
> > ' Convert the color WHITE to transparent on the foreground image
> > Call objAIL.Effect_SetTransparentColor(hImg2, vbWhite, 0, True)
> > ' Create a temporary image to create a BITMAP from
> > Call objAIL.IO_CreateNew(hTempImg, objAIL.Info_Width(hImg),
> > objAIL.Info_Height(hImg), 32)
> > ' Convert temp image to BITMAP
> > Call objAIL.Convert_ImageToBitmap(hTempImg, hNewBmp)
> > ' Clean up temp image
> > Call objAIL.IO_DestroyImage(hTempImg)
> > ' Create a memory-based Device Context (DC)
> > Call objAIL.MemoryDC_Create(hDevCon)
> > ' Insert the bitmap into the memory DC (this sets the
> > width/height/color depth of the memory DC)
> > Call objAIL.MemoryDC_InsertBitmap(hDevCon, hNewBmp, hOldBmp)
> >
> > ' Render the background image normally
> > objAIL.Render_Normal hImg, hDevCon
> > ' Render the foreground image transparently
> > objAIL.Render_Alpha hImg2, hDevCon
> >
> > ' Pull the modified image out of the memory DC by putting back the
> > original BITMAP
> > Call objAIL.MemoryDC_InsertBitmap(hDevCon, hOldBmp, hNewBmp)
> > ' Clean up the DC (the old/original BITMAP is automatically destroyed
> > with the DC)
> > Call objAIL.MemoryDC_Destroy(hDevCon): hOldBmp = 0
> > ' Convert the BITMAP back to an image
> > Call objAIL.Convert_BitmapToImage(hNewBmp, hTempImg)
> > ' Clean up the BITMAP
> > Call objAIL.IO_DestroyBitmap(hNewBmp)
> >
> > ' Now that we have the image we want, display it for inspection
> > Me.Visible = True
> > Me.AutoRedraw = True
> > objAIL.Render_Normal hTempImg, Me.hDC
> > Me.Refresh
> >
> > ' Clean up
> > Call objAIL.IO_DestroyImage(hTempImg)
> > Call objAIL.IO_DestroyImage(hImg2)
> > Call objAIL.IO_DestroyImage(hImg)
> > Set objAIL = Nothing
> >
> > MsgBox "Success"
> >
> > ---------------------------------------------
> >
> > I can see that the process of overlaying images transparently is
a bit
> > too complex... so I'm going to be adding a new function to AIL
shortly
> > which will do all this for you (in a more efficient manner). I'll
> > call it "Effect_OverlayPictureEx".
> >
> > - Kevin
> >
> >
> >
> >
> >
> > --- In AIL_Development@yahoogroups.com, "Michael" <michael.reep@>
> > wrote:
> > >
> > > I've seen the sample picstures of what AIL can do, so I know this
> > > has been done, atleast some way.
> > >
> > > I'm trying to use Effect_OverlayPicture, and trying to put one
> > image
> > > on top of another with a transparent background (similar to the
> > > FreeImage Composite function).
> > >
> > > I can load an image, and use:
> > > Effect_SetTransparentColor (hImg, vbWhite)
> > > and stream that back to the browser as a PNG, and everything's
> > cool,
> > > but when I do this:
> > >
> > > IO_LoadImage strPath,hImg2,IF_PNG
> > > Effect_SetTransparentColor hImg2,vbWhite
> > > Effect_OverlayPicture hImg, hImg2, 0, 0
> > >
> > > the background on the second image (hImg2) is white, no matter
> > what.
> > > I have even loaded transparent GIF's and transparent PNG's into
> > > hImg2 and the backgrounds are still white.
> > >
> > > Any help is appreciated.
> > > -Michael
> > >
> >
> >
> >
>