# Visual Basic > Visual Basic 6 and Earlier >  [RESOLVED] how can add save as png function to this attached class for layered canvas

## Black_Storm

i found this class about drawing on hdc and show like as layered window named "*cCanvasLayeredWindow.cls*" , i attached class.

now i need save canvas as png ,i found a function about save as jpg and its work good :




```
Public Sub SaveJPG(ByVal filename As String, Optional ByVal quality As Long)     '= 80
        Dim tSI        As GdiplusStartupInput
        Dim lRes       As Long
        Dim lGDIP      As Long
        Dim lBitmap    As Long
        
        ' Initialize GDI+
        tSI.GdiplusVersion = 1
        lRes = GdiplusStartup(lGDIP, tSI)
        
        If lRes = 0 Then
            ' Create the GDI+ bitmap from the image handle
            lRes = GdipCreateBitmapFromHBITMAP(c_lDIB, 0, lBitmap)
            If lRes = 0 Then
                Dim tJpgEncoder As GUID
                Dim tParams As EncoderParameters
                ' Initialize the encoder GUID
                CLSIDFromString StrPtr("{557CF401-1A04-11D3-9A73-0000F81EF32E}"), tJpgEncoder
                ' Initialize the encoder parameters
                tParams.Count = 1
                With tParams.Parameter    ' Quality
                    ' Set the Quality GUID
                    CLSIDFromString StrPtr("{1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}"), .GUID
                    .NumberOfValues = 1
                    .Type = 4
                    .Value = VarPtr(quality)
                End With
                
                ' Save the image
                lRes = GdipSaveImageToFile( _
                lBitmap, _
                StrPtr(filename), _
                tJpgEncoder, _
                tParams)
                ' Destroy the bitmap
                GdipDisposeImage lBitmap
            End If
            ' Shutdown GDI+
            GdiplusShutdown lGDIP
        End If
        If lRes Then
            Err.Raise 5, , "Cannot save the image. GDI+ Error:" & lRes
        End If
    End Sub
```



 but how can save as png with keep transparent backgroud?
i found some class about work with dib full or create png and drawing and save as png but after combining with that attached class its will be save with black background color

----------


## Black_Storm

resolved with :


```
Public Function SaveAsPng(FileName As String) As Boolean
    Dim hImage As Long
    Dim tEncoder  As GUID


    GdipCreateBitmapFromScan0 tSIZE.cx, tSIZE.cy, tSIZE.cx * 4, PixelFormat32bppPARGB, ByVal m_Pointer, hImage
    GdipImageRotateFlip hImage, &H6
    CLSIDFromString StrPtr(ImageCodecPNG), tEncoder
    SaveAsPng = GdipSaveImageToFile(hImage, StrPtr(FileName), tEncoder, ByVal 0) = 0
    GdipDisposeImage hImage
End Function
```

----------

