jpeg to bmp with stream. doesn't work

I use this to capture the screen, pass it to a stream,
load a jpeg from the stream, and finally pass the jpg to
a TImage..
when I try to pass the jpg to the TImage I get the error
"raised exception class EInvalidGraphic with message
'Bitmap image is not valid'" ( at run time in the IDE )
or "Bitmap image is not valid" outside the IDE.

I don't get an error if I load the stream with a jpeg,
but then.. how do I pass the jpg to a TImage ?

procedure TMouseForm.Button3Click(Sender: TObject);
Var colordepth,colormode,quality: Integer;
begin

colordepth:= 24;
colormode:= 0;
quality:= 100;

stream:= TMemoryStream.Create;
functions.Screen2Stream(colordepth,colormode,quality,stream);
stream.Position := 0;
Image1.Picture.Bitmap.LoadFromStream(stream);
stream.Free;
end;

procedure TFunctions.Screen2Stream(ColorDepth,ColorMode,Quality: Integer; stream: TMemoryStream);
Var a: TJPEGImage;
    b: TImage;
    DC: HDC;
begin
a:= TJpegImage.Create;
b:= TImage.Create(self);

DC:=getDC(GetDeskTopWindow);

b.Width:=Screen.Width;
b.Height:=Screen.Height;
BitBlt(b.Canvas.Handle,0,0,Screen.Width,Screen.Height,DC,0,0,SRCCOPY);
a.Assign(b.picture.bitmap);

if ColorDepth = 24 then a.PixelFormat:= jf24Bit else a.PixelFormat:= jf8Bit;
if Colormode = 1 then a.Grayscale:= true;
a.CompressionQuality:= Quality;

a.SaveToStream(stream);
showmessage(inttostr(stream.Size));

ReleaseDC(DC,DC);
b.Free;
a.Free;
end;
LVL 3
bryan7Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kretzschmarCommented:
hi bryan,

this errormessage occurs, because you will stream into the bitmap-object a jpeg-format in your button3Click procedure. use there an additional jpeg-object to read the stream into it and then assign it to the bitmap.

meikl
0
bryan7Author Commented:
how do I pass the jpg to a TImage ?
0
kretzschmarCommented:
hi bryan,

with the same method as you get the bitmap into a jpeg.

sample correction


procedure TMouseForm.Button3Click(Sender: TObject);
Var colordepth,colormode,quality: Integer;
    j : TJpegImage;
begin

  colordepth:= 24;
  colormode:= 0;
  quality:= 100;

  stream:= TMemoryStream.Create;
  functions.Screen2Stream(colordepth,colormode,quality,stream);
  stream.Position := 0;
  j := TJpegImage.Create;
  j.LoadfromStream(stream);
  image1.picture.bitmap.assign(j);
  j.free;
  stream.Free;
end;


meikl
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Wolfie320Commented:
Declare your function like this...

procedure TFunctions.Screen2Stream(ColorDepth,ColorMode,Quality: Integer; var stream: TMemoryStream);

and the example from meikl will work.
0
Wolfie320Commented:
Declare your function like this...

procedure TFunctions.Screen2Stream(ColorDepth,ColorMode,Quality: Integer; var stream: TMemoryStream);

and the example from meikl will work.
0
kretzschmarCommented:
hi wolfie,

the stream must not be a var parameter,
because its just a pointer and this pointer will not be modified.

meikl
0
Wolfie320Commented:
hi meikl,

yes you're right, my mistake sorry! :)

wolfie
0
bryan7Author Commented:
hi meikl.. I already found it myself..
though I did  image1.picture.assign(j);
in stead of  image1.picture.bitmap.assign(j);

anyway the points are yours..

thanks both
0
kretzschmarCommented:
hi bryan,

glad to helped you
good luck again

meikl ;-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Graphics Software

From novice to tech pro — start learning today.