image ResEx Logo
ResExcellence www : Powered by Google
Cell Phone Themes Icons Mighty Mouse Cursors Software Reviews Widgets & Widgets

Articles
   3D
   Audio
   Custom Controls
   General RB
   Graphics
   Hacks
   Mac OS X
   Menus
   Novelty
   Printing
   REALbasic 2005
   REALbasic 2006
   Registration
   Resources
   Reviews
   Serial
   Speech
   Sockets
   XML
   Video
Resource Links
News
   Current News
   February 2006
   January 2006
   December 2005
   November 2005
   October 2005
   September 2005
   August 2005
   July 2005
   June 2005
   May 2005
   April 2005
   March 2005









REALbasic for Dummies
by Erick Tejkowski


Learning REALbasic through Applications
by Clayton E., Crooks II


REALbasic for Macintosh
by Michael Swaine


REALbasic Cross-Platform Application Development
by Mark S. Choate





Older files are in Stuffit 5 or greater format. Newer files are ".Zip". Download StuffIt Expander
Tell us about a bad link. Thank You!

Picture String Extensions by Seth Willits
09-18-04

Printer Version




Pictures as Strings
Very often it's necessary or desired to get a picture into a string. For example, to store it in a database, or send it over a socket, you must have the data of the picture represented by a string somehow. How to do this comes up fairly often on the mailing lists, so I figured writing a tutorial explaining one of my new mega-template-project modules, PictureStringExtensions, would be useful. This module contains two methods that separately extend the Picture and String datatypes. The Data method extends Picture and returns a string for that picture, and GetPicture extends String, returning a picture if the string is valid data for an image.


The Data Method
To get a picture's data as a string simply involves reading the picture's file with a BinaryStream. However, since a picture can be created on the fly, the picture might not have a file, so we'll need to create one to read. The method extending the Picture class is as follows.



The first thing we do is save the picture to a new temporary file (using the current time as part of the file name just to make sure we don't kill any other application's temporary file that happens to be named the same). The file is then opened with a BinaryStream, read entirely into a local variable, close the stream, delete the temporary file, and return the picture's data. If an error occurs, an empty string is returned.


The GetPicture Method
The GetPicture method is simply the opposite of the Data method. We create a temporary file with a binary stream and write the data to it, thten we open the file using OpenAsPicture and return the Picture returned from it.



A word of caution: be very careful about encodings. A string has an encoding, and whatever happens, you DON'T want to convert the picture data string's encoding. If for some reason you're getting a nil picture when converting from stored data, follow the encoding and make sure it doesn't change.



Finished
It's a very simple module but it can home in handy if you have to work with sockets or databases. There are other 3rd part extensions to turn a string into a picture and vice versa, and they're probably better to use if you need convert maaany pictures in succession, but for just one at a time or something, this will work fine. As always, you can download the project here.




Cell Phone Themes Icons Mighty Mouse Cursors Software Reviews Widgets & Widgets

Maintained by the Staff of ResExcellence. This entire site ©1997-2006 ResExcellence
Privacy Statement? Sure we gotta Privacy Statement. [an error occurred while processing this directive]