Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#Compiler PBWin 10
#Compile Exe
#Dim All
%Unicode = 1
#Include "Win32API.inc"
#Include "cgdiplus.inc"
Enum Equates Singular
IDC_DrawSprite = 500
IDC_EraseSprite
IDC_Clear
IDC_Graphic
IDC_Select
IDC_Name
IDC_Theta
End Enum
Global hDlg As Dword
Function PBMain() As Long
Dialog New Pixels, 0, "PowerBASIC",300,300,400,400, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, %IDC_DrawSprite,"Draw Sprite", 10,10,70,20
Control Add Button, hDlg, %IDC_EraseSprite,"Erase Last", 90,10,70,20
Control Add Button, hDlg, %IDC_Clear,"Clear All", 170,10,70,20
Control Add TextBox, hDlg, %IDC_Theta,"0", 250,10,70,20
Control Add Button, hDlg, %IDC_Select, "Image ...", 10,40,100,20
Control Add TextBox, hDlg, %IDC_Name, "world.png", 130,40,200,20
Control Add Graphic, hDlg, %IDC_Graphic, "", 0,70,300,300
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local w,h As Long, hGraphicDC As Dword, temp$, theta As Single
Local XCenter, YCenter As Long
Select Case Cb.Msg
Case %WM_Command
Select Case Cb.Ctl
Case %IDC_DrawSprite
Graphic Attach hDlg, %IDC_Graphic, ReDraw
Graphic Clear %rgb_LightBlue
Graphic Get DC To hGraphicDC
Control Get Text hDlg, %IDC_Theta To temp$ : Theta = Val(temp$) * 0.01745
Control Get Text hDlg, %IDC_Name To temp$
XCenter = 200 : YCenter = 200
DrawSprite hGraphicDC, temp$, theta, XCenter, YCenter
Graphic ReDraw
End Select
Case %WM_Size
Dialog Get Client hDlg To w,h
Control Set Client hDlg, %IDC_Graphic, w,h
End Select
End Function
Sub DrawSprite(hDCG As Dword, ImgName As String, Theta As Single, XTarget As Long, YTarget As Long)
Local pGraphics,pImage,token As Dword, StartupInput As GdiplusStartupInput
Local imgW,imgH, imgWR, imgHR,XCenter,YCenter As Long, hBMP,hDC,hBMPR,hDCR As Dword
Local i,x,y As Long
Dim PlgPts(0 To 2) As PointAPI
'initialize GDI
StartupInput.GdiplusVersion = 1 'initialize GDIPlus
GdiplusStartup(token, StartupInput, ByVal %NULL) 'initialize GDIPlus
GdipLoadImageFromFile((ImgName), pImage) 'load image
GdipGetImageWidth(pImage,imgW) 'get width
GdipGetImageHeight(pImage,imgH) 'get height
Graphic Bitmap New imgW, imgH To hBMP
Graphic Attach hBMP, 0
Graphic Get DC To hDC
GdipCreateFromHDC(hDC, pGraphics) ' Create the Graphic object
GdipDrawImage(pGraphics, pImage, 0, 0) ' Draw the image
'shutdown GDI
If pImage Then GdipDisposeImage(pImage) 'cleanup
If pGraphics Then GdipDeleteGraphics(pGraphics) 'cleanup
GdiplusShutdown token 'shutdown GDI+
'rotation bitmap
imgWR = 2 * imgW : imgHR = 2 * imgH
Graphic Bitmap New imgWR,imgHR To hBMPR
Graphic Attach hBMPR, 0
Graphic Color %Black, %Black
Graphic Get DC To hDCR
'rotation of original bitmap to a temporary memory bitmap
XCenter = imgW/2 : YCenter = imgH/2
x = 0 : y = 0 'point in original bitmap
PlgPts(0).X = XCenter + (x - XCenter) * Cos(theta) - (y - YCenter) * Sin(theta) 'upper-left in target
PlgPts(0).Y = YCenter + (x - XCenter) * Sin(theta) + (y - YCenter) * Cos(theta)
x = imgW : y = 0 'point in original bitmap
PlgPts(1).X = XCenter + (x - XCenter) * Cos(theta) - (y - YCenter) * Sin(theta) 'upper-right in target
PlgPts(1).Y = YCenter + (x - XCenter) * Sin(theta) + (y - YCenter) * Cos(theta)
x = 0 : y = imgH 'point in original bitmap
PlgPts(2).X = XCenter + (x - XCenter) * Cos(theta) - (y - YCenter) * Sin(theta) 'lower left in target
PlgPts(2).Y = YCenter + (x - XCenter) * Sin(theta) + (y - YCenter) * Cos(theta)
Graphic Attach hBMPR, 0
Graphic Clear
PlgBlt(hDCR, PlgPts(0), hDC, 0, 0, imgW, imgH, 0&, 0, 0) ' Draw rotated image
'place rotated bitmap into the main graphic
Graphic Attach hDlg, %IDC_Graphic, ReDraw
' TransparentBlt hGraphicDC,s.x-s.w/2-dxr,s.y-s.h-dyr,RW,RH,hDCR,0,0,RW,RH,%Black
TransparentBlt hDCG,XTarget-imgWR/2,YTarget-imgHR/2,imgWR,imgHR,hDCR,0,0,imgWR,imgHR,%Black
End Sub
'gbs_01420
'Date: 10-17-2014
http://www.garybeene.com/sw/gbsnippets.htm