Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I set Image control color depth?

783 views
Skip to first unread message

DelphiUser4Ever

unread,
Sep 11, 2005, 8:08:06 PM9/11/05
to

How do I get an Image control set down to 256 colors
even if the computer is set on high color?

..


Ronaldo Souza

unread,
Sep 11, 2005, 8:07:09 PM9/11/05
to
DelphiUser4Ever wrote:
>
> How do I get an Image control set down to 256 colors
> even if the computer is set on high color?
>

You can set the PixelFormat to pf8bit but the result may not be good.
If you need higher quality, you'll have to create an optimized palette.
For this, use the GifImage lib written by Anders Melander and modified
by Finn Tolderlund to fix compatibility issues with Delphi 6 and 7.

It can be downloaded from Finn's site:

http://home20.inet.tele.dk/tolderlund/delphi/

http://finn.mobilixnet.dk/delphi/

//-----------------------------------------------
uses
GIFImage;

procedure TForm1.Button1Click(Sender: TObject);
var
BMP1,BMP2 : TBitmap;
begin
BMP1 := TBitmap.Create;
try
BMP1.LoadFromFile('24bit.bmp');
if (BMP1.PixelFormat > pf8bit)
then begin
BMP2 := ReduceColors(BMP1,rmQuantizeWindows,dmNearest,8,0)
try
//Do whatever you need with BMP2
BMP2.SaveToFile('8bit.bmp');
finally
BMP2.Free;
end;
end
else ShowMessage('Image already 8bit or less');
finally
BMP1.Free;
end;
end;
//-----------------------------------------------

HTH,
Ronaldo

DelphiUser4Ever

unread,
Sep 11, 2005, 8:35:16 PM9/11/05
to
Ronaldo Souza wrote:
> DelphiUser4Ever wrote:
>
>>
>> How do I get an Image control set down to 256 colors
>> even if the computer is set on high color?
>>
>
> You can set the PixelFormat to pf8bit but the result may not be good.
> If you need higher quality, you'll have to create an optimized palette.
> For this, use the GifImage lib written by Anders Melander and modified
> by Finn Tolderlund to fix compatibility issues with Delphi 6 and 7.

My Image control just gets text printed on it. It doesn't
load any pictures, or even uses them.
The control itself needs to be set, not the picture within it.

Ronaldo Souza

unread,
Sep 11, 2005, 8:27:24 PM9/11/05
to
> My Image control just gets text printed on it. It doesn't
> load any pictures, or even uses them.
Not sure why bother with the number of colors (PixelDepth) then...


> The control itself needs to be set, not the picture within it.

Are you saving the image to file?

Please show us some code.

Ronaldo

Jens Gruschel

unread,
Sep 11, 2005, 9:03:45 PM9/11/05
to
>>> How do I get an Image control set down to 256 colors
>>> even if the computer is set on high color?
>>>
> My Image control just gets text printed on it. It doesn't
> load any pictures, or even uses them.
> The control itself needs to be set, not the picture within it.

You cannot change the pixel format of a control. You can only change the
pixel format of a bitmap, which might be displayed by some control like
a TImage. If your TImage does display a bitmap, simply change its pixel
format. But it's hard to tell you more without seeing some code or a
better explaination what you're trying to do, especially since what you
want to do looks a bit strange to me. Why would you want to do this? And
isn't there a better way to print text to a control than using a TImage?

Jens

--
Jens Gruschel
http://www.pegtop.net

DelphiUser4Ever

unread,
Sep 12, 2005, 12:55:46 AM9/12/05
to
It's a huge scrollable bitmap in a window. Bitmap is
24,800 pixels high and takes up too much memory in
32 bit. When I have settings at 16 bit color I can go
as high as 100,000 pixels for the bitmap, which is way
more than I need. Text is manipulated anywhere on the
bitmap and can interact with graphics.

The problem is, the Delphi Image control takes on the resolution
that Windows is set in. Lowering it from 32 to 16 bit is
much helpful. 256 color might also be useful if text
still looks good. since it's a bitmap, graphics can go
anywhere quickly. The text is also scrollable easily without
all the hard work when a Image control is within a scrollable
form.

with Form1.Image1.canvas do begin
font.name := Settings.LongFontUsed;
font.size := Settings.LongFontSize;
font.color := clwhite;
brush.style := bssolid;
brush.color := clwhite;
TextOut(x,y, FileString[i]);
end;

Ronaldo Souza

unread,
Sep 12, 2005, 2:58:54 AM9/12/05
to
DelphiUser4Ever wrote:
> It's a huge scrollable bitmap in a window. Bitmap is
> 24,800 pixels high and takes up too much memory in
> 32 bit. When I have settings at 16 bit color I can go
> as high as 100,000 pixels for the bitmap, which is way
> more than I need. Text is manipulated anywhere on the
> bitmap and can interact with graphics.
Are you sure you need this setup? Can't you use a TMemo or TRichEdit?


> The problem is, the Delphi Image control takes on the resolution
> that Windows is set in.

No, it does not. You can set the number of colors using the
Image.Picture.Bitmap.PixelFormat. If you want 256 colors, set it to
pf8bit. Please notice that there are also the pf4bit (16 colors) and
pf1bit (2 colors) formats.


> since it's a bitmap, graphics can go anywhere quickly. The text
> is also scrollable easily without all the hard work when a Image
> control is within a scrollable form.

Another approach: keep the text in a StringList; keep a list of the
graphics and it's coordinates in a TList; create a smaller BMP (the size
of the form's ClientRect) it will work as the "current page". Use the
ScrollBox's OnChange event and the Position property to find out the
first string to be written; write them until you run out of text or the
page is filled; search the list for graphics on the current page and
draw them.

>
> with Form1.Image1.canvas do begin
> font.name := Settings.LongFontUsed;
> font.size := Settings.LongFontSize;
> font.color := clwhite;
> brush.style := bssolid;
> brush.color := clwhite;
> TextOut(x,y, FileString[i]);
> end;

Not really sure what I'm doing but I *think* this sample code using your
setup (if I undertstood it...) but controlling the number of colors. Add
a ScrollBox to a form and set it's Align property to alClient. Add an
Image inside the ScrollBox and set it's Align property to alClient. Add
an OnCreate event to the form:

procedure TForm1.FormCreate(Sender: TObject);
var
i,Y,dY : integer;
begin
Image1.Picture.Bitmap := TBitmap.Create;
Image1.Picture.Bitmap.PixelFormat := pf16bit; //pf4bit
Image1.Picture.Bitmap.Width := Image1.Width;
Image1.Picture.Bitmap.Height := 24800;
Image1.Picture.Bitmap.Canvas.Brush.Color := clBlue;

Image1.Picture.Bitmap.Canvas.FillRect(Image1.Picture.Bitmap.Canvas.ClipRect);
Image1.Picture.Bitmap.Canvas.Font.Color := clYellow;
SetBkMode(Image1.Picture.Bitmap.Canvas.Handle,TRANSPARENT);
ScrollBox1.VertScrollBar.Range := Image1.Picture.Bitmap.Height;
dY := Round(Image1.Picture.Bitmap.Canvas.TextHeight('Yy')*1.1);
Y := 0;
i := 0;
repeat
inc(i);
Image1.Picture.Bitmap.Canvas.TextOut(0,Y,IntToStr(i));//FileString[i]
inc(Y,dY);
until (Y > Image1.Picture.Bitmap.Height);
Image1.Picture.Bitmap.SaveToFile('C:\BMP256c.bmp'); //'C:\BMP16c.bmp'
end;


Please take some time to figure out if the huge bitmap approach is
really the best possible.

HTH,
Ronaldo

Ronaldo Souza

unread,
Sep 12, 2005, 3:02:04 AM9/12/05
to
Oops...
> Image1.Picture.Bitmap.PixelFormat := pf16bit; //pf4bit

shoud read
Image1.Picture.Bitmap.PixelFormat := pf8bit; //pf4bit

Ronaldo

Message has been deleted

Ronaldo Souza

unread,
Sep 13, 2005, 12:12:31 AM9/13/05
to
piddy wrote:
> ...I'm again a warm & happy! ...
>
Glad to hear that!
Now relax and have a beer... :-)
Good luck,
Ronaldo

0 new messages