Good morning all,
For anyone wishing to draw an image file (bmp/jpg/png.....), and in particular KCC, the following procedure should do the necessary

Thanks to feedback from KCC I have stamped on a silly bug and revised the code to be a bit safer.
Also, There is a
new function ; uGridDrawTextImage() to draw text in an image cell with the option to clear the cell before, or leave an existing picture in place.
I have not blended the new procedures into the main code because the main code is already about 60,000 characters in size and adding they would pop the 60,000 character upload limit.
Don't forget to install the appropriate decoders...
RichardL
Code: Select all
Procedure uGridDrawFileImage(*uGridCell,state,FileName$,NoScale=#False) ; Load and scale a file image into cell
; Load an image, scale it to fit a specified flip cell and draw
; it onto the specified cell. A grid refresh will required
; after all images have been drawn.
; Filename$ : Full path, name and extension.
; *uGridCell : Pointer to sructure defining the cell
; State : 0 or 1 to specify the cell's state.
; Scaling : Default is to keeping image aspect ratio correct and providing
; a border top & bottom or left & right.
; NoScale = #true the image is scaled to fill the cell and may
; appear distorted
Protected IID,IW,Ih,AR_Image.f,cw,ch,AR_Cell.f,X,Y,*CellDat.CELLOPTIONS,T,F.f
; Check for null pointer
If *uGridCell = 0 : ProcedureReturn #False : EndIf
; Load the specified picture image
If FileSize(FileName$) = 0 : ProcedureReturn #False : EndIf ; No file...
Debug FileName$
IID = LoadImage(#PB_Any,FileName$)
If IID = 0 : ProcedureReturn #False : EndIf ; Will not load...
Debug "here................"
; Get size of picture and calculate the aspect ratio.
IW = ImageWidth(IID) : Ih = ImageHeight(IID)
AR_Image = IW/Ih
*CellDat.CELLOPTIONS = *uGridCell
With *CellDat
; Get size of cell and calculate the aspect ratio.
T = \CellImage[state & 1]
cw = ImageWidth(T) : ch = ImageHeight(T)
AR_Cell = cw / ch
; Calculate scaling factor so major image dimension
; fits the cell, then scale the image.
If NoScale
ResizeImage(IID,cw,ch)
X=0 : Y = 0
Else
If AR_Image > AR_Cell
F = IW / cw
ResizeImage(IID, cw, Ih / F)
X = 0 : Y = (ch-ImageHeight(IID))/2
Else
F = Ih / ch
ResizeImage(IID, IW / F, ch)
X = (cw-ImageWidth(IID))/2 : Y = 0
EndIf
EndIf
; Draw the image on the cell
If StartDrawing(ImageOutput(\CellImage[state & 1]))
Box(0,0,cw,ch,\CellBackColour)
DrawImage(ImageID(IID),X,Y)
StopDrawing()
EndIf
EndWith
FreeImage(IID)
ProcedureReturn #True
EndProcedure
Procedure uGridDrawTextImage(GadNum,*uGridCell,state,Text$,NoClear=#True) ; Render a text string into an image cell.
; Gadnum : Ugrid gadget number
; *uGridCell : Pointer to sructure defining the cell
; State : 0 or 1 to specify the cell's state.
; Text$ ; String to be rendered. Use '|' to force a new line.
Protected IID,IW,Ih,cw,ch,Y,*CellDat.CELLOPTIONS,T,GridIndex,n,k$
GridIndex = uGridGetGridIndex(GadNum) ; Look in table for wanted grid
If GridIndex = -1 : ProcedureReturn #False : EndIf ; Grid not found
If *uGridCell = 0 : ProcedureReturn #False : EndIf ; Check for NULL pointer
*CellDat.CELLOPTIONS = *uGridCell
With *CellDat
; Get cell image dimensions
T = \CellImage[state & 1]
cw = ImageWidth(T) : ch = ImageHeight(T)
If StartDrawing(ImageOutput(\CellImage[state & 1]))
DrawingMode(#PB_2DDrawing_Transparent)
DrawingFont(uGrid(GridIndex)\GridFontID)
If NoClear = #False ; Optionally....
Box(0,0,cw,ch,\CellBackColour) ; clear the cell.
EndIf
Y = 0 ; Initial text position.
For n = 1 To CountString(Text$,"|") + 1
k$ = StringField(Text$,n,"|") ; Isolate the field,
DrawText(1,Y,k$,\CellTextColour) ; show it,
Y + TextHeight("Xy") ; move text position down one row.
If Y+TextHeight("Xy") > \CellH ; Check there is room for another row...
Break ; NO...!
EndIf
Next
StopDrawing()
EndIf
EndWith
ProcedureReturn #True
EndProcedure
Here is a code snip that I use to test the above procedures:
Code: Select all
UseJPEGImageDecoder() ; Define libraries
FileName$ = "c:\temp\FredIsHere.jpg"
T = uGridGetCellPtr(#Gad_Grid3,2,0)
uGridDrawFileImage(T,0,FileName$)
FileName$ = "c:\temp\temppic.jpg"
T = uGridGetCellPtr(#Gad_Grid3,2,1)
uGridDrawFileImage(T,0,FileName$,0)
FileName$ = "c:\temp\tall.jpg"
T = uGridGetCellPtr(#Gad_Grid3,2,2)
uGridDrawFileImage(T,0,FileName$)
FileName$ = "c:\temp\wide.jpg"
T = uGridGetCellPtr(#Gad_Grid3,2,2)
uGridDrawFileImage(T,1,FileName$)
uGridDrawTextImage(#Gad_Grid3,uGridGetCellPtr(#Gad_Grid3,2,0),0," Hello| World",1)
uGridDrawTextImage(#Gad_Grid3,uGridGetCellPtr(#Gad_Grid3,2,0),1," Hello| you...",1)