Display window with PNG image file

Windows specific questions.
Post Reply
visualfreebasic5
Posts: 25
Joined: Dec 11, 2019 15:14
Contact:

Display window with PNG image file

Post by visualfreebasic5 »

PNG image is displayed on a window, and it will not be displayed on the window except for the image
Parameters:
hWndForm Target Window Handle
PNG_img PNG image file name
m_Width Size of the target window
m_Height

Code: Select all

Sub PNGwindowImageDisplay(hWndForm As hWnd,PNG_img As String  , m_Width As Long  ,m_Height As Long )

   Dim nDc As HDC = GetDC(hWndForm)
   Dim As HDC hMemDC =CreateCompatibleDC(nDc)

   Dim bmpinfo As BITMAPINFO
   bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER)
   bmpinfo.bmiHeader.biBitCount = 32
   bmpinfo.bmiHeader.biHeight = m_Height
   bmpinfo.bmiHeader.biWidth = m_Width
   bmpinfo.bmiHeader.biPlanes = 1
   bmpinfo.bmiHeader.biCompression = BI_RGB
   bmpinfo.bmiHeader.biXPelsPerMeter = 0
   bmpinfo.bmiHeader.biYPelsPerMeter = 0
   bmpinfo.bmiHeader.biClrUsed = 0
   bmpinfo.bmiHeader.biClrImportant = 0
   bmpinfo.bmiHeader.biSizeImage = bmpinfo.bmiHeader.biWidth * bmpinfo.bmiHeader.biHeight * bmpinfo.bmiHeader.biBitCount / 8
   Dim pp As Long Ptr
   Dim m_Bmp As HBITMAP = CreateDIBSection(hMemDC, @bmpinfo, DIB_RGB_COLORS, @pp, 0, 0)

   Dim hOldBitmap As HBITMAP = SelectObject(hMemDC, m_Bmp)
   Dim m_GpDC As GpGraphics Ptr ' 主DC 的 GDI+句柄
   GdipCreateFromHDC(hMemDC, @m_GpDC)
   Dim nGpimage As GpImage Ptr ,wFile As Wstring *260 = PNG_img
   GdipLoadImageFromFile(wFile , @nGpimage)
   GdipDrawImageRectRect m_GpDC, nGpimage, 10, 10, 186, 114, 0, 0, 186, 114, 2, NULL, NULL, NULL
   GdipDisposeImage nGpimage
   
   GdipDeleteGraphics(m_GpDC)
   
   Dim ptSrc As POINT
   Dim blf As BLENDFUNCTION
   blf.BlendOp = AC_SRC_OVER
   blf.BlendFlags = 0
   blf.SourceConstantAlpha = 255
   blf.AlphaFormat = AC_SRC_ALPHA
   Dim psize As POINT
   psize.x = m_Width
   psize.y = m_Height
   UpdateLayeredWindow(hWndForm, NULL, NULL, @psize, hMemDC, @ptSrc, 0, @blf, ULW_ALPHA)
   SelectObject(hMemDC, hOldBitmap)
   DeleteObject(m_Bmp)
   DeleteDC(hMemDC)
   ReleaseDC(hWndForm, nDc)
End Sub
ruthmarx
Posts: 1
Joined: Jan 24, 2023 14:14

Re: Display window with PNG image file

Post by ruthmarx »

Thank you. Might use in future. Although I prefer to save all graphic files into jpg
UEZ
Posts: 856
Joined: May 05, 2017 19:59
Location: Germany

Re: Display window with PNG image file

Post by UEZ »

Here an example how to use that function (which I have modified a little bit):

Code: Select all

#Ifdef __Fb_64bit__
    #Inclib "gdiplus"
    #Include Once "win/gdiplus-c.bi"
#Else
    #Include Once "win/gdiplus.bi"
    Using gdiplus
#Endif
#Include "fbgfx.bi"
Using FB

Sub PNGwindowImageDisplay(hWndForm As hWnd,PNG_img As String  , m_Width As Long  ,m_Height As Long )

   Dim nDc As HDC = GetDC(hWndForm)
   Dim As HDC hMemDC =CreateCompatibleDC(nDc)
   Dim bmpinfo As BITMAPINFO
   bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER)
   bmpinfo.bmiHeader.biBitCount = 32
   bmpinfo.bmiHeader.biHeight = m_Height
   bmpinfo.bmiHeader.biWidth = m_Width
   bmpinfo.bmiHeader.biPlanes = 1
   bmpinfo.bmiHeader.biCompression = BI_RGB
   bmpinfo.bmiHeader.biXPelsPerMeter = 0
   bmpinfo.bmiHeader.biYPelsPerMeter = 0
   bmpinfo.bmiHeader.biClrUsed = 0
   bmpinfo.bmiHeader.biClrImportant = 0
   bmpinfo.bmiHeader.biSizeImage = bmpinfo.bmiHeader.biWidth * bmpinfo.bmiHeader.biHeight * bmpinfo.bmiHeader.biBitCount / 8
   Dim pp As Long Ptr
   Dim m_Bmp As HBITMAP = CreateDIBSection(hMemDC, @bmpinfo, DIB_RGB_COLORS, @pp, 0, 0)

   Dim hOldBitmap As HBITMAP = SelectObject(hMemDC, m_Bmp)
   Dim m_GpDC As GpGraphics Ptr ' 主DC 的 GDI+句柄
   GdipCreateFromHDC(hMemDC, @m_GpDC)
   Dim nGpimage As GpImage Ptr ,wFile As Wstring *260 = PNG_img
   GdipLoadImageFromFile(wFile , @nGpimage)
   GdipDrawImageRectRect m_GpDC, nGpimage, 0, 0, m_Width, m_Height, 0, 0, m_Width, m_Height, 2, NULL, NULL, NULL
   GdipDisposeImage nGpimage
   
   GdipDeleteGraphics(m_GpDC)
   
   Dim ptSrc As POINT
   Dim blf As BLENDFUNCTION
   blf.BlendOp = AC_SRC_OVER
   blf.BlendFlags = 0
   blf.SourceConstantAlpha = 255
   blf.AlphaFormat = AC_SRC_ALPHA
   Dim psize As POINT
   psize.x = m_Width
   psize.y = m_Height
   UpdateLayeredWindow(hWndForm, NULL, NULL, Cast(Any Ptr, @psize), hMemDC, @ptSrc, 0, @blf, ULW_ALPHA)
   SelectObject(hMemDC, hOldBitmap)
   DeleteObject(m_Bmp)
   DeleteDC(hMemDC)
   ReleaseDC(hWndForm, nDc)
End Sub

Function WndProc(hWnd As HWND, uMsg As UINT, wParam As WPARAM, lParam As LPARAM) As Integer
	Select Case As Const uMsg
		Case WM_CLOSE
			PostQuitMessage(0)
			Return 0
		Case Else
			Return DefWindowProc(hWnd, uMsg, wParam, lParam)
	End Select
End Function

Dim Shared As ULONG_PTR GDIPlusToken
Dim Shared As Single w, h
Dim As Any Ptr hImage_png

Dim GDIPlusStartupInput As GDIPLUSSTARTUPINPUT 
Dim As String sPNG_image = "PATH YOUR TRANSPARENT PNG IMAGE FILE" '<<<<<<<<<<<<<<<
GDIPlusStartupInput.GdiplusVersion = 1 
GdiplusStartup(@GDIPlusToken, @GDIPlusStartupInput, NULL)
GdipLoadImageFromFile(sPNG_image, @hImage_png)
GdipGetImageDimension(hImage_png, @w, @h)
GdipDisposeImage(hImage_png)

Dim Shared As HWND hGUI
Dim GUI As WNDCLASSEX
Dim szAppName As ZString * 30 => "FB GUI"
Dim As String sTitle = "GDI/GDI+ Demo by UEZ"

With GUI
	.style         = CS_HREDRAW Or CS_VREDRAW
	.lpfnWndProc   = @WndProc
	.cbClsExtra    = NULL
	.cbWndExtra    = NULL
	.hInstance     = GetModuleHandle(NULL)
	.hIcon         = LoadIcon(NULL, IDI_APPLICATION)
	.hCursor       = LoadCursor(NULL, IDC_ARROW)
	.hbrBackground = GetStockObject(WHITE_BRUSH)
	.lpszMenuName  = NULL
	.lpszClassName = @szAppName
	.cbSize		   = SizeOf(WNDCLASSEX)
End With
Dim As Long ExStyle = WS_EX_TOPMOST Or WS_EX_LAYERED, Style = WS_OVERLAPPEDWINDOW Or WS_VISIBLE

RegisterClassEx(@GUI)

Dim As Integer sW, sH
ScreenInfo(sW, sH) 

hGUI = CreateWindowEx(ExStyle, GUI.lpszClassName, sTitle, _
							  Style, _
							  (sW - w) / 2, (sH - h) / 2, _
							  w, h, _
							  NULL, NULL, GUI.hInstance, NULL)
ShowWindow(hGUI, SW_SHOWNORMAL)

PNGwindowImageDisplay(hGUI, sPNG_image, w, h)

GdiplusShutdown(GDIPlusToken)

Dim uMsg As MSG

While GetMessage(@uMsg, NULL, NULL, NULL) = 1
	TranslateMessage(@uMsg)
	DispatchMessage(@uMsg)
Wend
Don't forget to modify the string to the PNG image.


I also used this technique which can be found here Magnifier build 2020-03-05 [Windows only] ^^


ruthmarx wrote: Jan 24, 2023 14:16 Thank you. Might use in future. Although I prefer to save all graphic files into jpg
The purpose of that function is not to save images but to display transparent image in a windows gui.
Post Reply