I am trying to put the contents of a PNG file into a constant called kIconData of type string. When I run I am getting a syntax error. The string I am putting into the constant is as below. Any reasons why? I have also tried creating a string property and assigning this as the default value but again I get syntax error.
try using EncodeBase64 and DecodeBase64 when storing and retrieveing the picture…
Ironically I was adding the ability to convert a picture to and from a string when I read this post…
I found code I’d written a while back for another project and was about to drop it in to my current one
It does require a temp file but seems to be extremely fast…
FUNCTION PicturetoString(p as picture) as string
// Convert a picture to a String by saving the picture
// out to a file on disk and then reading in the binary
// data from that file.
Dim f As folderItem
Dim bs As binaryStream
Dim data As String
// save picture to a temporary file
If p=Nil Then Return ""
f=GetTemporaryFolderItem
p.Save(f,picture.SaveAsJPEG)
// read the data in from the temp file
bs=BinaryStream.Open(f,false)
data=bs.read( bs.length )
bs.close
// remove temporary file
f.delete
// return the binary data
data=EncodeBase64(data,0)
Return data
Exception// an exception occurred preventing the conversion
Return ""
END FUNCTION
FUNCTION StringtoPicture(data as string) as picture
// Convert a String to a picture by saving the binary data
// to a file on disk and then reading the file As a picture.
Dim f As folderItem
Dim bs As binaryStream
Dim p As picture
If data="" Then Return Nil
// write binary data to temporary file
f=GetTemporaryFolderItem
bs=binaryStream.Open(f,True)
bs.write DecodeBase64(data)
bs.close
// open the file As a picture
//p=f.openAsPicture
p=picture.open(f)
// remove temporary file
f.delete
// return the binary data
Return p
Exception// an exception occurred preventing the conversion
Return Nil
[quote=21193:@Dave S]Drag the icon into the IDE/Project
then use Drawpicture
assume you dragged myicon.png into the project
g.drawpicture myicon,x,y
simple as that… the ide stores it as a resource[/quote]
Dave, I understand that but I am trying to avoid having the icons in the IDE. The reason being I am writing a custom class which needs a couple of Icons with it. I want to encrypt the class and make available but dont want to have to separately have icons shipped with it. Therefore my thoughts were open up the icons in a text editor, copy the text into a constant within my class and from there create the picture from the string in the constant therefore eliminating the need of the icon files.
const myIcon=“8091384103941023948120934812309481”
where that string is the result of PictureToString
Then in you class add StringtoPicture
p=StringtoPicture(myIcon)
It is a combination of both methods. use one routine to give you the string to include as your const, and the other routine to create a pic from the hard-coded string
Almost, but using those methods allow you to convert a MemoryBlock/string directly to and from a Picture object, no temporary file required.
Function PicToString(Pic As Picture) As String
Return EncodeBase64(Pic.GetData(Picture.FormatPNG))
End Function
Function StringToPic(RawData As String) As Picture
Return Picture.FromData(DecodeBase64(RawData))
End Function
My quick and dirty tests show that both techniques are about the same speed. I would have expected the technique that avoids file I/O to be much faster, though.