Solved

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

Posted on 2002-04-27
6
152 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
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 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

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

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…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

18 Experts available now in Live!

Get 1:1 Help Now