Главная страница
    Top.Mail.Ru    Яндекс.Метрика
Форум: "Media";
Текущий архив: 2004.11.28;
Скачать: [xml.tar.bz2];

Вниз

Как изменить палитру TBitmap   Найти похожие ветки 

 
bug008   (2004-08-27 16:12) [0]

Здравствуйте.
Если у битмапа установить свойства:
Bitmap.HandleType := bmDIB;
Bitmap.PixelFormat := pf8bit;
То при отображениии картинки будет использоваться палитра, которая поумолчанию содержит 16 системных цветов, все остальные элементры палитры черный цвет.
Как поменять палитру так, чтобы были только цвета градации серого (0 - черный, 255 - белый)?
Спасибо.


 
Анонимщик ©   (2004-08-27 16:39) [1]

В справке написано:

Objects

The example demonstrates using the TBitmap object.

Running

To run this example, load the project file from the Help\Examples\Bitmap directory.

Description

This example demonstrates changing the palette of a TBitmap object. A TBitmap is an encapsulation of the windows BITMAP and PALETTE which manages realizing of the palette automatically. The bitmap can be loaded from a .BMP file (LoadFromFile method) or a resource (LoadFromResourceName or LoadFromResourceID method) and saved back to a file (SaveToFile method).  It can be drawn on a canvas by using the TCanvas" Draw or StretchDraw method.  The size of the bitmap can be determined by using the Height and Width properties of TBitmap.

The example below illustrates the use of TBitmap’s Palette, LoadFromFile, Draw, Height, and Width. Be sure that a small bitmap file named bor6.bmp is present in the same directory as the .exe file.

Disclaimer

VCL Reference
TBitmap example (bmpformu.pas)

bmpformu.dfm:

object BmpForm: TBmpForm

 Left = 311
 Top = 321
 Width = 466
 Height = 334
 Caption = "BmpForm"
 Font.Charset = DEFAULT_CHARSET
 Font.Color = clWindowText
 Font.Height = -11
 Font.Name = "MS Sans Serif"
 Font.Style = []
 OnCreate = FormCreate
 OnDestroy = FormDestroy
 OnPaint = FormPaint
 PixelsPerInch = 96
 TextHeight = 13
 object Button1: TButton
   Left = 56
   Top = 40
   Width = 113
   Height = 25
   Caption = "Scramble Palette"
   TabOrder = 0

   OnClick = Button1Click
 end
end

This is an example of using TBitmaps and is not intended to be an efficient method of tiling a form.

unit bmpformu;

//
interface
uses
 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
 StdCtrls;
type
 TBmpForm = class(TForm)
   Button1: TButton;
   procedure FormDestroy(Sender: TObject);
   procedure FormPaint(Sender: TObject);
   procedure Button1Click(Sender: TObject);
   procedure FormCreate(Sender: TObject);
 private
   { Private declarations }
   Bitmap: TBitmap;
   procedure ScrambleBitmap;

   procedure WMEraseBkgnd(var m: TWMEraseBkgnd); message WM_ERASEBKGND;
 public
   { Public declarations }
 end;
var
 BmpForm: TBmpForm;
implementation

{$R *.DFM}

procedure TBmpForm.FormCreate(Sender: TObject);
begin
 Bitmap := TBitmap.Create;
 Bitmap.LoadFromFile("bor6.bmp");
end;
procedure TBmpForm.FormDestroy(Sender: TObject);
begin
 Bitmap.Free;
end;
// since we"re going to be painting the whole form, handling this

// message will suppress the uneccessary repainting of the background
// which can result in flicker.
procedure TBmpform.WMEraseBkgnd(var m : TWMEraseBkgnd);
begin
 m.Result := LRESULT(False);
end;
procedure TBmpForm.FormPaint(Sender: TObject);
var
 x, y: Integer;
begin
 y := 0;
 while y < Height do
 begin
   x := 0;
   while x < Width do
   begin
     Canvas.Draw(x, y, Bitmap);

     x := x + Bitmap.Width;
   end;
   y := y + Bitmap.Height;
 end;
end;
procedure TBmpForm.Button1Click(Sender: TObject);
begin
 ScrambleBitmap;
 Invalidate;
end;

//
// scrambling the bitmap is easy when it has 256 colors:
// we just need to change each of the colors in the palette
// to some other value.
//
procedure TBmpForm.ScrambleBitmap;
var
 pal: PLogPalette;
 hpal: HPALETTE;

 i: Integer;
begin
 pal := nil;
 try
   GetMem(pal, sizeof(TLogPalette) + sizeof(TPaletteEntry) * 255);
   pal.palVersion := $300;
   pal.palNumEntries := 256;
   for i := 0 to 255 do
   begin
     pal.palPalEntry[i].peRed := Random(255);
     pal.palPalEntry[i].peGreen := Random(255);
     pal.palPalEntry[i].peBlue := Random(255);
   end;
   hpal := CreatePalette(pal^);
   if hpal <> 0 then

     Bitmap.Palette := hpal;
 finally
   FreeMem(pal);
 end;
end;
end.

И еще:

TBitmap.PixelFormat

Indicates the bit format of the bitmap image, specifying how the image is displayed and how the pixels of the bitmap image are stored in memory.

type TPixelFormat = (pfDevice, pf1bit, pf4bit, pf8bit, pf15bit, pf16bit, pf24bit, pf32bit, pfCustom);
property PixelFormat: TPixelFormat;

Description

Use PixelFormat to change a TBitmap"s internal image to a particular memory format and color depth, or to find out what memory format and color depth a TBitmap is using.

For example, PixelFormat can be used to set the pixel format of the bitmap image to 8-bit for video drivers that cannot display the native format of a bitmap image.

Note: The PixelFormat of a JPEG image object applies to the bitmap if the JPEG image is copied to it.

Changing the pixel format is most commonly used with ScanLine, because your code must decode the pixel data accessed by ScanLine. Image-editing tools usually use one pixel for all internal image operations and copy the results to the screen (in whatever format) as the last step.



Страницы: 1 вся ветка

Форум: "Media";
Текущий архив: 2004.11.28;
Скачать: [xml.tar.bz2];

Наверх





Память: 0.47 MB
Время: 0.035 c
14-1099938913
Piter
2004-11-08 21:35
2004.11.28
Поделитесь опытом удаленной работы


4-1098012867
volser
2004-10-17 15:34
2004.11.28
Генерация GUID


3-1099175813
Apophis
2004-10-31 02:36
2004.11.28
Сортировка в DBGrid...


14-1100242960
NewMan777
2004-11-12 10:02
2004.11.28
TDBF


1-1100268469
denis24
2004-11-12 17:07
2004.11.28
Изменения цвета окон встроенных диалогов





Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French
Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian
Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish Bengali Bosnian
Cebuano Esperanto Gujarati Hausa Hmong Igbo Javanese Kannada Khmer Lao Latin Maori Marathi Mongolian Nepali Punjabi Somali Tamil Telugu Yoruba
Zulu
Английский Французский Немецкий Итальянский Португальский Русский Испанский