Load picture path from text file and assign it to Canvas1

MAX OS X Desktop

I have a Listbox where in one column I save the name of a .jpg file.
I have the default path to the directory where the .jpg’s are stored, saved in a text file called “Connect.txt”
When I move thru the lines of the listbox, I am assigning the picture, based on the listbox line and column, to load in Canvas1.

My problem is after I load the default path that I saved in my .txt file, I am not sure how to assign the path plus the file name in my listbox to the canvas.
Here is my code:
//-----------------------------------------
dim FI as FolderItem
dim TOS as textInputStream
//******************
FI = SpecialFolder.ApplicationData()
FI = FI.Child (“Connect.txt”)
If FI.Exists Then
TOS = TextinputStream.Open(FI)
Dim MyParameter as string = TOS.ReadLine
TOS.Close

TextField1.text = MyParameter

End If

Dim p as Picture
Dim MyFile as FolderItem = MyParameter.Child(productLST.cell(productLST.ListIndex, 3)+" - 0.jpg")
p = Picture.Open(MyFile)

If productLST.ListIndex >=0 Then
If MyFile.Exists then
canvas1.graphics.drawpicture p,0,0, canvas1.width,canvas1.height,0,0,p.width,p.height
picLBL.text = (productLST.cell(productLST.ListIndex, 3)+" - 0.jpg")
Else
Canvas1.Backdrop = Nil
picLBL.text = “”
End IF
End IF
//-----------------------------------------

In the above code, if I change MyFile to:
Dim MyFile as FolderItem = SpecialFolder.Home.Child(“jeff”).Child(“Dropbox”).Child(“ImportFirst”).Child(“Inventory”).Child(“productshots”).Child(productLST.cell(productLST.ListIndex, 3)+" - 0.jpg")

Then it loads the file fine, but I don’t want to code the default path, I want to load and use the default path from the “Connect.txt” file that I have saved.

I think the problem is with the syntax trying to use “MyParameter” in: Dim MyFile as FolderItem = MyParameter.Child(productLST.cell(productLST.ListIndex, 3)+" - 0.jpg")

Thanks!

Dim MyFile as FolderItem = MyParameter.Child(productLST.cell(productLST.ListIndex, 3)+" - 0.jpg")

MyParameter is a string and can not have a child.

maybe something like

[code]MyParameter = MyParameter + productLST.cell(productLST.ListIndex, 3)+" - 0.jpg"

Dim MyFile as FolderItem = GetFolderItem(MyParameter, FolderItem.PathTypeNative)[/code]

Thanks for pointing me in the right direction. Works like a charm. Here is the final code:

Dim FI as FolderItem
Dim TOS as textInputStream
//******************
FI = SpecialFolder.ApplicationData()
FI = FI.Child (“Connect.txt”)
If FI.Exists Then
TOS = TextinputStream.Open(FI)
Dim MyParameter as string = TOS.ReadLine
TOS.Close

MyParameter = MyParameter + "/"+ productLST.cell(productLST.ListIndex, 3)+" - 0.jpg"

Dim p as Picture
Dim MyFile as FolderItem = GetFolderItem(MyParameter, FolderItem.PathTypeNative)
p = Picture.Open(MyFile)

If productLST.ListIndex >=0 Then
  If MyFile.Exists then
    canvas1.graphics.drawpicture p,0,0, canvas1.width,canvas1.height,0,0,p.width,p.height
    picLBL.text = (productLST.cell(productLST.ListIndex, 3)+" - 0.jpg")
  Else
    Canvas1.Backdrop = Nil
    picLBL.text = ""
  End IF
End IF

End IF

Also by chance do you know how to size the picture proportionately in Canvas1 ? The code fits the picture in canvas1 by height and width:

canvas1.graphics.drawpicture p,0,0, canvas1.width,canvas1.height,0,0,p.width,p.height

but it warps/distorts the picture to fit in canvas1. Is there any way to fit the picture in Canvas1 without stretching it or distorting it?

is the picture bigger than the canvas ?

If MyFile.Exists then
dim factor as double = p.Width/p.Height
  dim newPic as new Picture(Canvas1.Width,Canvas1.Height,32)
  newPic.Graphics.DrawPicture(p, 0, 0, Canvas1.Width, Canvas1.Width/factor, 0, 0, p.Width, p.Height)
  Canvas1.Backdrop = newPic

picLBL.text = (productLST.cell(productLST.ListIndex, 3)+" - 0.jpg")
....

Yes the picture is bigger than the Canvas so it needs to be scaled to fit but proportionally so it is not distorted.

With the code you suggested:


If MyFile.Exists then
dim factor as double = p.Width/p.Height
dim newPic as new Picture(Canvas1.Width,Canvas1.Height,32)
newPic.Graphics.DrawPicture(p, 0, 0, Canvas1.Width, Canvas1.Width/factor, 0, 0, p.Width, p.Height)
Canvas1.Backdrop = newPic

picLBL.text = (productLST.cell(productLST.ListIndex, 3)+" - 0.jpg")


The picture scales down and does not get warped, but several of the them get cut off on the bottom and you only see the top 3/4 of the picture.

[code]If MyFile.Exists then
dim factor as double = p.Width/p.Height
dim newPic as new Picture(Canvas1.Width,Canvas1.Height,32)
if factor > 1 then
newPic.Graphics.DrawPicture(p, 0, 0, Canvas1.Width, Canvas1.Width/factor, 0, 0, p.Width, p.Height)
else
newPic.Graphics.DrawPicture(p, 0, 0, Canvas1.Height/factor, Canvas1.Width, 0, 0, p.Width, p.Height)
End If
Canvas1.Backdrop = newPic

picLBL.text = (productLST.cell(productLST.ListIndex, 3)+" - 0.jpg")[/code]

a tipp, if you want to post code please click the ‘code symbol’ and then paste your code.

Tried but is now warping pictures that are portrait style. Landscape pictures seem to be fine.

or copy this method to your project

and then

If MyFile <> Nil and MyFile.Exists then
Canvas1.Backdrop = ScaledPicture(picture.Open(MyFile),Canvas1.Width, Canvas1.Height)

picLBL.text = (productLST.cell(productLST.ListIndex, 3)+" - 0.jpg")

Above gave me an error

Too many arguments: got 3 expected only 0
Canvas1.Backdrop = ScaledPicture(picture.Open(MyFile),Canvas1.Width, Canvas1.Height)

click on the Method (left side in IDE)

maybe you forgot to add Parameters and Return Type on the right side

Yep forgot add the parameters - thanks that fixed the function. (Sorry for my shortfalls, for databases I used to program in Access, then Filemaker…now jumping into Xojo since Filemaker’s pricing model is out of control)

The function works for landscape pictures and also centers them, but Portrait pictures are moved up in the Canvas and cut off the top 1/4 of the picture.

I added 2 lines added after I had published it

is this in the method? (works for me)

[code]dim factor as double = p.Width/p.Height
dim newPic as new Picture(w,h,32)

if factor > 1 then
//// scale picture proportional and center it
newPic.Graphics.DrawPicture(p, 0 , (h - w / factor ) / 2 , w, w/factor, 0, 0, p.Width, p.Height)
elseif factor = 1 then
newPic.Graphics.DrawPicture(p, (w - h / factor) / 2, 0, h/factor, h, 0, 0, p.Width, p.Height)
elseif factor < 1 then
newPic.Graphics.DrawPicture(p, (w - h * factor) / 2, (h - h / factor ) / 2, h * factor , w , 0, 0, p.Width, p.Height)
end if
Return newPic[/code]

Yes my Method is the same

[code] dim factor as double = p.Width/p.Height
dim newPic as new Picture(w,h,32)

if factor > 1 then
//// scale picture proportional and center it
newPic.Graphics.DrawPicture(p, 0 , (h - w / factor ) / 2 , w, w/factor, 0, 0, p.Width, p.Height)
elseif factor = 1 then
newPic.Graphics.DrawPicture(p, (w - h / factor) / 2, 0, h/factor, h, 0, 0, p.Width, p.Height)
elseif factor < 1 then
newPic.Graphics.DrawPicture(p, (w - h * factor) / 2, (h - h / factor ) / 2, h * factor , w , 0, 0, p.Width, p.Height)
end if
Return newPic[/code]

Put for portrait pictures I get this: (Landscape pictures work perfect)

it works here

https://dl.dropbox.com/s/hzxbkqjm27v8fxo/testmovie5.webm

Strange look here this is what is does for me. (Notice the portrait pictures are moved up and cut off in the video)

https://www.dropbox.com/s/zn4x402gqg3dr0j/IMG_2802.MOV.mov?dl=0

Thanks for putting me on the right path.
Did some playing around and came up with this approach that works perfect! Found it here: https://forum.xojo.com/12211-proportionally-resizing-a-picture/0#p90702

Used this method

[code]Function ResizeToFit(p as Picture, maxWidth as Integer, maxHeight as Integer) As Picture
’ Calculate the scale ratio
dim ratio as Double = min( maxHeight/p.height, maxWidth/p.width)

' Create a new picture to return
dim newPic as new Picture( p.width * ratio, p.height * ratio )

' Draw picture in the new size
newPic.graphics.DrawPicture( p, 0, 0, newPic.width, newPic.height, 0, 0, p.width, p.height)

return newPic

End Function[/code]

And executed it like this:

[code] Dim FI as FolderItem
Dim TOS as textInputStream

FI = SpecialFolder.ApplicationData()
FI = FI.Child (“Connect.txt”)
If FI.Exists Then
TOS = TextinputStream.Open(FI)
Dim MyParameter as string = TOS.ReadLine
TOS.Close

MyParameter = MyParameter + "/"+ productLST.cell(productLST.ListIndex, 3)+" - 0.jpg"

Dim p as Picture
Dim MyFile as FolderItem = GetFolderItem(MyParameter, FolderItem.PathTypeNative)
p = Picture.Open(MyFile)

If productLST.ListIndex >=0 Then
  If MyFile <> Nil and MyFile.Exists then
    Canvas1.Backdrop = ResizeToFit(picture.Open(MyFile),Canvas1.Width, Canvas1.Height)
    
    picLBL.text = (productLST.cell(productLST.ListIndex, 3)+" - 0.jpg")
  Else
    Canvas1.Backdrop = Nil
    picLBL.text = ""
  End IF
End IF

End IF
[/code]

Thanks for all your help. Couldn’t of figured it out without your direction!

in your movie I see you are using OSX.

you can use an imagewell and you have auto scale

ImageWell.Open

declare sub setImageScaling lib "Cocoa" selector "setImageScaling:" ( handle as integer, value as integer ) setImageScaling( me.handle, 0 )

Thanks.