Simple Rotate Image using GDI+ [Windows]

Share your advanced PureBasic knowledge/code with the community.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Simple Rotate Image using GDI+ [Windows]

Post by RASHAD »

Support 24,32 bit depth (Transparent image)
Edit :
Bad news : The problem with PB Vector Library Rotate function comes from GDI+
So I think that is why freak did not respond to Vector lib. bug reports

Code: Select all

#RotateNoneFlipNone = 0
#Rotate90FlipNone   = 1
#Rotate180FlipNone  = 2
#Rotate270FlipNone  = 3
#RotateNoneFlipX    = 4
#Rotate90FlipX      = 5
#Rotate180FlipX     = 6
#Rotate270FlipX     = 7

Structure RectF
  x.f
  y.f
  width.f
  height.f
EndStructure

Structure GdiplusStartupInput
  GdiPlusVersion.i
  *DebugEventCallback.DebugEventProc
  SuppressBackgroundThread.i
  SuppressExternalCodecs.i
EndStructure

; Structure ImageCodecInfo
;   clsid.CLSID
;   formatID.GUID
;   *codecName
;   *dllName
;   *formatDescription
;   *filenameExtension
;   *mimeType
;   flags.l
;   version.l
;   sigCount.l
;   sigSize.l
;   *sigPattern.byte
;   *sigMask.byte
; EndStructure

Global token,image,gfx ,img.RectF; ,Filename$ ,initpath$

If OpenLibrary(0, "gdiplus.dll") = 0
  MessageRequester("Error","Required component gdiplus.dll is not found. Please install it and retry    ", #MB_ICONERROR)
  End
EndIf

Import "gdiplus.lib"
  GdiplusStartup(token, *input.GdiplusStartupInput, output)
  GdiplusShutdown(token)
  GdipCreateFromHDC(hdc, *graphics)
  GdipLoadImageFromFile(filename.p-unicode, *image)
  GdipGetImageHeight(*image, *height.integer)
  GdipGetImageWidth(*image, *width.integer)
  GdipImageRotateFlip(*image, rotateFlipType)
  GdipGetImageBounds(*image, *srcRect.RectF, *srcUnit.integer)
  GdipDrawImage(*graphics, *image, x.f, y.f)
  GdipDrawImageI(*graphics, *image, x.l, y.l)
  GdipDrawImageRectI(*graphics, *image, x.l, y.l, width.l, height.l)
  GdipDrawImageRect(*graphics, *image, x.f, y.f, width.f, height.f)
  GdipGraphicsClear(*graphics, color)
  GdipDisposeImage(image)
  GdipDeleteGraphics(*graphics)
EndImport

Procedure LoadFile(Filename$)
  If GdipLoadImageFromFile(Filename$, @image) = 0
    GdipGetImageBounds(image, @img.RectF, @Unit) 
    If img\width > img\height
      CreateImage(1,img\width,img\width,32,#PB_Image_Transparent)
    Else
      CreateImage(1,img\height,img\height,32,#PB_Image_Transparent)
    EndIf
    hdc = StartDrawing(ImageOutput(1))
    GdipCreateFromHDC(hdc, @gfx)
    GdipDrawImageI(gfx, image, 0, 0)
    StopDrawing()
  Else
    MessageRequester("Error","No Loaded Image ...",#PB_MessageRequester_Ok|#MB_ICONERROR)
  EndIf
EndProcedure

Procedure rotimg()
  rotflag = GetGadgetState(3)
  hdc = StartDrawing(ImageOutput(1))
  GdipGraphicsClear(gfx, RGBA(255,255,255,0))
  GdipImageRotateFlip(image, rotflag)
  GdipDrawImageI(gfx, image, 0, 0)
  StopDrawing()
  SetGadgetState(0,ImageID(1))
EndProcedure

Procedure sizeCB()
  ResizeGadget(0,#PB_Ignore,#PB_Ignore,WindowWidth(0)-20,WindowHeight(0)-50)
EndProcedure

initpath$ = "c:\"
pattern$ = "All supported formats|*.bmp;*.gif; *.jpg; *.jpeg;*.wmf; *.emf; *.png; *.tif;*.tiff;*.tga;*.ppm;*pgm;*ico;*.cur;*ani|"+
           "TGA image (*.tga)|*.tga|TIF image (*.tif)|*.tif|TIFF image (*.tiff)|*.tiff|PNG image (*.png)|*.png|"+
           "BMP image (*.bmp)|*.bmp|JPEG image (*.jpg;*.jpeg)|*.jpg;*.jpeg;*.jp2|GIF image (*.gif)|*.gif|WMF Image (*.wmf)|*.wmf|EMF Image (*.emf)|*.emf|"+
           "PPM image (*.ppm)|*.ppm|PGM image (*.pgm)|*.pgm |Icon (*.ico)|*.ico|Cursor (*.cur)|*.cur|Animated Cursor (*.ani)|*.ani"

CreateImage(10, 16,16)
StartDrawing(ImageOutput(10))
  Box(0,0,8,8,$D9D9D9)
  Box(8,0,8,8,$FFFFFF)
  Box(0,8,8,8,$FFFFFF)
  Box(8,8,8,8,$D9D9D9)
StopDrawing()
hBrush = CreatePatternBrush_(ImageID(10))

flags = #PB_Window_SystemMenu| #PB_Window_MaximizeGadget| #PB_Window_MinimizeGadget| #PB_Window_ScreenCentered | #PB_Window_SizeGadget
OpenWindow(0,0,0,400,300,"Test",Flags)
SetClassLongPtr_(WindowID(0), #GCL_HBRBACKGROUND, hBrush)
ImageGadget(0,10,40,380,250,0)
ButtonGadget(1,10,10,60,26,"LOAD")
ButtonGadget(2,80,10,60,26,"RUN")
ComboBoxGadget(3,150,10,180,26)
AddGadgetItem(3, -1,"#RotateNoneFlipNone")
AddGadgetItem(3, -1,"#Rotate90FlipNone")
AddGadgetItem(3, -1,"#Rotate180FlipNone")
AddGadgetItem(3, -1,"#Rotate270FlipNone")
AddGadgetItem(3, -1,"#RotateNoneFlipX")
AddGadgetItem(3, -1,"#Rotate90FlipX")
AddGadgetItem(3, -1,"#Rotate180FlipX")
AddGadgetItem(3, -1,"#Rotate270FlipX")
SetGadgetState(3,1)
BindEvent(#PB_Event_SizeWindow,@sizeCB())
input.GdiplusStartupInput
input\GdiPlusVersion = 1.1
GdiplusStartup(@token, @input, #Null)
If token
  Repeat   
    Select WaitWindowEvent()       
      Case #PB_Event_CloseWindow
        Quit = 1     
        
      Case #PB_Event_Menu
        Select EventMenu()
          Case 1           
        EndSelect
        
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1  ;Load Image
            Filename$ = OpenFileRequester("Choose image file to load", initpath$+"*.*", Pattern$, 0)
            If Filename$ And FileSize(Filename$)
              initpath$ = GetPathPart(Filename$)
              LoadFile(Filename$)
              SetGadgetState(0,ImageID(1))
            EndIf
            
          Case 2
            rotimg()         
        EndSelect
    EndSelect   
  Until Quit = 1
  GdiplusShutdown(token)
EndIf
End
Edit : Modified version
Edit 2 : Modified again
Last edited by RASHAD on Thu Apr 01, 2021 5:35 am, edited 2 times in total.
Egypt my love
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Simple Rotate Image using GDI+ [Windows]

Post by Kwai chang caine »

Works very well here :wink:
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: Simple Rotate Image using GDI+ [Windows]

Post by RASHAD »

Thanks KCC
Previous post updated
Egypt my love
Post Reply