Solved

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

Posted on 2003-11-09
6
227 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
[X]
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
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 34

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
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!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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