Solved

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

Posted on 2003-11-09
6
224 Views
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.

thanks

Ian
0
Comment
Question by:alchemy9
6 Comments
 
LVL 2

Expert Comment

by:craig_capel
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?
0
 

Author Comment

by:alchemy9
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?

0
 
LVL 33

Expert Comment

by:Slick812
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

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Expert Comment

by:Gwena
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
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
 
LVL 17

Accepted Solution

by:
geobul earned 250 total points
ID: 9714710
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
 
LVL 7

Expert Comment

by:jconde
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",
"................",
".######.........",
".#aaaa##........",
".#aaaa#a#.......",
".#a##a#bbbbbb...",
".#aaaaabaaaabb..",
".#a####baaaabab.",
".#aaaaaba##abbbb",
".#a####baaaaaaab",
".#aaaaaba#####ab",
".######baaaaaaab",
".......ba#####ab",
".......baaaaaaab",
".......bbbbbbbbb",
"................",
"................"};
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

930 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now