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?
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,

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
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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:
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

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
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
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
Delphi

From novice to tech pro — start learning today.