saving an icon to a string and loding an icron from a string

Posted on 2003-11-09
Last Modified: 2010-04-05
has anyone got any sample code on how to save an icon to a string, then load an icon from a string.


Question by:alchemy9
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 9709709
My guess would be to turn it into MIME (like email) and store it as a string?  but then why? if you wanted to store it turn it into a resource file and put it direct into your exe?

Author Comment

ID: 9710131
I want to store other information into a string compress it then encrypt it, so all the information is in one file, thats the reason I want to export/imort the icon into a string,

oops come to think about it how would you do it if it was a bitmap?

LVL 34

Expert Comment

ID: 9710216
hello alchemy9, you only give a little information about what you want to do, you seem to want to use a "String" as a Memory block to store various items (data) into, and NOT use any of the string Charater methods at all, and then create a File and and transfer this memory block (string) into the file. . . If you have delphi  TIcons and  TBitimaps, the only delphi methods I know of to get their "File Data" is their "SaveToStream" method, which does not work with strings (at least not directly), It would be easier to create a TMemoryStream as a memory block to store your data segments (items, icons, bitmaps) and then compress , encript or other stuff to this TMemoryStream and then just use the "SaveToStream" to a TFileStream. . . . But this is not so easy, if you have not done this before. . . . You might look at my site and web page for building muti-data files at

for ideas and methods, You could use a TMemoryStream instead of a file stream to save your data segmemts to

ask questions if you need more
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 9712581
So basically you want to be able to store all sorts of things.. bitmaps,icons,exe files,txt files..whatever
into a single string and be able to save that string to a file on disk and work with it at runtime by
extracting and using any item you want?

I can show you how to put all sorts of data into a string and save it as a file...
You can then extract,alter,replace,delete,add any item you wish... it's almost like
the single file is a dir.

If this is what you want let me know and I will provide some sort of example for you to

In the meantime here is a link to an old demo I did for someone else that wanted to do
this sort of thing...saving images,wav files to an external file (one huge string) and encrypt
them as well.... I added very crude encryption...just to show him where to put his encrypt
code... you could also add compression/decompression code in the same spot.

You MUST right click this link and choose 'save target as' because geocities does not allow
external links to files.

and here is the readme file from this old demo
This is a simple demo that shows how to use the ExeMod.pas
routines to store and retrieve data to/from an external data file

The data file is named MyData.dat and is in the current dir.

I added simple XOR encrypt/decrypt just to show how easy it is
to scramble the data. This scrambling does have a bit of overhead
to it and slows down the loading of large pieces of data.

I have made up some small wav and jpg images and already loaded
them into MyData.dat so you can click on the [play wav] and [show jpg]
buttons immediately.. or erase the data file and start from scratch
with your own files... or simply overwrite my data by adding new data.

There is a lot of redundant code in this demo.. but I wanted each
button click handler to be complete in itself... to make it easier
to follow the logic of the code.

LVL 17

Accepted Solution

geobul earned 250 total points
ID: 9714710

The following is from Madshi (he posted the code below here in Ex-Ex):

I'm using CreateIconIndirect for such purposes. Here are some functions that I've written. These functions
can "convert" a bitmap or an icon into a string (I mean putting all relevant data into a string buffer)
and vice versa. You should see everything you need from those sources:

 TACardinal  = array [0..maxInt shr 2-1] of cardinal;
 TPACardinal = ^TACardinal;

function BitmapToStr(bh: dword) : string;
var dc1, dc2   : dword;
   bh2        : dword;
   obh1, obh2 : dword;
   s1         : string;
   p1         : pointer;
   bi         : packed record
                  bmiHeader : TBitmapInfoHeader;
                  bmiColors : packed array [0..255] of TRgbQuad;
 result := '';
 dc1 := CreateCompatibleDC(0);
   ZeroMemory(@bi, sizeOf(bi));
   bi.bmiHeader.biSize := sizeOf(bi.bmiHeader);
   GetDIBits(dc1, bh, 0, 0, nil, PBitmapInfo(@bi)^, DIB_RGB_COLORS);
   bh2 := 0;
     if (bi.bmiHeader.biBitCount > 1) and (bi.bmiHeader.biBitCount <> 24) then begin
       bi.bmiHeader.biBitCount    := 24;
       bi.bmiHeader.biCompression := BI_RGB;
       bh2 := CreateDIBSection(dc1, PBitmapInfo(@bi)^, DIB_RGB_COLORS, p1, 0, 0);
       dc2 := CreateCompatibleDC(0);
         obh1 := SelectObject(dc1, bh);
         obh2 := SelectObject(dc2, bh2);
         BitBlt(dc2, 0, 0, bi.bmiHeader.biWidth, bi.bmiHeader.biHeight, dc1, 0, 0, SRCCOPY);
         SelectObject(dc2, obh2);
         SelectObject(dc1, obh1);
         bh := bh2;
         ZeroMemory(@bi, sizeOf(bi));
         bi.bmiHeader.biSize := sizeOf(bi.bmiHeader);
         GetDIBits(dc1, bh, 0, 0, nil, PBitmapInfo(@bi)^, DIB_RGB_COLORS);
       finally DeleteDC(dc2) end;
     if bi.bmiHeader.biSizeImage <> 0 then begin
       SetLength(s1, 16 + bi.bmiHeader.biSizeImage);
       ZeroMemory(pointer(s1), Length(s1));
       if GetDIBits(dc1, bh, 0, bi.bmiHeader.biHeight, @TPACardinal(s1)^[4], PBitmapInfo(@bi)^, DIB_RGB_COLORS)
<> 0 then begin
         TPACardinal(s1)^[0] := bi.bmiHeader.biWidth;
         TPACardinal(s1)^[1] := bi.bmiHeader.biHeight;
         TPACardinal(s1)^[2] := bi.bmiHeader.biBitCount;
         TPACardinal(s1)^[3] := bi.bmiHeader.biSizeImage;
         result := s1;
   finally DeleteObject(bh2) end;
 finally DeleteDC(dc1) end;

function IconToStr(icon: dword) : string;
var ii : TIconInfo;
 result := '';
 if GetIconInfo(icon, ii) then
     result := BitmapToStr(ii.hbmColor) + BitmapToStr(ii.hbmMask);
     DeleteObject(ii.hbmMask );

function StrToBitmap(var str: string) : dword;
var dc : dword;
   p1 : pointer;
   bi : packed record
          bmiHeader : TBitmapInfoHeader;
          bmiColors : packed array [0..255] of TRgbQuad;
 ZeroMemory(@bi, sizeOf(bi));
 bi.bmiHeader.biSize        := sizeOf(bi.bmiHeader);
 bi.bmiHeader.biWidth       := TPACardinal(str)^[0];
 bi.bmiHeader.biHeight      := TPACardinal(str)^[1];
 bi.bmiHeader.biPlanes      := 1;
 bi.bmiHeader.biBitCount    := TPACardinal(str)^[2];
 bi.bmiHeader.biSizeImage   := TPACardinal(str)^[3];
 dword(bi.bmiColors[1])     := $FFFFFF;
 dc := CreateCompatibleDC(0);
   p1 := nil;
   result := CreateDIBSection(dc, PBitmapInfo(@bi)^, DIB_RGB_COLORS, p1, 0, 0);
   if (result <> 0) and (p1 <> nil) then
     Move(TPACardinal(str)^[4], p1^, bi.bmiHeader.biSizeImage);
 finally DeleteDC(dc) end;
 Delete(str, 1, 16 + TPACardinal(str)^[3]);

function StrToIcon(str: string) : dword;
var ii : TIconInfo;
 ii.hbmColor := StrToBitmap(str);
   ii.hbmMask := StrToBitmap(str);
     ii.fIcon := true;
     result := CreateIconIndirect(ii);
   finally DeleteObject(ii.hbmMask) end;
 finally DeleteObject(ii.hbmColor) end;
<End of quote>

Regards, Geo

Expert Comment

ID: 9715726
Have you tried looking at the XPM format commonly used in Linux ?

Maybe you can convert your icon to XPM and then put everything on the same line!  Then for reading, just use the "headers" to determine the original line-length.

Note:  Since XPM files are text files where a bunch of characters repeat itself, you will benefit the most in the compression algorithm (assuming your encryption algorithm doesn't change it too much).

Here's an example of an XPM file:

/* XPM */
static const char* const copyIcon_data[] = {
"16 16 4 1",  /*Width, Height, Colors Used, something else+/
". c None", /* transparent color*/
"# c #4a4a4a", /*some color in hex format ... just as in html*/
"b c #4a4aa5",
"a c #ffffff",

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question