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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
kretzschmarConnect With a Mentor Commented:
hi bryan,

glad to helped you
good luck again

meikl ;-)
0
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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
All Courses

From novice to tech pro — start learning today.