@srod (or anyone)
Can we use this method (Gdi+) to read JPEG in PB too?
Convert BMP 2 JPEG without UseJPEGImageEncoder
Sure.
The code to load the JPG is only a single line, the rest of the code is simply copying it to a PB image.
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")
I may look like a mule, but I'm not a complete ass.
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.
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")
I may look like a mule, but I'm not a complete ass.
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?
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 :
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.
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.
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.
I may look like a mule, but I'm not a complete ass.
[4.20 Final] Unicode problem
Thanks for your info but i im newbie, so still waitingAs I say if I had the time I'd hack up some code for you.
