Search the web
Sign In
New User? Sign Up
AIL_Development · AIL Developer's Forum
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Hear how Yahoo! Groups has changed the lives of others. Take me there.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Display 32-bit transparent PNG in Image control (VB)   Message List  
Reply | Forward Message #105 of 125 |
Re: Display 32-bit transparent PNG in Image control (VB)

--- "bluescreenweasel" <peteraddington@...> wrote:
>
> further to this one of your previous replies has a more elegant
method
> than the one I just posted -
>
> If you want to use the transparent PNG approach but don't want to
> save the images to the hard drive... you can always save them to
> memory-based byte arrays using the "IO_SaveToBinary" function. Then
> when it's time to render that "frame"... use the "IO_LoadFromBinary"
> function and then call "Convert_ImageToPicture" to convert the hIMG
> image handle to a System.Drawing.Image object to set the PictureBox
> control.
>
> im struggling with the filestream bit - this is what i have - im
> hoping it will return a transparent PNG for the picturebox like my
> method before of saving out a file.
>
> --SNIP--
>
> help me obi-wan!



Keep in mind that saving to a Stream object in .NET is intended for
things like pushing a custom image over the internet or network via
an open stream, or saving to a file via a custom stream-based
system. For what you're trying to do, use a byte array (easier and
faster to work with).

Also keep in mind that a System.IO.FileStream is meant to load from,
or save to a local hard drive or network drive... which is what
you're trying to avoid.

In theory, the following code will do what you're wanting to do (load
the TGA image from file, convert it in memory to a PNG, then convert
that PNG image to a System.Drawing.Image object and display it on the
user interface:

=========================

Option Explicit On
Option Strict Off
Option Compare Binary

Imports System
Imports System.IO
Imports System.Drawing
Imports Microsoft
Imports Microsoft.VisualBasic
Imports AdvImgLib_NET
Imports AdvImgLib_NET.cAdvancedImagery

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim objAIL As cAdvancedImagery = Nothing
Dim hIMG_Input As Int32 = 0
Dim hIMG_Output As Int32 = 0
Dim objPic As Image = Nothing
Dim arrIMG() As Byte = {}

objAIL = New cAdvancedImagery

If objAIL.IO_LoadImage("C:\TEST1.TGA", hIMG_Input) = True Then
If objAIL.IO_SaveToBinary(hIMG_Input, arrIMG,
IMAGE_FORMAT.IF_PNG, IMAGE_FLAGS.PNG_DEFAULT) = True Then
If objAIL.IO_LoadFromBinary(arrIMG, hIMG_Output) = True
Then
If objAIL.Convert_ImageToPicture(hIMG_Output, objPic)
= True Then
Me.BackgroundImageLayout = ImageLayout.None
Me.BackgroundImage = objPic
Me.Refresh()
Else
MsgBox("Error converting to System.Drawing.Image: "
& objAIL.Error_GetLast)
End If
objAIL.IO_DestroyImage(hIMG_Output)
Else
MsgBox("Error loading image from array: " &
objAIL.Error_GetLast)
End If
Erase arrIMG
Else
MsgBox("Error saving to array: " & objAIL.Error_GetLast)
End If
objAIL.IO_DestroyImage(hIMG_Input)
Else
MsgBox("Error loading image from file: " &
objAIL.Error_GetLast)
End If

objAIL = Nothing

End Sub

End Class

=========================

The only problem is that when the image is displayed, it is not
transparent. In fact, when I create a TGA image in PhotoShop which
is transparent, I can't get *ANY* program to load it correctly and
display it's transparency information. PhotoShop won't even load the
image and recognize the transparency correctly. Neither will
QuickTime image viewer, nor PictureIt (which was built with AIL /
FreeImage).

The only way I could get the transparency to render correctly was by
calling the "Render_Transparent" function (which you stated was slow):

=========================

Option Explicit On
Option Strict Off
Option Compare Binary

Imports System
Imports System.IO
Imports System.Drawing
Imports Microsoft
Imports Microsoft.VisualBasic
Imports AdvImgLib_NET
Imports AdvImgLib_NET.cAdvancedImagery

Public Class Form1

Private Declare Function GetDC Lib "USER32.DLL" (ByVal hWnd As
Int32) As Int32
Private Declare Function ReleaseDC Lib "USER32.DLL" (ByVal hWnd As
Int32, ByVal hDC As Int32) As Int32

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim objAIL As cAdvancedImagery = Nothing
Dim hIMG As Int32 = 0
Dim hDC As Int32

Me.Show()
hDC = GetDC(Me.Handle.ToInt32)

objAIL = New cAdvancedImagery

If objAIL.IO_LoadImage("C:\TEST2.TGA", hIMG) = True Then
If objAIL.Render_Transparent(hIMG, hDC) = True Then
MsgBox("Success")
Else
MsgBox("Error rendering image: " & objAIL.Error_GetLast)
End If
objAIL.IO_DestroyImage(hIMG)
Else
MsgBox("Error loading image from file: " &
objAIL.Error_GetLast)
End If

objAIL = Nothing

Call ReleaseDC(Me.Handle.ToInt32, hDC)

End Sub

End Class

=========================

If not saving to the hard drive is an absolute REQUIREMENT, then this
is your only option... unless you can change the source image to
transparent PNG instead of transparent TGA. However, the method you
came up with of loading the TGA, converting to PNG, then displaying
that PNG is the fastest and least memory intensive... so that would
be my choice. This is how I would code it:

=========================

Option Explicit On
Option Strict Off
Option Compare Binary

Imports System
Imports System.IO
Imports System.Drawing
Imports Microsoft
Imports Microsoft.VisualBasic
Imports AdvImgLib_NET
Imports AdvImgLib_NET.cAdvancedImagery

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim objAIL As cAdvancedImagery = Nothing
Dim hIMG As Int32 = 0

Me.Show()

objAIL = New cAdvancedImagery

If objAIL.IO_LoadImage("C:\TEST2.TGA", hIMG) = True Then
If objAIL.IO_SaveImage(hIMG, "C:\OUTPUT.PNG",
IMAGE_FORMAT.IF_PNG, IMAGE_FLAGS.PNG_DEFAULT, True) = True Then
PictureBox1.Image = Image.FromFile("C:\OUTPUT.PNG")
PictureBox1.Refresh()
MsgBox("Success")
Else
MsgBox("Error rendering image: " & objAIL.Error_GetLast)
End If
objAIL.IO_DestroyImage(hIMG)
Else
MsgBox("Error loading image from file: " &
objAIL.Error_GetLast)
End If

objAIL = Nothing

End Sub

End Class





Fri Oct 17, 2008 6:59 pm

kwilson1997
Offline Offline
Send Email Send Email

Forward
Message #105 of 125 |
Expand Messages Author Sort by Date

Daniel asked: "how do you load the 32-bit transparent PNG image. it shows as normal when loaded in a image box"...
Kevin Wilson
kwilson1997
Offline Send Email
Jul 26, 2008
4:16 pm

In VB5 and VB6, there's no way to load a transparent pictures into an Image control because the Image control does not have a Device Context handle (hDC)...
Kevin Wilson
kwilson1997
Offline Send Email
Jul 26, 2008
4:19 pm

I have been musing over this one for a while, as I need to display the transparency of Targa files. A picturebox in VB.net will render a 32-bit PNG correctly...
bluescreenweasel
bluescreenwe...
Offline Send Email
Oct 17, 2008
5:49 pm

further to this one of your previous replies has a more elegant method than the one I just posted - If you want to use the transparent PNG approach but don't...
bluescreenweasel
bluescreenwe...
Offline Send Email
Oct 17, 2008
5:50 pm

... method ... Keep in mind that saving to a Stream object in .NET is intended for things like pushing a custom image over the internet or network via an open...
Kevin Wilson
kwilson1997
Offline Send Email
Oct 17, 2008
6:59 pm

Great stuff kevin, many thanks. I guess i had the wrong idea about what filestreams were there. I couldn't work out why it asked for a file location string! ...
bluescreenweasel
bluescreenwe...
Offline Send Email
Oct 21, 2008
1:58 am
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help