Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2002-04-27
6
Medium Priority
?
161 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 400 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

618 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