Page 1 of 1

Convert Any Image to GIF Format [Windows]

Posted: Mon Jan 11, 2021 6:25 pm
by RASHAD
- Code by srod
- Added and Modified by RASHAD
- It can use GDI+ directly to load the image but not TGA
- I hate to see some questions not answered
Have fun

Code: Select all

UsePNGImageDecoder()
UseJPEGImageDecoder()
UseJPEG2000ImageDecoder()
UseTIFFImageDecoder()
UseTGAImageDecoder()

#GDIPLUS_OK = 0

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

Import "gdiplus.lib"
  GdiplusStartup(token, *input.GdiplusStartupInput, output)
  GdiplusShutdown(token)
  GdipCreateBitmapFromFile(filename.p-unicode, *bitmap)
  GdipCreateBitmapFromHBITMAP(hbm, hpal, *bitmap)
  GdipSaveImageToFile(image, filename.p-unicode, *clsidEncoder.CLSID, *encoderParams)
  GdipDisposeImage(image)
EndImport

pattern$ = "PNG, BMP, JPEG, TIFF|*.png;*.bmp;*.jpg;*.jpeg;*.tiff|PNG (*.png)|*.png|BMP (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg|TIFF (*.tif)|*.tif"
Filename$ = OpenFileRequester("Choose an image to convert to GIF:","",pattern$, 0)
If Filename$
  If LoadImage(0,Filename$) = 0
    MessageRequester("Error","Image not loaded",#MB_OK)
    End
  EndIf
EndIf

input.GdiplusStartupInput
input\GdiPlusVersion = 1
GdiplusStartup(@token, @input, #Null)
If token
  If GdipCreateBitmapFromHBITMAP(ImageID(0),0, @image) = #GDIPLUS_OK
    GdipSaveImageToFile(image, GetTemporaryDirectory()+"test.gif", ?clsid_giff, 0)
    GdipDisposeImage(image)
  EndIf
  If IsImage(0)
    FreeImage(0)
  EndIf
  GdiplusShutdown(token)
EndIf
End

DataSection
  clsid_giff:
  Data.l $557CF402
  Data.w $1A04, $11D3
  Data.b $9A, $73, $00, $00, $F8, $1E, $F3, $2E
EndDataSection

Re: Convert Any Image to GIF Format [Windows]

Posted: Tue Jan 12, 2021 8:49 am
by VB6_to_PBx
- I hate to see some questions not answered
Rashad ... thank you for this Code !

a few questions :

is it possible to set or change a .Gif picture quality value ?
is this Code saving a .Gif at its highest possible picture quality ?

and
DataSection
clsid_giff:
Data.l $557CF402
Data.w $1A04, $11D3
Data.b $9A, $73, $00, $00, $F8, $1E, $F3, $2E
EndDataSection
how and where are you getting the clsid values ?

Re: Convert Any Image to GIF Format [Windows]

Posted: Tue Jan 12, 2021 12:03 pm
by BarryG
Rashad, thanks, but it seems the GIF image with your code is dithered or something, because the quality is lower than the original code.

(a) Original test image -> https://i.imgur.com/ADfHBip.png
(b) Original code to gif -> https://i.imgur.com/5FeUsRg.png
(c) Your new code to gif -> https://i.imgur.com/t0C6Agl.png

See what I mean? Compare (b) and (c). Can your routine make it look like the (b) image above?

(BTW, I saved the results as PNGs simply out of habit, but the visuals are the same as PNG is lossless).

Re: Convert Any Image to GIF Format [Windows]

Posted: Tue Jan 12, 2021 12:27 pm
by Saki
With a GIF photo you go back to the computer stone age.
This is how it will look like.
That is normal.
Just have a look in Wikipedia how a GIF works.
I myself don't see any use for a mono frame gif because it has only disadvantages, sorry.

Re: Convert Any Image to GIF Format [Windows]

Posted: Tue Jan 12, 2021 12:31 pm
by BarryG
Saki, Rashad's modification of the original code leads to a dithered image. The only difference is that the same original image was converted from a file (b) or from RAM (c). The output should be the same, but the file version is clearly visually superior.

Re: Convert Any Image to GIF Format [Windows]

Posted: Tue Jan 12, 2021 12:52 pm
by Saki
Hmm, I can't say anything about that.
Gif is devil's work. :wink:
What would be needed would be the full support of PB.
But this is all very complex.
The hype after GIF is largely over again and it will probably disappear again completely, i think

Re: Convert Any Image to GIF Format [Windows]

Posted: Tue Jan 12, 2021 8:44 pm
by RASHAD
Hi VB6_to_PBx

Next are the gdiplus_GUID for different formats

Code: Select all

GUID_ImageEncoderBMP:
  Data.l $557cf400
  Data.w $1a04, $11d3
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e
GUID_ImageEncoderJPEG:
  Data.l $557cf401
  Data.w $1a04, $11d3 
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e
GUID_ImageEncoderGIF:
  Data.l $557cf402
  Data.w $1a04, $11d3
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e
GUID_ImageDecoderEMF:
  Data.l $557cf403
  Data.w $1a04, $11d3
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e
GUID_ImageDecoderWMF:
  Data.l $557cf404
  Data.w $1a04, $11d3
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e
GUID_ImageEncoderTIFF:
  Data.l $557cf405
  Data.w $1a04, $11d3
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e
GUID_ImageEncoderPNG:
  Data.l $557cf406
  Data.w $1a04, $11d3
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e

Re: Convert Any Image to GIF Format [Windows]

Posted: Wed Jan 13, 2021 6:46 am
by VB6_to_PBx
RASHAD wrote:Hi VB6_to_PBx

Next are the gdiplus_GUID for different formats

Code: Select all

GUID_ImageEncoderBMP:
  Data.l $557cf400
  Data.w $1a04, $11d3
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e
GUID_ImageEncoderJPEG:
  Data.l $557cf401
  Data.w $1a04, $11d3 
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e
GUID_ImageEncoderGIF:
  Data.l $557cf402
  Data.w $1a04, $11d3
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e
GUID_ImageDecoderEMF:
  Data.l $557cf403
  Data.w $1a04, $11d3
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e
GUID_ImageDecoderWMF:
  Data.l $557cf404
  Data.w $1a04, $11d3
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e
GUID_ImageEncoderTIFF:
  Data.l $557cf405
  Data.w $1a04, $11d3
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e
GUID_ImageEncoderPNG:
  Data.l $557cf406
  Data.w $1a04, $11d3
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e
thank you Rashad , you are always helpful !
i wish we could Vote or give Stars in this Forum .