String Constant Causing Syntax Error?

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.

PNG

IHDR
IDAT8?=kAܘ(m:! ?ĥJ?:Q@L?k)
}{?q)nw???\?z?fۂ6w>???,䢜8_̈?(???PB?Z?0tbn]?N>V02lXe1M
jT*GS퉱 ?z4=(DjG3N|>7J?C 6h!CW?r?4H4Mj?2xܺg?Pk?ID?B6 ay0نp(.NB~1?P*ND;)T,00??OtDojcZ?0g?GS?,?}FX?4M1AbA@: s*p?>8?3?)|?Ka??|??1eIENDB

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

Dont really want to write a file. I wanted to store the icon data in a constant and then load this as a picture from within my class.

You can use the FromData and GetData methods of the Picture class to eliminate the need for a temp file.

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

Uh… confused… that code is almost exactly what I posted…

[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.

Ok… then use the code I provided above.

Get the Encoded string and put that in your class

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

Like it Dave, good thinking. I’ll give that a whirl. Thanks

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

yes but that is NOT the code in the XOJO lang ref.

But thank. :slight_smile: that sure is smaller (and I hope FASTER)

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.

Thanks gents. I have gone with Andrew L’s version, merely to reduce the overhead of the file i/o.

I would say the LR wants updating with this version.

No worries… I too converted to that method… just adding one line to make sure things were not nil