Question:
Failure to load JPEG image in memory stream in Delphi 2010?
DJJD
2010-07-28 17:18:25 UTC
This code used to work perfect in Delphi 7. I don't know what happened, but I'm getting 'JPEG Error 53' when loading image from memory stream. I am processing the JPEG through sockets. Here's the relevant pieces of code:

Sending stream:
{Public}
FStream: TMemoryStream;

//(I have changed nothing here, the stream is perfect, but here's how I send it in the first place)
PrepareToSend; //Take Screen Shot to Memory Stream and load to FStream
//This works fine, I can load the stream into any other format
Socket.SendText(IntToStr(FStream.Size)+ #0 ); //send size of screenshot image (First send event)
//I don't know about the + #0 but it used to work just fine
FStream.Position:= 0; //Go to beginning of stream
try Socket.SendStream(FStream) except end; //send screenshot through a stream (Second send event)


Receiving stream:
{Public}
FData: TMemoryStream;
FDataSize: Integer;
FReciving: Bool;

(Private)
var Jpg: TJpegImage;
Len:string;
s: String;
s1,s2,s3: string;
p:integer;
Sz: Integer;
W: PWideChar;

Prepare to receive stream - after remote socket sends the size (First receive event)
FData:= TMemoryStream.Create; //Create memory stream 'S' comes in as '12915'#0 (size of stream)
SetLength(Len, StrLen(PChar(s))+1);
StrLCopy(@Len[1],PChar(s),Length(Len)-1);
FDataSize:= StrToIntDef(Len,0); //Set size of data
Delete(s,1,Length(Len));
FData.Write(s[1],length(s)); //Append size of data to stream
FReciving:= True; //next set of packets will be a stream

Receive each individual piece of the stream - after remote socket sends the stream (Second receive event)
FData.Write(s[1], length(s)); //Append new data to data stream
if (FData.Size = FDataSize) then begin //Check if all data has been received
FReciving:=false; //All data is received, no more is expected
try
Jpg := TJpegImage.Create; //Create JPEG Image object
FData.Position:= 0; //Go to beginning of data
Jpg.LoadFromStream(FData); //Fill JPEG with image data - THIS IS WHERE ERROR OCCURS
//(Removed extra irrelevant code to further process Jpg)
finally
Jpg.Free;
FData.Free;
end;
end;

So, somewhere in this process, the data in the stream must get messed up. Again, this used to work perfect in delphi 7, but not now in 2010. I think it has to do with converting the size in the first packet received.

If you can't figure out what's wrong with my code, can someone point me in a good direction where I can learn more about streams like this? This is the first time I've worked with memory streams.
Three answers:
mystic smeg
2010-07-29 00:44:27 UTC
Hi There,



You've not posted enough code on here to be of much help to us, and in most cases code like this is easier to trace to identify the problem rather than by eye. (if you want to send some code to me, I'll debug it; you already have my mail address).



Having said this, I've had an initial look at the code, and nothing obvious stands out. If you think the numeric conversion of the file size is to blame, make an image of a known byte size, and "watch" the value go through the system. For example: image = 1024 bytes, value is 1024, value received is 1024 etc.



Hope this helps



na
larison
2016-10-21 12:45:51 UTC
Jpeg Error 53
dorita
2017-01-17 22:39:28 UTC
the prophecy to lycurgus replaced into that sparta could fail by economic greed, and that the city could prosper as long because it eschewed slavery. in fact sparta replaced into the sole substantial greek state to have an economic device in accordance with enslavement of hellenes (the helots) and fell by militia defeat which had no longer something to do with economic concerns (epaminondas at leuctra). so this prophecy replaced into thoroughly out of whack. as have been lots of the different 'real' ones. merely because you slept by historic previous would not practice that there is a god.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...