Solved

i want save a jpg image in a string and then convert it to a jpg ,how?

Posted on 2002-04-27
6
153 Views
Last Modified: 2010-04-04
i want to  save a jpg image with text format,
then  transmit these text to another program,

then convert the text into a jpg image , then show it .

i need help , how to do it ?

can html have this function?

maybe this question is too hard!

but maybe here has expert!

i have only 100 points  , sorry
0
Comment
Question by:bloodbird
6 Comments
 
LVL 1

Accepted Solution

by:
MBo earned 100 total points
ID: 6974541
uses ...jpeg;

//reads file and saves it as text (only readable symbols)
//you can make shorter strings
//it is useful to check last byte of file - Is it $FF (255)?
procedure TForm1.Button1Click(Sender: TObject);
var
FS:TFileStream;
s,s1:string;
i,j,k:integer;
begin
FS:=TFilestream.create('e:\4.jpg',fmOpenRead);
setlength(s,fs.size);
fs.Read(s[1],fs.size);
fs.free;
case Length(s) mod 3 of
  1: s:=s+#255#255;
  2: s:=s+#255;
end;
s1:='';
for i:=1 to Length(s) div 3 do begin
  j:=(ord(s[3*i-2]) and 252) shr 2;
  s1:=s1+chr(j+48);
  j:=(ord(s[3*i-2]) and 3) shl 4;
  k:=(ord(s[3*i-1]) and 240) shr 4;
  s1:=s1+chr((j or k)+48);
  j:=(ord(s[3*i-1]) and 15) shl 2;
  k:=(ord(s[3*i]) and 192) shr 6;
  s1:=s1+chr((j or k)+48);
  k:=(ord(s[3*i]) and 63);
  s1:=s1+chr(k+48);
end;
fs:=TFileStream.create('e:\jpg.txt',fmCreate);
j:=length(s1);
fs.write(s1[1],j);
fs.free;
end;

//loads text, converts to jpg and shows in Image1
//this is techique like uuencoding
procedure TForm1.Button2Click(Sender: TObject);
var
FS:TFileStream;
MS:TMemorystream;
jp:tjpegimage;
s,s1:string;
var i,j,k:integer;
begin
FS:=TFilestream.create('e:\jpg.txt',fmOpenRead);
setlength(s,fs.size);
fs.Read(s[1],fs.size);
fs.free;
s1:='';
for i:=1 to Length(s) div 4 do begin
  k:=(ord(s[4*i-3])-48) shl 2;
  j:=((ord(s[4*i-2])-48) and 48) shr 4;
  s1:=s1+chr(k or j);
  k:=((ord(s[4*i-2])-48) and 15) shl 4;
  j:=((ord(s[4*i-1])-48) and 60) shr 2;
  s1:=s1+chr(k or j);
  k:=((ord(s[4*i-1])-48) and 3) shl 6;
  j:=(ord(s[4*i])-48);
  s1:=s1+chr(k or j);
end;
while s1[Length(s1)]=#255 do delete(s1,Length(s1),1);
MS:=TMemorystream.Create;
Ms.Write(s1[1],length(s1));
jp:=tjpegimage.create;
ms.position:=0;
jp.loadfromstream(ms);
image1.picture.assign(jp);
jp.free;
Ms.Free;
end;
0
 
LVL 5

Expert Comment

by:Gwena
ID: 6974654
Listening :-)
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 6974927
why would you wanna do that ?
if you wanna send the image over internet there are better simpler ways of doing it
one would be to sayve it to a memory stream and send that over
create a header in which you set it's size so when receiving you know how much data to receive
that's really how binary data should be sent
if you wanna know the details I can post an example
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 1

Expert Comment

by:Dumani
ID: 6975391
Try the code below. It should work fine to send picture or any data over internet... I didn't test it, just wrote and send you. If i have time i can try. Let me know if it works. Feel free to ask if you don't understand...

Sincerly,

Atilla

type
  TFileInfo = packed record
    Name: ShortString;
    Size: Cardinal;
  end;

procedure SendJpeg;
var
  jpg: TJpegImage;
  stream: TMemoryStream;
  fi: TFileInfo;
  StartPos, OutBuf, InBuf: Integer;
  Buffer: array [1..4096] of byte;
begin
  jpg := TJpegImage.Create;
  try
    jpg.LoadFromFile('myfile.jpg');
    stream := TMemoryStream.Create;
    try
      jpg.SaveToStream(stream);
      stream.Position := 0;
      FillChar(fi, SizeOf(fi), #0);
      fi.Name := 'myfile.jpg';
      fi.Size := stream.Size;
      with TClientSocket.Create(nil) do
      try
        Address := '212.175.17.21'; // here comes ip..
        Port    := 123;
        ClientType := ctBlocking;
        Open;
        // First we are sending file info..
        InBuf := Socket.SendBuf(fi, SizeOf(fi));
        // if data has been recieved then begin transfer..
        if InBuf > 0 then
        begin
          while True do
          begin
            StartPos := stream.Position;
            InBuf := stream.Read(Buffer, SizeOf(Buffer));
            if InBuf > 0 then
            begin
              OutBuf := Socket.SendBuf(Buffer, InBuf);
              if InBuf > OutBuf then
                Stream.Position := StartPos + OutBuf
              else if stream.Position = stream.Size then
                Break;
            end;
          end;
        end;
        Close; // Close connection...
      finally
        Free; // Free ClientSocket...
      end;
    finally
      stream.Free; // Free stream...
    end;
  finally
    jpg.Free; // Free jpg...
  end;
end;
0
 

Expert Comment

by:RayNorrish
ID: 7184035
I` d like to know *why* more than *how* :)
0
 
LVL 1

Expert Comment

by:pnh73
ID: 9004215
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept answer from MBo

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Paul (pnh73)
EE Cleanup Volunteer
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 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