#include ONCE "window9.bi"

function GetClipBoardImage() as HBITMAP export
	
	dim as GtkClipboard ptr cb = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD)
	
	if gtk_clipboard_wait_is_image_available(cb) then
		
		dim as Hbitmap im ,  imNew
		
		dim as long iWidthIm, iHeightIm
		
		dim as HDC cr
		
		dim as cairo_surface_t ptr surf
		
		im = gtk_clipboard_wait_for_image(cb)
		
		iWidthIm = Image_Width(im)
		
		iHeightIm = Image_Height(im)	
		
		if 	iWidthIm = 0 orelse iHeightIm = 0 then return 0
		
		imNew = Create_Image(iWidthIm, iHeightIm)
		
		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)
		
		cairo_paint(cr)
		
		gdk_cairo_set_source_pixbuf(cr, im, 0, 0)
		
		cairo_paint(cr)
		
		cairo_fill (cr)	
		
		cairo_destroy (cr)
		
		cairo_surface_destroy(surf)	
		
		free_image(im)
		
		dim as Ubyte ptr pPixels = gdk_pixbuf_get_pixels(imNew)
		
		dim as Integer iStride = gdk_pixbuf_get_rowstride(imNew)	
		
		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	
		
		return imNew		
		
	EndIf
	
End Function