Dismiss Notice

REGISTRATION IS AFTER ADMIN ACTIVATION

DONATIONS WITH PAYPAL CLICK TO BUTTON

3 MONTHS VIP - 10$; 6 MONTHS VIP - 20$; 1 YEAR VIP - 30$; 2 YEARS VIP - 50$; GOLD USER FOR LIFE VIP - 150$

DONATIONS WITH Bitcoin Address:3NRs3CK3fhXifrNYxHEZKpETDd9vNLMsMD

Dismiss Notice
The registration is approved by the Administrator. It takes about 1 day to approve your registration
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 N0body, Feb 17, 2014.

  1. N0body
    Online

    N0bodyN0body is a Verified Member DelphiFan Administrator 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

Laws
Our website, law, laws, copyright and aims to be respectful of individual rights. Our site, as defined in the law of 5651 serves as a provider of space. According to the law, contrary to the law of site management has no obligation to monitor content. Therefore, our site has adopted the principle of fit and remove. Works are subject to copyright laws and is shared in a manner that violated their legal rights, or professional associations, rights holders who, adsdelphi@gmail.com I can reach us at e-mail address. Complaints considered to be infringing on the examination of the content will be removed from our site.
Donate Help To Us and Be VIP
DONATIONS WITH PAYPAL CLICK TO BUTTON
6 MONTHS VIP - 20$; 1 YEAR VIP - 30$; 2 YEARS VIP - 50$; GOLD USER FOR LIFE VIP - 150$
Social Block