Solved

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

Posted on 2002-04-27
6
155 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
[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 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
Independent Software Vendors: 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 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

Technology Partners: 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!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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