FMXTBitmapDataPixel (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example shows how to use the TBitmapData.GetPixel and TBitmapData.SetPixel methods. This sample draws and fills the rectangle on an image, pixel by pixel.

To build and test this example, create a Multi-Device Application - Delphi, then add the following objects to the form:

  • A TImage to display the TBitmap.
  • A TLabel to display the value of the TTrackBar.
  • A TTrackBar to control the amount of Red in relation to which the transparency of certain pixels changes.

Add the following code to the OnChange event handlers of the track bar.

Code

void __fastcall TForm1::FormCreate(TObject *Sender) {
	MyBitmap = new TBitmap(0, 0);
	MyBitmap->Assign(Image1->Bitmap);
}

Add the following code to the OnChange event handlers of the track bar.

Code

void __fastcall TForm1::TrackBar1Change(TObject *Sender) {
	TBitmap *MyBitmapCopy;
	int X, Y;
	TBitmapData CurrentData, ModifiedData;
	TAlphaColorRec ColorRec;
	MyBitmapCopy = new TBitmap(0, 0);
	MyBitmapCopy->Assign(MyBitmap);
	Label1->Text = IntToStr(int(TrackBar1->Value));
	__try {
	// Changes the color of certain pixels.
        // This changes the transparence of the pixels that have the amount of Red.
	// lower then the curent value of the trackbar
		if (MyBitmap->Map(TMapAccess::Read, CurrentData) && MyBitmapCopy->Map
			(TMapAccess::Write, ModifiedData)) {
			__try {
			   for (X = 0; X < MyBitmap->Width; X++) {
			      for (Y = 0; Y < MyBitmap->Height; Y++) {
			 	ColorRec.Color = CurrentData.GetPixel(X, Y);
				   if (ColorRec.R < StrToInt(Label1->Text)) {
					//255 is the maximum value for a color channel
					ColorRec.A = 255 - StrToInt(Label1->Text);
					ModifiedData.SetPixel(X, Y, ColorRec.Color);
					}
					}
				}
			}
			__finally {
				MyBitmap->Unmap(CurrentData);
				MyBitmapCopy->Unmap(ModifiedData);
			}
		}
		Image1->Bitmap = MyBitmapCopy;
	}
	__finally {
		delete MyBitmapCopy;
	}
}

The result should look like the following image:

TBitmapDataPixel.png

Uses

See Also

Personal tools
Assistance