Link to home
Start Free TrialLog in
Avatar of bryan7
bryan7Flag for Japan

asked on

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;
Avatar of kretzschmar
kretzschmar
Flag of Germany image

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
Avatar of Wolfie320
Wolfie320

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
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);
Oops! sorry declare the Procedure.... :)
Avatar of bryan7

ASKER

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
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
hi wolfie,

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

meikl ;-)
ASKER CERTIFIED SOLUTION
Avatar of Wolfie320
Wolfie320

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm new to this site sorry if I mess things up here ;)

Wolfie
Avatar of bryan7

ASKER

have you been able to check the 8bit thing ?
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
Avatar of bryan7

ASKER

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

thanks for all