Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WIA / PNG 1

Status
Not open for further replies.

vitaloverdose

Programmer
Jan 29, 2006
13
GB
Hi there , a while ago i wrote some image manipulation functions using microsofts WIA mainly because it allows you to easily load png images and modify them on a perpixel basis. So i then wrote a GUI (the program was a button editor) but when ive come to use the functions in the main program im now having problems with transparent pixels being saved as black. Ive gone back and checked all the old test bits of code i made when i first made the functions and they are giving me the same results.

Hers an example of the code ive been using
Code:
Private Sub Modify()
Dim Img As ImageFile
Dim ImgB As ImageFile
Dim IP As ImageProcess
Dim vA As Vector
Dim vB As Vector
Dim i As Long

Set ImgB = CreateObject("WIA.ImageFile")
Set Img = CreateObject("WIA.ImageFile")
Set IP = CreateObject("WIA.ImageProcess")

MakePic ImgB, 40, 40

Img.LoadFile "C:\Test.png"

Set vA = Img.ARGBData
Set vB = ImgB.ARGBData

For i = 1 To vA.Count
    vB(i) = vA(i)
Next

IP.Filters.Add IP.FilterInfos("ARGB").FilterID
Set IP.Filters(1).Properties("ARGBData") = vB
Set ImgB = IP.Apply(ImgB)
ImgB.SaveFile "C:\done.png"
End Sub


Public Function MakePic(ImgBlank As ImageFile, W As Long, H As Long)
Dim WiaImgProcessRes As ImageProcess
Dim nVec As Vector
Dim nCol As Long
nCol = -1
'=============== Create 2x2 blank pic
Set nVec = CreateObject("WIA.Vector")
nVec.Add nCol: nVec.Add nCol: nVec.Add nCol: nVec.Add nCol
Set ImgBlank = nVec.ImageFile(2, 2)
'=============== resize blank pic
Set WiaImgProcessRes = CreateObject("WIA.ImageProcess")
WiaImgProcessRes.Filters.Add WiaImgProcessRes.FilterInfos("Scale").FilterID
WiaImgProcessRes.Filters(1).Properties!PreserveAspectRatio = False
WiaImgProcessRes.Filters(1).Properties("MaximumWidth") = W
WiaImgProcessRes.Filters(1).Properties("MaximumHeight") = H
Set ImgBlank = WiaImgProcessRes.Apply(ImgBlank)
End Function

Its simply loads in a png (with some transparent pixels) and puts it into an array, i then make a blank image(of transparent pixels) of the same size and put that into another array. Then copy from one array to the other and save. Normal this mixes the 2 images fine but now the transparent pixels are being saved as black for some reason ?
Thanks
 
Has the WIA Scale Filter ever preserved the Alpha channel? In other words, I suspect your "blank" is not transparent.
 
Hi dilettante , yes the blank image is being created correctly ,i saved it off to check.

Ive run some tests on the code:

When reading from the array:
Trans = 0
Black = -16777216
red = -49088

When writing to the array:
Trans should be -1

Just to see what happens i added a check for the color 0 and swapped it for -1 when passing the data from one Va to Vb.This worked fine for the 100% transparent pixels but anything partially transparent is still coming through as black. It almost looks like the process of assigning the image to the vector array is killing the alpha channel but that seems unlikely as the A in ARGB
Code:
Set vA = Img.ARGBData
would indicate that the alpha channel is somehow included.

The only thing i can think of is maybe the color is read in one format and has to be written in a different format?
 
Very confusing. I keep looking at this but I suspect I am missing something important.
 
On further testing it seems that assigning the image.ARGBData to a vector doesnt alter the color of transparent pixels on its own. I tried the sample from Microsoft on a png and it saves the trans pixels correctly.
Code:
Dim Img 'As ImageFile
Dim IP 'As ImageProcess
Dim v 'As Vector
Dim i 'As Long

Set Img = CreateObject("WIA.ImageFile")
Set IP = CreateObject("WIA.ImageProcess")

Img.LoadFile "C:\ImageWithSomeTransPix.png"

Set v = Img.ARGBData

For i = 1 To v.Count Step 21
    v(i) = &HFFFF00FF 'opaque pink (A=255,R=255,G=0,B=255)
Next

IP.Filters.Add IP.FilterInfos("ARGB").FilterID
Set IP.Filters(1).Properties("ARGBData") = v

Set Img = IP.Apply(Img)

Img.SaveFile "C:\ImageWithSomeTransPixB.png"

 
Would the Alpha layer have anything to do with V(0) because the sample code from MS starts the for loop at V(1)?
Code:
Set v = Img.ARGBData

For i = 1 To v.Count Step 21
    v(i) = &HFFFF00FF
    Next
 
My mistake dilettante you were right on the button with that one... the blank pic is not being created as transparent. I cant see way to create a blank trans image at the moment though. As a last resort i could always have one pre-loaded into my app and use that when i need to. :)
 
Well you probably could create one, if you're willing to wait for it!

I don't think the Scale filter can do a clean enough job. You'd have to load up a full size Vector pixel by pixel which is a slow process.


Even if you get this working what result would you expect? If you want to combine two Alpha images you might try the Stamp filter instead.
 
I got it working by loading a 16x16 blank trans png into memory when the project opens up.I can then copy and resize this whenever i need a new blank trans image. I think this would be quicker than generating a new pic each time anyway.
Code:
'*MainForm
Private Sub Form_Load()
Set ImgBlank = CreateObject("WIA.ImageFile")
ImgBlank.LoadFile (App.Path & "\images\Blank.png") ' 16x16 blank png trans
End Sub

'*module code
Global ImgBlank as ImageFile

Public Function WIANewImg(W As Long, H As Long) as ImageFile
Dim Ip As ImageProcess
Set Ip = CreateObject("WIA.ImageProcess")
Ip.Filters.Add 
Ip.FilterInfos("Scale").FilterID
Ip.Filters(1).Properties!PreserveAspectRatio = False
Ip.Filters(1).Properties("MaximumWidth") = W
Ip.Filters(1).Properties("MaximumHeight") = H
Set NewWIAImg = Ip.Apply(ImgBlank)
End Function
For what im doing i need to be able to check/modify certain pixels as well as copy to a new image so im killing 2 birds with one stone. I checked the speed on the various filters and was surprised how much faster it was to use the ARGB filter on its own than using the crop and stamp filters together to do the same task.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top