Solved

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

Posted on 2002-04-27
6
151 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

21 Experts available now in Live!

Get 1:1 Help Now