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

has anyone got any sample code on how to save an icon to a string, then load an icon from a string.

thanks

Ian
alchemy9Asked:
Who is Participating?
 
geobulConnect With a Mentor Commented:
Hi,

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

<Quote>
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:

type
 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;
                end;
begin
 result := '';
 dc1 := CreateCompatibleDC(0);
 try
   ZeroMemory(@bi, sizeOf(bi));
   bi.bmiHeader.biSize := sizeOf(bi.bmiHeader);
   GetDIBits(dc1, bh, 0, 0, nil, PBitmapInfo(@bi)^, DIB_RGB_COLORS);
   bh2 := 0;
   try
     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);
       try
         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;
     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;
       end;
     end;
   finally DeleteObject(bh2) end;
 finally DeleteDC(dc1) end;
end;

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

function StrToBitmap(var str: string) : dword;
var dc : dword;
   p1 : pointer;
   bi : packed record
          bmiHeader : TBitmapInfoHeader;
          bmiColors : packed array [0..255] of TRgbQuad;
        end;
begin
 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);
 try
   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]);
end;

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

Regards, Geo
0
 
craig_capelCommented:
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?
0
 
alchemy9Author Commented:
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?

0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Slick812Commented:
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

http://www.angelfire.com/hi5/delphizeus/customfiles.html

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
0
 
GwenaCommented:
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
test.

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.

http://www.geocities.com/gacarpenter386/easydatademo.zip

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.

..Gwen..
0
 
jcondeCommented:
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",
"................",
".######.........",
".#aaaa##........",
".#aaaa#a#.......",
".#a##a#bbbbbb...",
".#aaaaabaaaabb..",
".#a####baaaabab.",
".#aaaaaba##abbbb",
".#a####baaaaaaab",
".#aaaaaba#####ab",
".######baaaaaaab",
".......ba#####ab",
".......baaaaaaab",
".......bbbbbbbbb",
"................",
"................"};
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.