Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#Compile Exe
#Dim All
%Unicode = 1
#Include "Win32API.inc"
#Include "cgdiplus.inc"
%IDC_Button = 500
%IDC_Graphic = 501
Global hDlg As Dword
Function PBMain() As Long
Dialog New Pixels, 0, "PowerBASIC",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, %IDC_Button,"Push", 50,10,100,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local temp$
Select Case Cb.Msg
Case %WM_Command
Select Case Cb.Ctl
Case %IDC_Button
temp$ = "This is a test of the Paul problem for a potential solution!"
? TextToPNG(temp$, 100, Exe.Path$ + "paul.png")
End Select
End Select
End Function
Function TextToPNG(textIn$, SizeX As Long, fName As WStringZ * %Max_Path) As String
Local Part1, Part2, sMimeType As String, Result$, M, LineCount,w,h,i,imgW,imgH As Long
Local hBMP, hDC As Dword, temp$, sEncoderClsid As Guid, pImage, hBitmap, pGraphics As Dword
Local hGraphicDC,token As Dword, StartupInput As GdiplusStartupInput
StartupInput.GdiplusVersion = 1 : GdiplusStartup(token, StartupInput, ByVal %NULL) 'initialize GDIP
Graphic Bitmap New 10,10 To hBMP : Graphic Attach hBMP, 0 : Graphic Get DC To hDC 'Create graphic bitmap
Graphic Split Word textIn$, SizeX To Part1, Part2 'begin word wrap
Result$ = Part1 + $CrLf
While Len(Part2)
Graphic Split Word Trim$(Part2), SizeX To Part1, Part2
Result$ += Part1 + $CrLf
Wend
'put in memory bitmap
LineCount = ParseCount(Result$,$CrLf) : h = Graphic(Text.Size.Y,"X")
Graphic Set Client SizeX,(LineCount-1)*h
For i = 1 To LineCount : Graphic Set Pos (0,(i-1)*h) : Graphic Print Parse$(result$, $CrLf, i) : Next i
'save as PNG
hBitmap = GetCurrentObject(hDC, %OBJ_Bitmap) 'from graphic target
GDIpCreateBitmapFromHBITMAP(hBitmap, ByVal %Null, pImage) 'create GDI+ image (pImage)
GdipCreateFromHDC hDC, pGraphics 'create graphic object containing Graphic Control
GdipDrawImageRect pGraphics, pImage, 0, 0, imgW, imgH 'use (0,0)-(140,110)
sEncoderClsid = Guid$(GDIPlusGetEncoderClsid(("image/png")))
GdipSaveImageToFile pImage,fName, sEncoderClsid, ByVal %Null 'save to file
If pImage Then GdipDisposeImage(pImage) 'cleanup + shut down GDEIP
If pGraphics Then GdipDeleteGraphics(pGraphics)
GdiplusShutdown token
End Function
http://www.garybeene.com/sw/gbsnippets.htm