Jpeg to stream: 8bit and grayscale doesn't work

I use this to capture the desktop, pass it to a stream,
and see the image size. Somehow, if I use 8 bit
color mode or grayscale I get 0 as the stream size..

what's wrong ?

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?
 
Wolfie320Connect With a Mentor Commented:
You don't need to save the jpeg into another stream just set the property before the assing part of the code.

Like this...

if ColorDepth = 24 then a.PixelFormat:= jf24Bit else a.PixelFormat:= jf8Bit;
if Colormode = 1 then a.Grayscale:= true;
a.CompressionQuality:= Quality;
a.Assign(b.picture.bitmap);

This will work just fine.

Wolfie
0
 
kretzschmarCommented:
hi bryan,

hmm, i guess adjustments on that propertys will cause a discarding of the current jpeg-image.

do just before you adjust the settings save the jpeg into a stream, adjust the settings and reload it from the stream.

meikl
0
 
Wolfie320Commented:
You don't need to save the jpeg into another stream just set the property before the assing part of the code.

Like this...

if ColorDepth = 24 then a.PixelFormat:= jf24Bit else a.PixelFormat:= jf8Bit;
if Colormode = 1 then a.Grayscale:= true;
a.CompressionQuality:= Quality;
a.Assign(b.picture.bitmap);

This will work just fine.

/Tommy
0
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 
Wolfie320Commented:
Found another problem in the function.
You will lose your stream if you don't declare the function like this...

procedure Screen2Stream(ColorDepth,ColorMode,Quality: Integer; var stream: TMemoryStream);
0
 
Wolfie320Commented:
Oops! sorry declare the Procedure.... :)
0
 
bryan7Author Commented:
hi wolfie thanks it worked  just setting the property before the assing part.. though it doesn't seem to change by setting 8bit color..only the grayscale..

anyway, I found a demo in extras folder of delphi, which shows how to use jpeg, and it loads an image and lets you change the properties, and I don't see it changing by setting 8bit color in stead of 24.. is that normal ?

about this: "procedure Screen2Stream(ColorDepth,ColorMode,Quality: Integer; var stream: TMemoryStream)"

why do I need to put var ? I don't do it and it works fine.. I'm using delphi3

btw, points are yours..

thanks both
0
 
Wolfie320Commented:
hi bryan7

You don't need to add the var in your function I was abit tired when I wrote that, sorry!

I'm not sure why 8bit doesen't work I will check it out later today.

/wolfie
0
 
kretzschmarCommented:
hi wolfie,

you are forced to answer this q, do it.
congrates to your first expert-points.

meikl ;-)
0
 
Wolfie320Commented:
I'm new to this site sorry if I mess things up here ;)

Wolfie
0
 
bryan7Author Commented:
have you been able to check the 8bit thing ?
0
 
Wolfie320Commented:
Yes I did,

I don't know if you want to use this property to make the file smaller, but the PixelFormat property is used for decompression only, the file size will be the same with both 8bit and 24bit.

Set the PixelFormat property to 8bit for video drivers that cannot display 24bit. The native format of a JPEG image is 24bit.

Wolfie
0
 
bryan7Author Commented:
oh, yes, I wanted to make the size smaller since I'm sending the jpeg stream through tcp/ip..

thanks for all
0
All Courses

From novice to tech pro — start learning today.