Drawing onto c32bppDIB causes transparent areas when saved (but not when rendered)
Hello!
I would like to load an image into a c32bppDIB class and then draw onto its dc.
I do it like this:
Code:
Dim c As c32bppDIB
Set c = New c32bppDIB
c.LoadPicture_File "D:\someimage.png"
Dim lDC&
lDC = c.LoadDIBinDC(True)
APIFillRect lDC, r, vbRed
c.SaveToFile_PNG "c:\users\myuser\desktop\test1.png", False
If I render it onto a dc, a do see the red area.
However, if I save it to a file, the red area is transparent.
If I save it using c.SaveToFile_JPG "c:\users\myuser\desktop\test1.jpg", False, vbBlue, the red area is blue. So it's obvious that drawing the area causes the area to become transparent.
How do I avoid this?
Thanks for any help!
ps:
This drawing function works fine, I have used it since several years.
I'm just stating it here for completeness:
Code:
Public Function APIRectangle(ByVal hDC As Long, _
ByVal X As Long, _
ByVal Y As Long, _
ByVal w As Long, _
ByVal h As Long, _
Optional lColor As OLE_COLOR = -1) As Long
On Error GoTo APIRectangle_Error
Dim hPen As Long, hPenOld As Long
Dim pt As POINTAPI
hPen = CreatePen(0, 1, lColor)
hPenOld = SelectObject(hDC, hPen)
MoveToEx hDC, X, Y, pt
LineTo hDC, X + w, Y
LineTo hDC, X + w, Y + h
LineTo hDC, X, Y + h
LineTo hDC, X, Y
SelectObject hDC, hPenOld
DeleteObject hPen
Exit Function
APIRectangle_Error:
End Function
Edit:
I have resolved it by calling this sub when I'm done drawing:
Code:
Public Sub MakeOpaque()
Dim tSA As SafeArray
Dim dibBytes() As Byte
' here we will force every alpha byte to be fully opaque
iparseOverlayHost_Byte dibBytes, VarPtr(tSA), 2, m_Height, m_Width * 4&, m_Pointer ' overlay DMA array
Dim x&
Dim y&
For y = 0& To m_Height - 1&
For x = 3& To m_Width * 4& - 1& Step 4&
dibBytes(x, y) = 255
Next
Next
iparseOverlayHost_Byte dibBytes, 0, 0, 0, 0, 0 ' remove DMA overlay
m_AlphaImage = AlphaNone ' we are not using transparency
m_Format = imgCheckerBoard ' special flag for user
End Sub
I have stolen it from the sub CreateWhiteCanvas.
It works now, but I wonder why I have to make the pixels opaque again after drawing onto them.
Thanks for any insights.