Page 2 of 2

Posted: Sun Aug 17, 2008 1:32 pm
by Wolf
@srod (or anyone)

Can we use this method (Gdi+) to read JPEG in PB too?

Posted: Sun Aug 17, 2008 2:13 pm
by srod
Sure.

Code: Select all

#GDIPLUS_OK = 0 

;-Structures. 
  Structure GdiplusStartupInput ;{ 
    GdiPlusVersion.l 
    *DebugEventCallback.DebugEventProc 
    SuppressBackgroundThread.l 
    SuppressExternalCodecs.l 
  EndStructure 

;-Imports. 
  Import "gdiplus.lib" 
    GdiplusStartup(token, *input.GdiplusStartupInput, output) 
    GdipCreateBitmapFromFile(filename.p-unicode, *bitmap) 
    GdipGetImageWidth(*image, *width) 
    GdipGetImageHeight(*image, *height) 
    GdipCreateFromHDC(hdc.l, *graphics) 
    GdipDrawImageRectI(*graphics, *image, x.l, y.l, width.l, height.l) 
    GdipDeleteGraphics(*graphics) 
    GdipDisposeImage(image) 
    GdiplusShutdown(token) 
  EndImport 

;Returns zero if an error or the PB image#. 
Procedure.l LoadJPG(filename$) 
  Protected result, width, height 
  Protected input.GdiplusStartupInput, token, image, imageID, hdc, graphics 
  ;First initialise gdi+. 
    input\GdiPlusVersion = 1 
    GdiplusStartup(@token, @input, #Null) 
  ;Was the initialisation successful? 
    If token 
      If GdipCreateBitmapFromFile(filename$, @image) = #GDIPLUS_OK 
        GdipGetImageWidth(image, @width) 
        GdipGetImageHeight(image, @height) 
        imageID = CreateImage(#PB_Any, Width, Height, 32) 
        If imageID 
          hdc = StartDrawing(ImageOutput(imageID)) 
          If hdc 
            GdipCreateFromHDC(hdc, @graphics) 
            If graphics 
              GdipDrawImageRectI(graphics, image, 0, 0, width, height) 
              GdipDeleteGraphics(graphics) 
              result = imageID      
            EndIf 
            StopDrawing()  
          EndIf 
        EndIf 
        GdipDisposeImage(image) 
      EndIf 
      ;Tidy up. 
        GdiplusShutdown(token) 
    EndIf 
  ProcedureReturn result 
EndProcedure 

;Test. 

imageNum = LoadJPG("test.jpg") 
The code to load the JPG is only a single line, the rest of the code is simply copying it to a PB image.

Posted: Sun Aug 17, 2008 11:35 pm
by Wolf
Thank you srod great like before :D

Posted: Sun Aug 17, 2008 11:50 pm
by srod
Here's a better way perhaps. This time the imageID is returned (not the image#).

You can get a PB image by simply drawing to a separate image etc.

Code: Select all

#GDIPLUS_OK = 0 
#PixelFormat32bppARGB = 2498570


;-Structures. 
  Structure GdiplusStartupInput ;{ 
    GdiPlusVersion.l 
    *DebugEventCallback.DebugEventProc 
    SuppressBackgroundThread.l 
    SuppressExternalCodecs.l 
  EndStructure 

;-Imports. 
  Import "gdiplus.lib" 
    GdiplusStartup(token, *input.GdiplusStartupInput, output) 
    GdipCreateBitmapFromFile(filename.p-unicode, *bitmap) 
    GdipCreateHBITMAPFromBitmap(*bitmap, *image, background)
    GdipDisposeImage(image) 
    GdiplusShutdown(token) 
  EndImport 

;Returns zero if an error or the PB image#. 
Procedure.l LoadJPG(filename$) 
  Protected result, width, height 
  Protected input.GdiplusStartupInput, token, image, imageID, hdc, graphics 
  ;First initialise gdi+. 
    input\GdiPlusVersion = 1 
    GdiplusStartup(@token, @input, #Null) 
  ;Was the initialisation successful? 
    If token 
      If GdipCreateBitmapFromFile(filename$, @image) = #GDIPLUS_OK 
        GdipCreateHBITMAPFromBitmap(image, @result, #PixelFormat32bppARGB)
        GdipDisposeImage(image) 
      EndIf 
      ;Tidy up. 
        GdiplusShutdown(token) 
    EndIf 
  ProcedureReturn result 
EndProcedure 

;Test. 

imageID = LoadJPG("test.jpg") 

Posted: Thu Sep 25, 2008 2:49 pm
by Fredi
Thank you srod for your nice work, but i have a small problem at your two last code, i want resize picture after load with GDI+ then use your first source for save image in JPG with compression, but between your load and save jpg i can't resize imageID (With PB command or GDI+ command if exist). can you help me to do this?

Posted: Thu Sep 25, 2008 4:28 pm
by srod
Hi,

I haven't time at the moment, but, if you want to allow for gdi+ refinements, then the routine will run along the following lines :
  • Use GdipCreateBitmapFromFile(filename$, @*image) to get your gdi+ image object.
  • create a normal 32-bit PB image of the new size that you are after.
  • select the image into a HDC by using StartDrawing() etc.
  • use GdipCreateFromHDC(HDC, @*graphics) to obtain a gdi+ graphics object containing the new bitmap.
  • (Optional) set *graphics smoothing mode with the GdipSetSmoothingMode() function. You will need to look up the appropriate values on the web.
  • (Optional) Set *graphics interpolation mode with GdipSetInterpolationMode().
  • Use the gdi+ function GdipDrawImageRectI(*graphics, *image, x.l, y.l, width.l, height.l) to draw the *image object into the graphics object with the relevant width and height values.
  • Use GdipCreateBitmapFromScan0() to convert the PB image to a gdi+ image object.
  • Use the code in this thread to save the new image as a jpeg with compression etc.
Don't forget to tidy up by deleting image objects, graphics objects and hdc's etc.

This can all be simplified (if you do not require to set the smoothing or interpolation modes) by simply using the code I posted above to load a jpeg and then using PB's ResizeImage() command before using the last two steps in the above list.

As I say if I had the time I'd hack up some code for you.

[4.20 Final] Unicode problem

Posted: Fri Sep 26, 2008 11:46 pm
by Fredi
As I say if I had the time I'd hack up some code for you.
Thanks for your info but i im newbie, so still waiting :D