Load Any Image Using GDI+ [Windows]

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

Load Any Image Using GDI+ [Windows]

Post by RASHAD »

Format Supported :
*.bmp;*.rle; *.ico; *.cur; *.gif; *.jpg; *.jpeg; *.wmf; *.emf; *.png;*.tif;*.tiff;*.tga

And MultiPages Tif

To do:
Save to ....

Code: Select all


UseTGAImageDecoder()

#SmoothingModeInvalid     = -1
#SmoothingModeDefault     = 0
#SmoothingModeHighSpeed   = 1
#SmoothingModeHighQuality = 2
#SmoothingModeNone        = 3
#SmoothingModeAntiAlias   = 4

;- CodecInfo\MimeType
#Jpeg_Encoder = "image/jpeg"
#Gif_Encoder = "image/gif"
#Bmp_Encoder = "image/bmp"
#Png_Encoder = "image/png"
#Tif_Encoder = "image/tiff"

Global *token, *Image, *dimensionID.GUID, dimensionsCount = 0,encoderCLSID.GUID
Global  Num,Quit,Count,Result,Width.i,Height.i,FileName$
  
Structure GdiplusStartupInput
     GdiPlusVersion.i
     *DebugEventCallback.DebugEventProc
     SuppressBackgroundThread.i
     SuppressExternalCodecs.i
EndStructure 

Procedure StringToBStr (string$) ; By Zapman Inspired by Fr34k
  Protected Unicode$ = Space(Len(String$)* 2 + 2)
  Protected bstr_string.l
  PokeS(@Unicode$, String$, -1, #PB_Unicode)
  bstr_string = SysAllocString_(@Unicode$)
  ProcedureReturn bstr_string
EndProcedure

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

Macro Gdiplus_Del(Token)
     If Token
          CallFunction(0,"GdiplusShutdown",token)
          Token = 0
     EndIf
EndMacro

Procedure.f Max(n.f,m.f)
   If n >= m
      Max.f = n
   Else
      Max.f = m
   EndIf
   ProcedureReturn Max
EndProcedure

; Procedure.l Max(n1.l,n2.l)
;   !MOV Eax,dword[p.v_n1]
;   !MOV Ecx,dword[p.v_n2]
;   !CMP Ecx,Eax
;   !cmovg Eax,Ecx
;   ProcedureReturn
; EndProcedure

Procedure.l ARGB(rgb.l, a.b = 255)
  !MOV eax, [p.v_rgb]
  !BSWAP eax 
  !SHR eax, 8 
  !MOV [p.v_rgb], eax 
  !MOV eax, [p.v_a]
  !MOV dword [p.v_rgb+3], eax
  ProcedureReturn rgb
EndProcedure

Procedure.i Gdiplus_New(version = 1, *hEventCB = #Null, Codecs = #False, bgThread = #False)
OpenLibrary(0, "gdiplus.dll")
Protected *token, input.GdiplusStartupInput
With input
     \GdiPlusVersion = version
     \DebugEventCallback = *hEventCB
     \SuppressExternalCodecs = Codecs
     \SuppressBackgroundThread = bgThread
EndWith
CallFunction(0, "GdiplusStartup", @*token, @input, #Null)
ProcedureReturn *token
EndProcedure

Procedure Draw_Image()
   CallFunction(0,"GdipImageSelectActiveFrame",*image, ?FrameDimensionPage, Num)
   CallFunction(0,"GdipGetImageWidth",*image, @Width.i)
   CallFunction(0,"GdipGetImageHeight",*image, @Height.i)
   CreateImage(0,Width,Height)
          dc = StartDrawing(ImageOutput(0))            
             If CallFunction(0,"GdipCreateFromHDC",dc, @*Localgfx) = 0 
                CallFunction(0,"GdipGraphicsClear",*Localgfx,ARGB(GetSysColor_(#COLOR_BTNFACE), 255))                          
                CallFunction(0,"GdipDrawImageRectI",*Localgfx, *image, 0,0, width, height)
                ;CallFunction(0,"GdipDrawImage",*Localgfx, *image, 0,0)
                ;CallFunction(0,"GdipdisposeImage",*image)
             EndIf
                StopDrawing()
  If Width > GadgetWidth(0) Or Height > GadgetHeight(0)
      Scale.f = 588/Max(Width,Height)
      ResizeImage(bkgimage,Width*Scale,Height*scale)
  EndIf
   SetGadgetAttribute(1,#PB_Button_Image,ImageID(0))
   RedrawWindow_(WindowID(0), 0, 0, 1)
 EndProcedure
 
Procedure GetEncoderClsid(format$, *Clsid.CLSID)
Protected number
Protected Size 
Protected *pImageCodecInfo.ImageCodecInfo 
Protected i, *memory

CallFunction(0,"GdipGetImageEncodersSize",@number, @Size)
If Size = 0
     ProcedureReturn -1
EndIf

*memory = AllocateMemory(Size)
If *memory = #Null
     ProcedureReturn -1
EndIf

*pImageCodecInfo = *memory
CallFunction(0,"GdipGetImageEncoders",number, Size, *pImageCodecInfo)

For i = 1 To number
  If format$ = PeekS(*pImageCodecInfo\MimeType, -1, #PB_Unicode)
          CopyMemory(*pImageCodecInfo\clsid, *Clsid, SizeOf(CLSID))
          FreeMemory(*memory)
          ProcedureReturn i 
     EndIf
     *pImageCodecInfo + SizeOf(ImageCodecInfo)
Next
FreeMemory(*memory)
ProcedureReturn -1 
EndProcedure


If OpenWindow(0, 0, 0, 800, 650, "GdiPlus 1.1 - View Image",#PB_Window_MinimizeGadget| #PB_Window_MaximizeGadget| #PB_Window_SizeGadget| #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
		ContainerGadget(0, 10, 10, 780, 600,#PB_Container_Flat)
		    ButtonImageGadget(1, -4, -4, 788, 608, 0)
		    SetWindowTheme_(GadgetID(1), @null.w, @null.w)
    CloseGadgetList()
    DisableGadget(0,1)
    ContainerGadget(10, 10, 615, 336, 30,#PB_Container_Flat)
        ButtonGadget(2,3,3,80,22,"Load Image")
        TextGadget(3,95,5,18,18,"M",#SS_CENTERIMAGE | #SS_CENTER |#WS_BORDER)
        SetGadgetColor(3,#PB_Gadget_BackColor,#Green)
        ButtonGadget(4,120,4,40,20,"<|")
        ButtonGadget(5,166,4,40,20,"|>")
        ButtonGadget(6,250,3,80,22,"Save As")
    CloseGadgetList()
    HideGadget(3,1)
Repeat        
  Select WaitWindowEvent() 
              Case #PB_Event_CloseWindow
                      Quit = 1
                      
                Case #PB_Event_Gadget
                    Select EventGadget()
                         Case 2
                             SetGadgetAttribute(1,#PB_Button_Image,0)
                             If IsImage(0)
                                 FreeImage(0)
                             EndIf
                             FileName$ = OpenFileRequester("SELECT IMAGE","","All supported formats|*.bmp;*.rle; *.ico; *.cur; *.gif; *.jpg; *.jpeg; *.wmf; *.emf; *.png;*.tif;*.tiff;*.tga|TGA image (*.tga)|*.tga|TIF image (*.tif)|*.tif|TIFF image (*.tiff)|*.tiff|PNG image (*.png)|*.png|BMP image (*.bmp)|*.bmp|RLE image(*.rle)|*.rle|Icon file (*.ico)|*.ico|Cursor file (*.cur)|*.cur|JPEG image (*.jpg;*.jpeg)|*.jpg;*.jpeg|GIF image (*.gif)|*.gif|Windows Metafile (*.wmf)|*.wmf|Enhanced Metafile (*.emf)|*.emf",0)
                              If GetExtensionPart(FileName$) = "tga"
                                  	  LoadImage(0,FileName$)
                                  	  SetGadgetAttribute(1,#PB_Button_Image,ImageID(0))
                              ElseIf GetExtensionPart(FileName$) = "cur"
                                     hCursor=LoadCursorFromFile_(FileName$) 
                                     SetGadgetAttribute(1,#PB_Button_Image,hCursor)
                              Else
                                   *token = Gdiplus_New()
                                    If *token
                                         If CallFunction(0,"GdipLoadImageFromFile",StringToBStr(FileName$), @*Image) = 0   ;"e:\MPage_4.tif"
                                              If CallFunction(0,"GdipImageGetFrameDimensionsCount",*image, @dimensionsCount) = 0
                                                   If dimensionsCount
                                                        *dimensionID = AllocateMemory(SizeOf(GUID)*dimensionsCount)
                                                        If *dimensionID
                                                             CallFunction(0,"GdipImageGetFrameCount",*image, ?FrameDimensionPage, @count)
                                                             If Count > 1
                                                                  HideGadget(3,0)
                                                             Else
                                                                 HideGadget(3,1)
                                                             EndIf
                                                             FreeMemory(*dimensionID)
                                                        EndIf
                                                   EndIf
                                              EndIf
                                                 Draw_Image()               
                                         Else
                                              MessageRequester("Error", "Can not load image", #MB_OK	|#MB_ICONERROR)
                                         EndIf
                                   EndIf
                              EndIf 
                              
                          Case 4
                                    Num - 1
                                    If Num < 0
                                        Num = Count - 1
                                    EndIf
                                    Draw_Image()
                                
                          Case 5
                                    Num + 1
                                    If Num > Count - 1
                                       Num = 0
                                    EndIf
                                    Draw_Image()
                                    
                          Case 6
                                    If IsImage(0)
                                      sFile$ = SaveFileRequester("Please choose file to save",""," All supported formats|*.bmp;*.gif; *.jpg; *.jpeg; *.wmf; *.emf; *.png; *.tif;*.tiff;*.tga| 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| GIF image (*.gif)|*.gif| Windows Metafile (*.wmf)|*.wmf| Enhanced Metafile (*.emf)|*.emf",0)
                                    EndIf
                                    If GetExtensionPart(sFile$) = "bmp"
                                      GetEncoderClsid(#Bmp_Encoder, @encoderCLSID)
                                      CallFunction(0,"GdipSaveImageToFile",*Image, StringToBStr(sFile$), @encoderCLSID, 0) 
                                    ElseIf GetExtensionPart(sFile$) = "jpg"
                                      GetEncoderClsid(#Jpeg_Encoder, @encoderCLSID)
                                      CallFunction(0,"GdipSaveImageToFile",*Image, StringToBStr(sFile$), @encoderCLSID, 0)
                                    ElseIf GetExtensionPart(sFile$) = "png"
                                      GetEncoderClsid(#Png_Encoder, @encoderCLSID)
                                      CallFunction(0,"GdipSaveImageToFile",*Image, StringToBStr(sFile$), @encoderCLSID, 0)
                                    ElseIf GetExtensionPart(sFile$) = "tif"
                                      GetEncoderClsid(#Tif_Encoder, @encoderCLSID)
                                      CallFunction(0,"GdipSaveImageToFile",*Image, StringToBStr(sFile$), @encoderCLSID, 0)
                                    ElseIf GetExtensionPart(sFile$) = "gif"
                                      GetEncoderClsid(#Gif_Encoder, @encoderCLSID)
                                      CallFunction(0,"GdipSaveImageToFile",*Image, StringToBStr(sFile$), @encoderCLSID, 0)
                                    ElseIf GetExtensionPart(sFile$) = "tga"
                                        ResizeImage(0,32,32)
																				StartDrawing(ImageOutput(0))
																				  If CreateFile(0,sFile$)
																				    Restore Header
																				    For tmp=1 To 12
																				      Read Header
																				      WriteByte(0,Header)
																				    Next    
																				    WriteWord(0,ImageWidth(0))
																				    WriteWord(0,ImageHeight(0))    
																				    WriteByte(0,24)
																				    WriteByte(0,32)
																				    For y=0 To ImageHeight(0)-1
																				      For x=0 To ImageWidth(0)-1
																				        color=Point(x,y)        
																				        WriteByte(0,Blue(color))
																				        WriteByte(0,Green(color))
																				        WriteByte(0,Red(color))
																				      Next 
																				    Next
																				    CloseFile(0)
																				  EndIf
																				StopDrawing() 
                                    ElseIf GetExtensionPart(sFile$) = "emf"
																			emf_hDC = CreateEnhMetaFile_(0, @sFile$, 0, 0)
																			If emf_hDC
																			    img_hDC = CreateCompatibleDC_(emf_hDC)
																			    oldImage = SelectObject_(img_hDC, ImageID(0))
																			    BitBlt_(emf_hDC, 0, 0, Width,Height, img_hDC, 0, 0, #SRCCOPY)
																			    SelectObject_(img_hDC, oldImage)
																			    DeleteDC_(img_hDC)
																			    hEMF = CloseEnhMetaFile_(emf_hDC)
																			    DeleteEnhMetaFile_(hEMF)
																			EndIf
										              	EndIf                                      
                  EndSelect 
    
                Case #PB_Event_SizeWindow
                        LockWindowUpdate_(WindowID(0))
                        ResizeGadget(0,#PB_Ignore, #PB_Ignore,WindowWidth(0,#PB_Window_InnerCoordinate)-20, WindowHeight(0,#PB_Window_InnerCoordinate)-50)
                        ResizeGadget(1,#PB_Ignore, #PB_Ignore,GadgetWidth(0)+4,GadgetHeight(0)+4)
                        ResizeGadget(10,#PB_Ignore,WindowHeight(0,#PB_Window_InnerCoordinate)-35,#PB_Ignore,#PB_Ignore)
                        LockWindowUpdate_(0)
                        UpdateWindow_(WindowID(0))
  EndSelect         
Until Quit = 1
EndIf
End

DataSection
  IconHeader_32:
    Data.l 0,0,1,0,0,0,0,16,0,0,0,0
  Header:
    Data.l 0,0,2,0,0,0,0,16,0,0,0,0
  FrameDimensionPage:
    Data.l  $7462DC86
    Data.u  $6180, $4C7E
    Data.b  $8E, $3F, $EE, $73, $33, $A7, $A4, $83
EndDataSection  
   
Edit :Ver 2.0
Egypt my love
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Load Any Image Using GDI+ [Windows]

Post by RASHAD »

Previous code updated
- More format to load
- Can be saved to different format
And More

Thanks to :
NM,Danilo,Denis,flype and Erix14

Have fun
Egypt my love
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Load Any Image Using GDI+ [Windows]

Post by Kwai chang caine »

You a really productive this time :shock:
Perhaps it's rain in your country :lol:
Thanks a lot RASHAD for sharing !! 8)
ImageThe happiness is a road...
Not a destination
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Load Any Image Using GDI+ [Windows]

Post by Kwai chang caine »

I have tested the icon creator works great !!
Very useful for me ..
Thanks RASHAD for sharing 8)
ImageThe happiness is a road...
Not a destination
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Re: Load Any Image Using GDI+ [Windows]

Post by Flype »

Well done Rashad
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
VisualJump3D
User
User
Posts: 75
Joined: Thu Jun 23, 2011 8:32 pm
Location: italy
Contact:

Re: Load Any Image Using GDI+ [Windows]

Post by VisualJump3D »

Hi,

does not save in .wmf

Thank !
sphinx
Enthusiast
Enthusiast
Posts: 120
Joined: Thu Oct 26, 2006 6:34 pm
Contact:

Re: Load Any Image Using GDI+ [Windows]

Post by sphinx »

Thanks Rashad

Did you get back to Egypt? :)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Load Any Image Using GDI+ [Windows]

Post by RASHAD »

@sphinx
Hi fellow
No not yet,I am glad you are OK
Take care


@VisualJump3D
Hi
Added Save to WMF and maybe more I do not remember what I did after the first post :)
Hope it is OK

Code: Select all

UseTGAImageDecoder()

#SmoothingModeInvalid     = -1
#SmoothingModeDefault     = 0
#SmoothingModeHighSpeed   = 1
#SmoothingModeHighQuality = 2
#SmoothingModeNone        = 3
#SmoothingModeAntiAlias   = 4

;- CodecInfo\MimeType
#Jpeg_Encoder = "image/jpeg"
#Gif_Encoder = "image/gif"
#Bmp_Encoder = "image/bmp"
#Png_Encoder = "image/png"
#Tif_Encoder = "image/tiff"

Global *token, *Image, *dimensionID.GUID, dimensionsCount = 0,encoderCLSID.GUID
Global  Num,Quit,Count,Result,Width.i,Height.i,FileName$
  
Structure GdiplusStartupInput
     GdiPlusVersion.i
     *DebugEventCallback.DebugEventProc
     SuppressBackgroundThread.i
     SuppressExternalCodecs.i
EndStructure 

Procedure StringToBStr (string$) ; By Zapman Inspired by Fr34k
  Protected Unicode$ = Space(Len(String$)* 2 + 2)
  Protected bstr_string.l
  PokeS(@Unicode$, String$, -1, #PB_Unicode)
  bstr_string = SysAllocString_(@Unicode$)
  ProcedureReturn bstr_string
EndProcedure

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

Macro Gdiplus_Del(Token)
     If Token
          CallFunction(0,"GdiplusShutdown",token)
          Token = 0
     EndIf
EndMacro

Procedure.f Max(n.f,m.f)
   If n >= m
      Max.f = n
   Else
      Max.f = m
   EndIf
   ProcedureReturn Max
EndProcedure

; Procedure.l Max(n1.l,n2.l)
;   !MOV Eax,dword[p.v_n1]
;   !MOV Ecx,dword[p.v_n2]
;   !CMP Ecx,Eax
;   !cmovg Eax,Ecx
;   ProcedureReturn
; EndProcedure

Procedure.l ARGB(rgb.l, a.b = 255)
  !MOV eax, [p.v_rgb]
  !BSWAP eax 
  !SHR eax, 8 
  !MOV [p.v_rgb], eax 
  !MOV eax, [p.v_a]
  !MOV dword [p.v_rgb+3], eax
  ProcedureReturn rgb
EndProcedure

Procedure.i Gdiplus_New(version = 1, *hEventCB = #Null, Codecs = #False, bgThread = #False)
OpenLibrary(0, "gdiplus.dll")
Protected *token, input.GdiplusStartupInput
With input
     \GdiPlusVersion = version
     \DebugEventCallback = *hEventCB
     \SuppressExternalCodecs = Codecs
     \SuppressBackgroundThread = bgThread
EndWith
CallFunction(0, "GdiplusStartup", @*token, @input, #Null)
ProcedureReturn *token
EndProcedure

Procedure Draw_Image()
   CallFunction(0,"GdipImageSelectActiveFrame",*image, ?FrameDimensionPage, Num)
   CallFunction(0,"GdipGetImageWidth",*image, @Width.i)
   CallFunction(0,"GdipGetImageHeight",*image, @Height.i)
   CreateImage(0,Width,Height)
          dc = StartDrawing(ImageOutput(0))            
             If CallFunction(0,"GdipCreateFromHDC",dc, @*Localgfx) = 0 
                CallFunction(0,"GdipGraphicsClear",*Localgfx,ARGB(GetSysColor_(#COLOR_BTNFACE), 255))                          
                CallFunction(0,"GdipDrawImageRectI",*Localgfx, *image, 0,0, width, height)
                ;CallFunction(0,"GdipDrawImage",*Localgfx, *image, 0,0)
                ;CallFunction(0,"GdipdisposeImage",*image)
             EndIf
                StopDrawing()
  If Width > GadgetWidth(0) Or Height > GadgetHeight(0)
      Scale.f = 588/Max(Width,Height)
      ResizeImage(bkgimage,Width*Scale,Height*scale)
  EndIf
   SetGadgetAttribute(1,#PB_Button_Image,ImageID(0))
   RedrawWindow_(WindowID(0), 0, 0, 1)
 EndProcedure
 
Procedure GetEncoderClsid(format$, *Clsid.CLSID)
Protected number
Protected Size 
Protected *pImageCodecInfo.ImageCodecInfo 
Protected i, *memory

CallFunction(0,"GdipGetImageEncodersSize",@number, @Size)
If Size = 0
     ProcedureReturn -1
EndIf

*memory = AllocateMemory(Size)
If *memory = #Null
     ProcedureReturn -1
EndIf

*pImageCodecInfo = *memory
CallFunction(0,"GdipGetImageEncoders",number, Size, *pImageCodecInfo)

For i = 1 To number
  If format$ = PeekS(*pImageCodecInfo\MimeType, -1, #PB_Unicode)
          CopyMemory(*pImageCodecInfo\clsid, *Clsid, SizeOf(CLSID))
          FreeMemory(*memory)
          ProcedureReturn i 
     EndIf
     *pImageCodecInfo + SizeOf(ImageCodecInfo)
Next
FreeMemory(*memory)
ProcedureReturn -1 
EndProcedure


If OpenWindow(0, 0, 0, 800, 650, "GdiPlus 1.1 - View Image",#PB_Window_MinimizeGadget| #PB_Window_MaximizeGadget| #PB_Window_SizeGadget| #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
		ContainerGadget(0, 10, 10, 780, 600,#PB_Container_Flat)
		    ButtonImageGadget(1, -4, -4, 788, 608, 0)
		    SetWindowTheme_(GadgetID(1), @null.w, @null.w)
    CloseGadgetList()
    DisableGadget(0,1)
    ContainerGadget(10, 10, 615, 336, 30,#PB_Container_Flat)
        ButtonGadget(2,3,3,80,22,"Load Image")
        TextGadget(3,95,5,18,18,"M",#SS_CENTERIMAGE | #SS_CENTER |#WS_BORDER)
        SetGadgetColor(3,#PB_Gadget_BackColor,#Green)
        ButtonGadget(4,120,4,40,20,"<|")
        ButtonGadget(5,166,4,40,20,"|>")
        ButtonGadget(6,250,3,80,22,"Save As")
    CloseGadgetList()
    HideGadget(3,1)
Repeat        
  Select WaitWindowEvent() 
              Case #PB_Event_CloseWindow
                      Quit = 1
                      
                Case #PB_Event_Gadget
                    Select EventGadget()
                         Case 2
                             SetGadgetAttribute(1,#PB_Button_Image,0)
                             If IsImage(0)
                                 FreeImage(0)
                             EndIf
                             FileName$ = OpenFileRequester("SELECT IMAGE","","All supported formats|*.bmp;*.rle; *.ico; *.cur; *.gif; *.jpg; *.jpeg; *.wmf; *.emf; *.png;*.tif;*.tiff;*.tga|TGA image (*.tga)|*.tga|TIF image (*.tif)|*.tif|TIFF image (*.tiff)|*.tiff|PNG image (*.png)|*.png|BMP image (*.bmp)|*.bmp|RLE image(*.rle)|*.rle|Icon file (*.ico)|*.ico|Cursor file (*.cur)|*.cur|JPEG image (*.jpg;*.jpeg)|*.jpg;*.jpeg|GIF image (*.gif)|*.gif|Windows Metafile (*.wmf)|*.wmf|Enhanced Metafile (*.emf)|*.emf",0)
                              If GetExtensionPart(FileName$) = "tga"
                                  	  LoadImage(0,FileName$)
                                  	  SetGadgetAttribute(1,#PB_Button_Image,ImageID(0))
                              ElseIf GetExtensionPart(FileName$) = "cur"
                                     hCursor=LoadCursorFromFile_(FileName$) 
                                     SetGadgetAttribute(1,#PB_Button_Image,hCursor)
                              Else
                                   *token = Gdiplus_New()
                                    If *token
                                         If CallFunction(0,"GdipLoadImageFromFile",StringToBStr(FileName$), @*Image) = 0   ;"e:\MPage_4.tif"
                                              If CallFunction(0,"GdipImageGetFrameDimensionsCount",*image, @dimensionsCount) = 0
                                                   If dimensionsCount
                                                        *dimensionID = AllocateMemory(SizeOf(GUID)*dimensionsCount)
                                                        If *dimensionID
                                                             CallFunction(0,"GdipImageGetFrameCount",*image, ?FrameDimensionPage, @count)
                                                             If Count > 1
                                                                  HideGadget(3,0)
                                                             Else
                                                                 HideGadget(3,1)
                                                             EndIf
                                                             FreeMemory(*dimensionID)
                                                        EndIf
                                                   EndIf
                                              EndIf
                                                 Draw_Image()               
                                         Else
                                              MessageRequester("Error", "Can not load image", #MB_OK	|#MB_ICONERROR)
                                         EndIf
                                   EndIf
                              EndIf 
                              
                          Case 4
                                    Num - 1
                                    If Num < 0
                                        Num = Count - 1
                                    EndIf
                                    Draw_Image()
                                
                          Case 5
                                    Num + 1
                                    If Num > Count - 1
                                       Num = 0
                                    EndIf
                                    Draw_Image()
                                    
                          Case 6
                                    If IsImage(0)
                                      sFile$ = SaveFileRequester("Please choose file to save",""," All supported formats|*.bmp;*.gif; *.jpg; *.jpeg; *.wmf; *.emf; *.png; *.tif;*.tiff;*.tga| 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| GIF image (*.gif)|*.gif| Windows Metafile (*.wmf)|*.wmf| Enhanced Metafile (*.emf)|*.emf",0)
                                    EndIf
                                    If GetExtensionPart(sFile$) = "bmp"
                                          GetEncoderClsid(#Bmp_Encoder, @encoderCLSID)
                                          CallFunction(0,"GdipSaveImageToFile",*Image, StringToBStr(sFile$), @encoderCLSID, 0) 
                                        ElseIf GetExtensionPart(sFile$) = "jpg"
                                          GetEncoderClsid(#Jpeg_Encoder, @encoderCLSID)
                                          CallFunction(0,"GdipSaveImageToFile",*Image, StringToBStr(sFile$), @encoderCLSID, 0)
                                        ElseIf GetExtensionPart(sFile$) = "png"
                                          GetEncoderClsid(#Png_Encoder, @encoderCLSID)
                                          CallFunction(0,"GdipSaveImageToFile",*Image, StringToBStr(sFile$), @encoderCLSID, 0)
                                        ElseIf GetExtensionPart(sFile$) = "tif"
                                          GetEncoderClsid(#Tif_Encoder, @encoderCLSID)
                                          CallFunction(0,"GdipSaveImageToFile",*Image, StringToBStr(sFile$), @encoderCLSID, 0)
                                        ElseIf GetExtensionPart(sFile$) = "gif"
                                          GetEncoderClsid(#Gif_Encoder, @encoderCLSID)
                                          CallFunction(0,"GdipSaveImageToFile",*Image, StringToBStr(sFile$), @encoderCLSID, 0)
                                        ElseIf GetExtensionPart(sFile$) = "tga"
                                            ResizeImage(0,32,32)
    																				StartDrawing(ImageOutput(0))
    																				  If CreateFile(0,sFile$)
    																				    Restore Header
    																				    For tmp=1 To 12
    																				      Read Header
    																				      WriteByte(0,Header)
    																				    Next    
    																				    WriteWord(0,ImageWidth(0))
    																				    WriteWord(0,ImageHeight(0))    
    																				    WriteByte(0,24)
    																				    WriteByte(0,32)
    																				    For y=0 To ImageHeight(0)-1
    																				      For x=0 To ImageWidth(0)-1
    																				        color=Point(x,y)        
    																				        WriteByte(0,Blue(color))
    																				        WriteByte(0,Green(color))
    																				        WriteByte(0,Red(color))
    																				      Next 
    																				    Next
    																				    CloseFile(0)
    																				  EndIf
    																				StopDrawing() 
                                        ElseIf GetExtensionPart(sFile$) = "emf"
    																			emf_hDC = CreateEnhMetaFile_(0, @sFile$, 0, 0)
    																			If emf_hDC
    																			    img_hDC = CreateCompatibleDC_(emf_hDC)
    																			    oldImage = SelectObject_(img_hDC, ImageID(0))
    																			    BitBlt_(emf_hDC, 0, 0, Width,Height, img_hDC, 0, 0, #SRCCOPY)
    																			    SelectObject_(img_hDC, oldImage)
    																			    DeleteDC_(img_hDC)
    																			    hEMF = CloseEnhMetaFile_(emf_hDC)
    																			    DeleteEnhMetaFile_(hEMF)
    																			EndIf
    																	  ElseIf GetExtensionPart(sFile$) = "wmf"
    																	    tmpEMF$ =  GetHomeDirectory()+"tmp_EMF.emf"
    																	    emf_hDC = CreateEnhMetaFile_(0, @tmpEMF$, 0, 0)
    																			If emf_hDC
    																			    img_hDC = CreateCompatibleDC_(emf_hDC)
    																			    oldImage = SelectObject_(img_hDC, ImageID(0))
    																			    BitBlt_(emf_hDC, 0, 0, Width,Height, img_hDC, 0, 0, #SRCCOPY)
    																			    SelectObject_(img_hDC, oldImage)
    																			    DeleteDC_(img_hDC)
    																			    hEMF = CloseEnhMetaFile_(emf_hDC)
    																			    DeleteEnhMetaFile_(hEMF)
    																			    ;**********************
    																			    hDc = GetDC_(GetDesktopWindow_());
                                              hDcComp = CreateCompatibleDC_(hDc);
                                              ReleaseDC_(GetDesktopWindow_(), hDc);
                                              SetMapMode_(hDcComp,#MM_ANISOTROPIC);
                                              hEmf = GetEnhMetaFile_(@tmpEMF$) 
                                              uSize = GetWinMetaFileBits_(hEmf, 0, #Null, #MM_ANISOTROPIC, hDcComp);
                                              *pBuffer = GlobalAlloc_(#GPTR, uSize);
                                              GetWinMetaFileBits_(hEmf, uSize, *pBuffer, #MM_ANISOTROPIC, hDcComp);
                                              hWmf = SetMetaFileBitsEx_(uSize, *pBuffer)
                                              PlayMetaFile_(hDcComp, hWmf);
                                              hWmfNew = CopyMetaFile_(hWmf, sFile$) 
                                              DeleteMetaFile_(hWmfNew);
                                              DeleteMetaFile_(hWmf);
                                              DeleteEnhMetaFile_(hEmf);
                                              GlobalFree_(*pBuffer);
                                              DeleteDC_(hDcComp);
                                              DeleteFile(tmpEMF$)
    																			EndIf
										              EndIf                                      
                  EndSelect 
    
                Case #PB_Event_SizeWindow
                        LockWindowUpdate_(WindowID(0))
                        ResizeGadget(0,#PB_Ignore, #PB_Ignore,WindowWidth(0,#PB_Window_InnerCoordinate)-20, WindowHeight(0,#PB_Window_InnerCoordinate)-50)
                        ResizeGadget(1,#PB_Ignore, #PB_Ignore,GadgetWidth(0)+4,GadgetHeight(0)+4)
                        ResizeGadget(10,#PB_Ignore,WindowHeight(0,#PB_Window_InnerCoordinate)-35,#PB_Ignore,#PB_Ignore)
                        LockWindowUpdate_(0)
                        UpdateWindow_(WindowID(0))
  EndSelect         
Until Quit = 1
EndIf
End

DataSection
  IconHeader_32:
    Data.l 0,0,1,0,0,0,0,16,0,0,0,0
  Header:
    Data.l 0,0,2,0,0,0,0,16,0,0,0,0
  FrameDimensionPage:
    Data.l  $7462DC86
    Data.u  $6180, $4C7E
    Data.b  $8E, $3F, $EE, $73, $33, $A7, $A4, $83
EndDataSection
 
Egypt my love
User avatar
VisualJump3D
User
User
Posts: 75
Joined: Thu Jun 23, 2011 8:32 pm
Location: italy
Contact:

Re: Load Any Image Using GDI+ [Windows]

Post by VisualJump3D »

when I get home I try it, Thanks!
User avatar
VisualJump3D
User
User
Posts: 75
Joined: Thu Jun 23, 2011 8:32 pm
Location: italy
Contact:

Re: Load Any Image Using GDI+ [Windows]

Post by VisualJump3D »

Thank ! All OK !
Post Reply