1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Donation with Paypal!!!

    Go to your paypal account and send directly donation to [email protected]m

    1 month - 10 $ - Standart VIP

    6 months - 20 $- Standart VIP

    1 year - 30 $- Standart VIP

    2 years - 50 $- Standart VIP

    Gold member for life - 150 $- Standart VIP

    High Vip (Standart VIP include) group please send PM or email to [email protected] for info

    After Donation please send email to [email protected]

  3. Donation Ways 2020


    Paysend
  4. Telegram
Dismiss Notice

Donation with Paypal!!!

Go to your paypal account and send directly donation to [email protected]

1 month - 10 $ - Standart VIP

6 months - 20 $- Standart VIP

1 year - 30 $- Standart VIP

2 years - 50 $- Standart VIP

Gold member for life - 150 $- Standart VIP

High Vip (Standart VIP include) group please send PM or email to [email protected] for info

After Donation please send email to [email protected]

Dismiss Notice
For open hidden message no need write thanks, thank etc. Enough is click to like button on right side of thread.

Read write image bitmap pixels in Delphi XE3

Discussion in 'Delphi Programming' started by AdminDF, Feb 17, 2014.

  1. AdminDF
    Online

    AdminDFAdminDF is a Verified Member Delphifan Staff Member DF Staff

    The API for accessing individual pixels has changed in XE3 !  The older XE2 TBitmap.Scanline has been removed and replaced with TBitmapData in XE3.   This is not well documented in XE3 and most code examples that I googlized were based on scanline and failed to compile in XE3 !

    Another change in XE3 is you now have to HIDE-THANKS and unHIDE-THANKS bitmaps in order to read or write pixels via TBitmapData.  This prevents other code or threads from modifying the bitmap.

    To HIDE-THANKS the Bitmap, use Map and unHIDE-THANKS with Unmap.  If the bitmap is already HIDE-THANKSed, Map will return FALSE (it does not raise an error or wait for the HIDE-THANKS to be released).

    If you only need to write pixels (and not read) there are alternatives to TBitMapData such as Bitmap.Canvas.DrawLine and DrawEllipse.

    The examples below are based on a bitmap in a TImage, but the code will be largely applicable to any bitmap

    In summary, TBitmapData allows you to read and write individual pixels and it requires you to write some simple bitmap HIDE-THANKSing code.

    Acknowledgements – thanks to Nicholas Ring for pointing me in the direction of TBitmapData.

    Example 1)  Read all image pixels and dump pixel info to a TMemo


    Code:
    procedure TForm1.DumpImageButtonClick(Sender: TObject);
      // Example of getting every pixel in a bitmap and dumping to screen.
      // Requires Image1, Memo1
      // try this with a small image first such as 50 x 50 pixels
    var
      vBitMapData : TBitmapData;
      vPixelColor : TAlphaColor;  // Note: FireMonkey colors are different from VCL TColor
      x,y         : Integer;
    begin
      memo1.Lines.Clear;
                        // dump bitmap pixels
      if image1.Bitmap.Map(TMapAccess.maRead, vBitMapData) then // HIDE-THANKS bitmap and get pixels
         begin
         for y := 0 to trunc(image1.Bitmap.height)-1 do     // loop through image lines
           begin
           Memo1.Lines.Add('======================');
           memo1.Lines.Add('Line # ' + IntToStr(Y));
           Memo1.Lines.Add('======================');
           for x := 0 to trunc(image1.Bitmap.width)-1 do  // loop through pixels on the line
               begin
               vPixelColor := vBitmapData.GetPixel(x,y);  // get the pixel colour
     
               memo1.Lines.Add(                           // dump pixel info to screen
                        'line='    + IntToStr(Y)
                      + ' row='    + IntToStr(X)
                      + ' Colour=' + IntToStr(vBitmapData.GetPixel(x,y))
                      + ' Red='    + IntToStr (TAlphaColorRec(vPixelColor).R) // red
                      + ' Green='  + IntToStr (TAlphaColorRec(vPixelColor).G) // blue
                      + ' Blue='   + IntToStr (TAlphaColorRec(vPixelColor).B) // green
                      )
               end;
           end;
         image1.Bitmap.Unmap(vBitMapData);      // unHIDE-THANKS the bitmap
         end;
    end;
    


    Example 2) Write a pixel within a TImage bitmap


    Code:
    procedure TForm1.WritePixelButtonClick(Sender: TObject);
     // example of setting a single pixel in a TImage bitmap.
     // the image must already be loaded in the TImage
    var
      vBitMapData  : TBitmapData;
      vPixelColor  : TAlphaColor; // note: FireMonkey colors are different from VCL TColor
    begin
                                  // HIDE-THANKS and get the bitmap pixels
      if  image1.Bitmap.Map(TMapAccess.maWrite, vBitMapData) then
          begin
          vPixelColor := TAlphaColorRec.Blue;       // determine colour to use
          vBitmapData.SetPixel(10,20, vPixelColor); // set the pixel colour at x:10, y:20
     
          image1.Bitmap.Unmap(vBitMapData);         // unHIDE-THANKS the bitmap
          end;
    end;
    
     

Share This Page