[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

F*CK

10/4/2010 6:40:00 PM

Hello Everyone,

I am looking for lightweight code/control to display PNG images. I found a
few samples on PSC, they work but not suitable in my scenario.

I have a form on which I am drawing a gradient and on top of that I want to
show a PNG image (with transparency). using the code I found on PSC, they
are able to display the PNG *but* they clear my gradient and the form's
backcolor start showing. example:
http://img521.imageshack.us/img521/9307...

there was one AlphaImage by LaVolpe which was able to work just like a image
control, but it was buggy and didnt used to work on Windows 2000. and he has
also also deleted it from PSC now.

any inputs on how to show a PNG without messing up the gradient?


thanks in advance.
abhishek


11 Answers

tmoran4511

10/4/2010 7:11:00 PM

0

On Oct 4, 11:40 am, "Abhishek" <u...@server.com> wrote:
> Hello Everyone,
>
> I am looking for lightweight code/control to display PNG images. I found a
> few samples on PSC, they work but not suitable in my scenario.
>
> I have a form on which I am drawing a gradient and on top of that I want to
> show a PNG image (with transparency). using the code I found on PSC, they
> are able to display the PNG *but* they clear my gradient and the form's
> backcolor start showing. example:http://img521.imageshack.us/img521/9307...
>
> there was one AlphaImage by LaVolpe which was able to work just like a image
> control, but it was buggy and didnt used to work on Windows 2000. and he has
> also also deleted it from PSC now.
>
> any inputs on how to show a PNG without messing up the gradient?
>
> thanks in advance.
> abhishek

Try this image user control. Should work for you. The web address
is:

http://translate.google.com/translate?js=y&prev=_t&hl=en&ie=UTF-8&layout=1&eotf=1&u=http%3A%2F%2Fwww.leandroascierto.com.ar%2F&sl=es...

This is an english translation of a Spanish web site. On the right
side of this page, scroll down to User Controls and then click on
ucImage and ucImageList. You can then download the control.

Tom

Mayayana

10/4/2010 9:38:00 PM

0

I have straight VB code that I use to display
a PNG, usually saving it as a BMP first.
I think I got the basics from a PSC download but
that code didn't work right. Some types of PNGs
would show with a black background, upside down,
etc. I got the kinks ironed out of it and have used
it for some time in an Explorer Bar. You're welcome
to the code, but I don't know if it's really what you
want.

| any inputs on how to show a PNG without messing up the gradient?
|


Jeff Johnson [MVP: VB]

10/4/2010 10:09:00 PM

0

"Mayayana" <mayayana@invalid.nospam> wrote in message
news:i8dhbp$utt$1@news.eternal-september.org...

> I have straight VB code that I use to display
> a PNG, usually saving it as a BMP first.
> I think I got the basics from a PSC download but
> that code didn't work right. Some types of PNGs
> would show with a black background, upside down,
> etc. I got the kinks ironed out of it and have used
> it for some time in an Explorer Bar. You're welcome
> to the code, but I don't know if it's really what you
> want.

Since he apparently wants to preserve transparency you'd have to be careful
to convert the transparent areas into a mask color that doesn't exist
anywhere else in the image if you're going to have a transitional save to
BMP.


Mayayana

10/5/2010 12:32:00 AM

0

| Since he apparently wants to preserve transparency you'd have to be
careful
| to convert the transparent areas into a mask color that doesn't exist
| anywhere else in the image if you're going to have a transitional save to
| BMP.
|

Yes. I haven't looked at this for a long
time. I just retried the original code and
it works for what he wants:

http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=68355&am...

Abhishek: If you use the code in the "load from resource"
button sub and just edit it to open from a file, you'll
see it sort of works. I tested it by putting a background
picture on the form. You can specify where to draw the
image on the form and it will account for the transparent
areas, BUT, in my test the background was inverted. In
other words, the transparent areas "showed through", but
the code inverted the image in that area.

As I recall, my experience with normal display was similar.
I'm sorry that I can't remember it better. It was some
years ago the last time I looked at the code. I seem to
remember that of the several types of PNGs I was seeing
irregularities, like inversion or blackening, and I had to
just hunt down the errors for each format until they
all displayed properly.

I suspect the alpha rendering code is fine. If you're
ambitious you can try comparing my version to the
download and maybe get what you need out of the two.


Mayayana

10/5/2010 2:08:00 AM

0

I'm not sure whether this will work for all PNGs...
as I said before, there are some glitches in the
class that I linked to. But so far I have this:

Running the sample program, I changed
Command2 sub to this:

Private Sub Command2_Click()
Dim Test() As Byte
Dim filename As String
CommonDialog1.ShowOpen
filename = CommonDialog1.filename
pngClass.PicBox = Form1 'or Picturebox
pngClass.SetToBkgrnd True, 0, 300 '100, 50 'set to Background (True or
false), x and y
pngClass.BackgroundPicture = Form1 'same Backgroundpicture
pngClass.SetAlpha = True 'when Alpha then alpha
pngClass.SetTrans = True 'when transparent Color then transparent Color
pngClass.OpenPNG filename

That draws the sample picture at 0 left, 300 px from
top. I set a background picture on the form. My sample
PNG happens to be like so:

bitdepth: 4
colortype: 3

I then made 2 changes to the MakeAlpha sub in
the LoadPNG class:

ReDim DestData(bytesperrow * Me.Height - 1)
ReDim SrcData(bytesperrow * Me.Height - 1) '(UBound(Buffer))
'--*****changed
DestHdr.BitCount = 24
DestHdr.Height = Me.Height
DestHdr.Width = Me.Width
DestHdr.Planes = 1
DestHdr.Size = 40
MemDC = CreateCompatibleDC(hdc)
hBmp = CreateCompatibleBitmap(hdc, Me.Width, Me.Height)
hOldBmp = SelectObject(MemDC, hBmp)
BitBlt MemDC, 0, 0, Me.Width, Me.Height, hdc, x, y, vbSrcCopy
GetDIBits MemDC, hBmp, 0, Me.Height, SrcData(0), DestHdr, 0
MirrorData SrcData, bytesperrow '-- *******added
SelectObject hOldBmp, MemDC

With those 2 line changes, my sample PNG draws onto
the form, with the transparent areas showing through
properly.

HOWEVER, in order to make the same thing work with
the test.png image (the jelly button), which is bitdepth 8,
colortype 6, I had to change the function back to it's original
format.



Mayayana

10/5/2010 4:07:00 AM

0

Yet another iteration of this. This is using the PNG
class here:

http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=68355&am...

With a little trial and error I editied two subs so that
all test images I have access to work fine. For some
reason some PNG types need to be inverted and some
don't.

Using the sample program at the above link, I
put the following in a button sub:

pngClass.PicBox = Form1
pngClass.SetToBkgrnd True, 0, 200 'draw at 0x/200y
pngClass.BackgroundPicture = Form1
pngClass.SetAlpha = True 'when Alpha then alpha
pngClass.SetTrans = True 'when transparent Color then transparent Color
pngClass.OpenPNG filename

filename here is a string path to a PNG file.

I then put a background image on the form.

Then I used the following edits. In the MakeAlpha
sub there's an added Boolean named Reverse that
indicates whether the process of coloring the
transparent pixels should first invert the bitmap or not.
The MirrorData sub provides a similar function.
In the MakePicture sub there are several changes
or additions relating to MakeAlpha and MirrorData
calls, in order to make all PNG formats draw
properly. The new versions are below. So far I have
not found a PNG that does not render properly, with
background showing in transparent areas.

'----------------------------------

Private Sub MakeAlpha(PicObject As Object, Buffer() As Byte, Reverse As
Boolean, Optional x As Long = 0, Optional y As Long = 0)
Dim Myx As Long, Myy As Long, DatOff As Long
Dim R As Long, G As Long, b As Long, a As Long
Dim sR As Long, sG As Long, sB As Long
Dim dR As Long, dG As Long, dB As Long
Dim DestData() As Byte, bytesperrow As Long
Dim DestOff As Long, DestHdr As BITMAPINFOHEADER
Dim MemDC As Long, hBmp As Long, hOldBmp As Long
Dim SrcData() As Byte
Dim hdc As Long
On Error Resume Next
hdc = PicObject.hdc
If Err.Number = 91 Then
ReDim SrcData(UBound(Buffer))
bytesperrow = LineBytes(Me.Width, 24)
If m_OBCol = False Then
FillColorArray SrcData, Me.BkgdColor, bytesperrow
Else
FillColorArray SrcData, m_OBCol, bytesperrow
End If
ReDim DestData(bytesperrow * Me.Height - 1)
Err.Clear
Else
If PicObject.Width < Me.Width * Screen.TwipsPerPixelX Then
PicObject.Width = Screen.TwipsPerPixelX * Me.Width + 100
End If
If PicObject.Height < Me.Height * Screen.TwipsPerPixelY Then
PicObject.Height = Screen.TwipsPerPixelY * Me.Height + 100
End If
hdc = PicObject.hdc
bytesperrow = LineBytes(Me.Width, 24)
ReDim DestData(bytesperrow * Me.Height - 1)
If Reverse Then
ReDim SrcData(bytesperrow * Me.Height - 1)
Else
ReDim SrcData(UBound(Buffer))
End If
DestHdr.BitCount = 24
DestHdr.Height = Me.Height
DestHdr.Width = Me.Width
DestHdr.Planes = 1
DestHdr.Size = 40
MemDC = CreateCompatibleDC(hdc)
hBmp = CreateCompatibleBitmap(hdc, Me.Width, Me.Height)
hOldBmp = SelectObject(MemDC, hBmp)
BitBlt MemDC, 0, 0, Me.Width, Me.Height, hdc, x, y, vbSrcCopy
GetDIBits MemDC, hBmp, 0, Me.Height, SrcData(0), DestHdr, 0
If Reverse Then MirrorData SrcData, bytesperrow '-- added
SelectObject hOldBmp, MemDC
DeleteObject hBmp
DeleteDC MemDC
End If
For Myy = 0 To Me.Height - 1
For Myx = 0 To Me.Width - 1
DestOff = Myy * bytesperrow + Myx * 3
sR = SrcData(DestOff + 2)
sG = SrcData(DestOff + 1)
sB = SrcData(DestOff)
b = Buffer(DatOff)
G = Buffer(DatOff + 1)
R = Buffer(DatOff + 2)
a = Buffer(DatOff + 3)
If a = 255 Then
DestData(DestOff + 2) = R
DestData(DestOff + 1) = G
DestData(DestOff) = b
ElseIf a = 0 Then
DestData(DestOff + 2) = sR
DestData(DestOff + 1) = sG
DestData(DestOff) = sB
Else
dR = R * a + (255 - a) * sR + 255
dG = G * a + (255 - a) * sG + 255
dB = b * a + (255 - a) * sB + 255
CopyMemory DestData(DestOff + 2), ByVal VarPtr(dR) + 1, 1
CopyMemory DestData(DestOff + 1), ByVal VarPtr(dG) + 1, 1
CopyMemory DestData(DestOff), ByVal VarPtr(dB) + 1, 1
End If
DatOff = DatOff + 4
Next Myx
Next Myy
Buffer = DestData
End Sub


Private Sub MakePicture()
Dim DataSize As Long
Dim Buffer() As Byte
Dim BitCount As Integer
Dim Bitdepht As Long
Dim Drehen As Integer
m_hAlpha = False
Drehen = 1
Select Case Me.Interlacing
Case 0
DataSize = DataPerRow * Me.Height
Case 1
DataSize = (DataPerRow * Me.Height) + Me.Height
End Select
ReDim Buffer(UBound(IDATData) - 2)
CopyMemory Buffer(0), IDATData(2), UBound(IDATData) - 1
Select Case Me.Compression
Case 0
Decompress Buffer, DataSize
End Select
Select Case Me.Interlacing
Case 0
Buffer = DeFilter(Buffer)
Drehen = 1
Case 1
Buffer = DeFilterInterlaced(Buffer)
Drehen = 0
End Select
BitCount = Me.Bitdepht
Select Case Me.ColorType
Case 0 'Grayscale
Select Case Me.Bitdepht
Case 16
Conv16To8 Buffer
InitColorTable_Grey 8
BitCount = 8
BPPprivat = 8
Case 8, 4, 1
Select Case Interlacing
Case 0
BitCount = Me.Bitdepht
InitColorTable_Grey Me.Bitdepht, False
Align32 BitCount, Buffer
Case Else
BitCount = 8
InitColorTable_Grey Me.Bitdepht, True
End Select
Case 2
InitColorTable_Grey 2
If Me.Interlacing = 0 Then
Pal2To8 Me.Width, Me.Height, Buffer, DataPerRow
End If
BitCount = 8
BPPprivat = 8
End Select
If m_hTrans And m_sTrans Then
If Me.Bitdepht <> 2 Then
Align32 BitCount, Buffer
End If
PalToRGBA Me.Width, Me.Height, BitCount, Buffer
BitCount = 32
BPPprivat = 32
MakeAlpha m_BGPic, Buffer, True, m_Bgx, m_Bgy
BitCount = 24
BPPprivat = 24
End If
Case 2 'RGB
If Me.Bitdepht = 16 Then Conv16To8 Buffer
BitCount = 24
BPPprivat = 24
ReverseRGB Buffer
Drehen = 1
BPPprivat = 8
Align32 BitCount, Buffer
BPPprivat = 24
If m_hTrans And m_sTrans Then
MakeRGBTransparent Buffer
MirrorData Buffer, Me.Width * 4 '-
Drehen = 0
BitCount = 32
BPPprivat = 32
MakeAlpha m_BGPic, Buffer, False, m_Bgx, m_Bgy
BitCount = 24
BPPprivat = 24
End If
Case 3 'Palette
Select Case Me.Bitdepht
Case 8, 4, 1
If Me.Interlacing = 1 Then
BitCount = 8
BPPprivat = 8
Align32 BitCount, Buffer
MirrorData Buffer, Me.Width '-
Else
BitCount = Me.Bitdepht
If BitCount >= 8 Then
Align32 BitCount, Buffer
End If
End If
Case 2
If Me.Interlacing = 0 Then
Pal2To8 Me.Width, Me.Height, Buffer, DataPerRow
BitCount = 8
BPPprivat = 8
Else
BitCount = 8
BPPprivat = 8
Align32 BitCount, Buffer
End If
End Select
If m_hTrans And m_sTrans Then
If Me.Bitdepht <> 2 Then
Align32 BitCount, Buffer
End If
PalToRGBA Me.Width, Me.Height, BitCount, Buffer
BitCount = 32
BPPprivat = 32
If Me.Interlacing = 1 Then
MakeAlpha m_BGPic, Buffer, False, m_Bgx, m_Bgy '-
Else
MakeAlpha m_BGPic, Buffer, True, m_Bgx, m_Bgy '-
End If
BitCount = 24
BPPprivat = 24

End If
Case 4 'Grayscale + Alpha
m_hAlpha = True
If Me.Bitdepht = 16 Then Conv16To8 Buffer
GrayAToRGBA Buffer
BPPprivat = 32
BitCount = 32
MirrorData Buffer, LineBytes(Me.Width, BitCount)
Drehen = 0
If m_sAlpha = True Then
MakeAlpha m_BGPic, Buffer, False, m_Bgx, m_Bgy
BPPprivat = 24
BitCount = 24
End If
Case 6 'RGB + Alpha
m_hAlpha = True
If Me.Bitdepht = 16 Then Conv16To8 Buffer
BitCount = 32
BPPprivat = 32
ReverseRGBA Buffer
MirrorData Buffer, LineBytes(Me.Width, BitCount)
Drehen = 0
If m_sAlpha = True Then
MakeAlpha m_BGPic, Buffer, False, m_Bgx, m_Bgy
BPPprivat = 24
BitCount = 24
End If
End Select
If Not (((Me.ColorType = 3) And (BitCount = 32)) Or _
(Me.Bitdepht = 2)) Then
Select Case Me.Bitdepht
Case 16
Bitdepht = 8 'c
Bitdepht = 16
End Select
End If
Select Case BitCount
Case 1, 2, 4
Align32 BitCount, Buffer
End Select
Select Case BitCount
Case 1
Select Case Me.ColorType
Case 3
InitColorTable_1Palette Palettenbyte
Case Else
InitColorTable_1
End Select
CreateBitmap_1 Buffer, Me.Width, Me.Height, True, Colorused
DrawBitmap Me.Width, Me.Height, m_PicBox, True, m_Bgx, m_Bgy, m_settoBG
Case 4
Select Case Me.ColorType
Case 0
Case Else
InitColorTable_4 Palettenbyte
End Select
CreateBitmap_4 Buffer, Me.Width, Me.Height, True, Colorused
DrawBitmap Me.Width, Me.Height, m_PicBox, True, m_Bgx, m_Bgy, m_settoBG
Case 8
Select Case Me.ColorType
Case 0, 4
Case Else
InitColorTable_8 Palettenbyte
End Select
Drehen = 1
CreateBitmap_8 Buffer, Me.Width, Me.Height, Drehen, Colorused
DrawBitmap Me.Width, Me.Height, m_PicBox, True, m_Bgx, m_Bgy, m_settoBG
Case 24
CreateBitmap_24 Buffer, Me.Width, Me.Height, Drehen, 1
DrawBitmap Me.Width, Me.Height, m_PicBox, True, m_Bgx, m_Bgy, m_settoBG
Case 32
CreateBitmap_24 Buffer, Me.Width, Me.Height, Drehen
DrawBitmap Me.Width, Me.Height, m_PicBox, True, m_Bgx, m_Bgy, m_settoBG
End Select
End Sub


F*CK

10/6/2010 2:03:00 AM

0

thanks, that exactly what i was looking for. nice site, btw. lots of
interesting code.


"Shotgun Thom" <tmoran4511@gmail.com> wrote in message
news:e3aca5a1-d339-4b62-8179-cbea9a3e3649@30g2000yqm.googlegroups.com...
On Oct 4, 11:40 am, "Abhishek" <u...@server.com> wrote:
> Hello Everyone,
>
> I am looking for lightweight code/control to display PNG images. I found a
> few samples on PSC, they work but not suitable in my scenario.
>
> I have a form on which I am drawing a gradient and on top of that I want
> to
> show a PNG image (with transparency). using the code I found on PSC, they
> are able to display the PNG *but* they clear my gradient and the form's
> backcolor start showing.
> example:http://img521.imageshack.us/img521/9307...
>
> there was one AlphaImage by LaVolpe which was able to work just like a
> image
> control, but it was buggy and didnt used to work on Windows 2000. and he
> has
> also also deleted it from PSC now.
>
> any inputs on how to show a PNG without messing up the gradient?
>
> thanks in advance.
> abhishek

Try this image user control. Should work for you. The web address
is:

http://translate.google.com/translate?js=y&prev=_t&hl=en&ie=UTF-8&layout=1&eotf=1&u=http%3A%2F%2Fwww.leandroascierto.com.ar%2F&sl=es...

This is an english translation of a Spanish web site. On the right
side of this page, scroll down to User Controls and then click on
ucImage and ucImageList. You can then download the control.

Tom


F*CK

10/8/2010 4:16:00 AM

0

Thanks, this also works. it doesnt need GDI+


"Mayayana" <mayayana@invalid.nospam> wrote in message
news:i8driu$st8$1@news.eternal-september.org...
|| Since he apparently wants to preserve transparency you'd have to be
| careful
|| to convert the transparent areas into a mask color that doesn't exist
|| anywhere else in the image if you're going to have a transitional save to
|| BMP.
||
|
| Yes. I haven't looked at this for a long
| time. I just retried the original code and
| it works for what he wants:
|
|
http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=68355&am...
|
| Abhishek: If you use the code in the "load from resource"
| button sub and just edit it to open from a file, you'll
| see it sort of works. I tested it by putting a background
| picture on the form. You can specify where to draw the
| image on the form and it will account for the transparent
| areas, BUT, in my test the background was inverted. In
| other words, the transparent areas "showed through", but
| the code inverted the image in that area.
|
| As I recall, my experience with normal display was similar.
| I'm sorry that I can't remember it better. It was some
| years ago the last time I looked at the code. I seem to
| remember that of the several types of PNGs I was seeing
| irregularities, like inversion or blackening, and I had to
| just hunt down the errors for each format until they
| all displayed properly.
|
| I suspect the alpha rendering code is fine. If you're
| ambitious you can try comparing my version to the
| download and maybe get what you need out of the two.
|
|


F*CK

10/8/2010 4:19:00 AM

0

i found another solution, i can also use AlphaIcons instead of PNG.

load icons using ExtractIcon and then Drawing it using DrawIcon. just few
lines of code and same effect.


"Abhishek" <user@server.com> wrote in message
news:i8d726$efl$1@speranza.aioe.org...
| Hello Everyone,
|
| I am looking for lightweight code/control to display PNG images. I found a
| few samples on PSC, they work but not suitable in my scenario.
|
| I have a form on which I am drawing a gradient and on top of that I want
to
| show a PNG image (with transparency). using the code I found on PSC, they
| are able to display the PNG *but* they clear my gradient and the form's
| backcolor start showing. example:
| http://img521.imageshack.us/img521/9307...
|
| there was one AlphaImage by LaVolpe which was able to work just like a
image
| control, but it was buggy and didnt used to work on Windows 2000. and he
has
| also also deleted it from PSC now.
|
| any inputs on how to show a PNG without messing up the gradient?
|
|
| thanks in advance.
| abhishek
|
|


Mayayana

10/8/2010 1:33:00 PM

0

| Thanks, this also works. it doesnt need GDI+
|

Yes, it reads directly. There's some impressive code
there, which also seems to include an inline deflate
routine. But it doesn't seem to be entirely finished,
as you can see by the edits I had to make. and it can
be a bit slow on larger images. Your post prompted me
to look at the class again and see if maybe I can
improve upon it.