Help with GDI+

Just starting out? Need help? Post your questions and find answers here.
Beach
Enthusiast
Enthusiast
Posts: 677
Joined: Mon Feb 02, 2004 3:16 am
Location: Beyond the sun...

Help with GDI+

Post by Beach »

I am able to extract pages from a multipage tiff file using the GDI+ example pb code (posted by Andreas). The problem is that the extracted tiff image has a *huge* black border on the right and bottom of the extracted image. This border size changes for every file I try. Can anyone help with this issue?

Here is the code:

Code: Select all

Global gdiplusToken.l,ImageObject.l,ImageWidth.l,ImageHeight.l,GraphicObject.l,IsInit.l,but.l,myimg.l
#GDIPlusDll = 0

Structure GdiplusStartupInput
  GdiplusVersion.l
  DebugEventCallback.l
  SuppressBackgroundThread.l
  SuppressExternalCodecs.l
EndStructure

Procedure.l gdipLoadImage(Image.s)
  Protected RetVal.l,Bitmap.l
  RetVal = 0
  FileWSize = (Len(Image)*2)+2
  *FileW = CallFunction(#GDIPlusDll,"GdipAlloc",FileWSize)
  MultiByteToWideChar_(0, 0, Image, -1, *FileW, FileWSize)
  CallFunction(#GDIPlusDll,"GdipCreateBitmapFromFile",*FileW,@Bitmap)
  CallFunction(#GDIPlusDll,"GdipFree",*FileW)
  If Bitmap
    RetVal = Bitmap
  EndIf
  ProcedureReturn RetVal
EndProcedure

Procedure.l InitGDIPlus()
  If OpenLibrary(#GDIPlusDll,"GDIPlus.DLL")
    gdpsi.GdiplusStartupInput
    gdpsi\GdiplusVersion = 1
    gdpsi\DebugEventCallback = 0
    gdpsi\SuppressBackgroundThread = 0
    gdpsi\SuppressExternalCodecs = 0
    CallFunction(#GDIPlusDll,"GdiplusStartup",@gdiplusToken,@gdpsi,0)
    RetVal = gdiplusToken
  Else
    RetVal = 0
  EndIf
  ProcedureReturn RetVal
EndProcedure

Procedure ExecuteGDIPlus(file.s)
  gdiplusToken = InitGDIPlus()
  If gdiplusToken
    ImageObject  = gdipLoadImage(file)
    CallFunction(#GDIPlusDll,"GdipImageSelectActiveFrame",ImageObject,?MultiPageGUID,0) ;just looking at the first page
    CallFunction(#GDIPlusDll,"GdipGetImageWidth",ImageObject,@ImageWidth)
    CallFunction(#GDIPlusDll,"GdipGetImageHeight",ImageObject,@ImageHeight)
    CallFunction(#GDIPlusDll,"GdipCreateFromHWND",WindowID(),@GraphicObject) 
    IsInit = 1
    ;RepaintImage()
  EndIf
EndProcedure

Procedure DisplayTiff(Bitmap.l)
  RetVal = 0
  If CreateImage(0,ImageWidth,ImageHeight)
    IDC = StartDrawing(ImageOutput())
    If CallFunction(#GDIPlusDll,"GdipCreateFromHDC",IDC,@GraphicObject1) = 0
      If CallFunction(#GDIPlusDll,"GdipDrawImageI",GraphicObject1,Bitmap,0,0) = 0
        If CallFunction(#GDIPlusDll,"GdipDeleteGraphics",GraphicObject1) = 0
          StopDrawing()
          ResizeImage(0,380,455)
          ;ResizeImage(0,ImageWidth/5,ImageHeight/5)
          SetGadgetState(myimg,UseImage(0))

          RetVal = 1
          FreeImage(0)
          
        EndIf
      EndIf
    EndIf
  EndIf
  ProcedureReturn RetVal
EndProcedure

If OpenWindow(0,0,0,400,500,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"View Tiff images with GDIPlus") And CreateGadgetList(WindowID(0)) 
  img = CreateImage(#PB_Any,50,50) 
  myimg = ImageGadget(#PB_Any,10,10,620,460,UseImage(img))
  but = ButtonGadget(#PB_Any,170,475,60,18,"Load Tiff")
EndIf

Repeat 
  EventID.l = WaitWindowEvent()
  If EventID = #PB_Event_Gadget
    If EventGadgetID() = but
      myfile$ = OpenFileRequester("Please choose a tiff file", "", "Tiff files (*.tif)|*.tif","") 
      If myfile$
        ExecuteGDIPlus(myfile$)
        DisplayTiff(ImageObject)
      EndIf
    EndIf
  EndIf
Until EventID = #PB_Event_CloseWindow

DataSection
MultiPageGUID:
Data.l $7462DC86
Data.w $6180,$4C7E
Data.b $8E,$3F,$EE,$73,$33,$A7,$A4,$83
EndDataSection
Here is an example of the output:

Image

Every tiff file I have tried shows the border but here is the multipage tiff file I used in the example:
MultiFrame.tif
-Beach
Beach
Enthusiast
Enthusiast
Posts: 677
Joined: Mon Feb 02, 2004 3:16 am
Location: Beyond the sun...

Post by Beach »

The example tiff file supplied in Andreas example seems to work fine. The difference is that Andreas tiff images are true color images and the ones I need to work are binary (one color?)... Hopefully this helps.
-Beach
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

Don't get the impression that I know GDI+ like the back of my hand, but in trying different GDI+ functions, I did find something that works for me...

Change line 58

Code: Select all

If CallFunction(#GDIPlusDll,"GdipDrawImageI",GraphicObject1,Bitmap,0,0) = 0 
to this

Code: Select all

If CallFunction(#GDIPlusDll,"GdipDrawImageRectI",GraphicObject1,Bitmap,0,0, ImageWidth, ImageHeight) = 0 
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Beach
Enthusiast
Enthusiast
Posts: 677
Joined: Mon Feb 02, 2004 3:16 am
Location: Beyond the sun...

Post by Beach »

Sparkie, I'm naming my next child after you... Once again, you're the Man! :)

It works perfectly, many thanks!
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

Beach wrote:Sparkie, I'm naming my next child after you...
Beach, I feel honored. 'Lil Sparkie... PureBasic registered user# 2,000,985 :D
Beach wrote:It works perfectly, many thanks!
You're welcome. (It was dumb luck with that one.) ;)
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Post Reply