#include Once "window9.bi"

Function Createhbitmapfromgpbitmap(Byval GpBitmap As Any Ptr,Byval iBackColor As Integer=&hf0f0f0) As HBITMAP
	
	If GpBitmap Then
		
		Dim As Any Ptr pTemp = gdk_pixbuf_copy(GpBitmap) , imNew
		
		If pTemp Then
			
			Dim As Integer iWidthIm = gdk_pixbuf_get_width(pTemp)
			
			Dim As Integer iHeightIm = gdk_pixbuf_get_height(pTemp)	
			
			Dim As cairo_surface_t Ptr surf
			
			Dim As HDC cr
			
			imNew =  gdk_pixbuf_new (GDK_COLORSPACE_RGB, True, 8 , iWidthIm, iHeightIm)
			
			Dim As Long iColorFill = ((iBackColor And &h000000ff) Shl 24) Or ((iBackColor And &h0000ff00) Shl 8) Or ((iBackColor And &h00ff0000) Shr 8) Or &hFF
			
			gdk_pixbuf_fill(imNew , iColorFill)
			
			surf = cairo_image_surface_create_for_data(gdk_pixbuf_get_pixels(imNew),CAIRO_FORMAT_ARGB32,iWidthIm,iHeightIm,gdk_pixbuf_get_rowstride(imNew))
			
			cr = cairo_create (surf)

			Dim As Ubyte Ptr pPixels = gdk_pixbuf_get_pixels(pTemp)
			
			Dim As Integer iStride = gdk_pixbuf_get_rowstride(pTemp)
			
			For y As Long = 0 To iHeightIm  -1
				
				Dim As Long Ptr row = Cast(Any Ptr,pPixels)
				
				For x As Long = 0 To iWidthIm-1
					
					row[x] = (row[x] And &hFF00FF00) Or ((row[x] And &h00FF0000) Shr 16) Or ((row[x] And &h000000FF) Shl 16)
					
				Next
				
				pPixels += iStride
				
			Next
			
			gdk_cairo_set_source_pixbuf(cr, pTemp, 0, 0)
			
			cairo_paint(cr)
			
			cairo_destroy (cr)
			
			cairo_surface_destroy(surf)	
			
			g_object_unref(pTemp)	
			
			Return imNew			
			
		Endif

	Endif	
	
End Function