Drawing with TBGRABitmap Ubuntu 9.10 Laz 9.28.2-0 lazarus / 39-Graphics ---------------------------------------------------------------------- panoss · 2011-02-24 13:29 MouseMove over Image1: calls the procedure drawSelectedColor. (to draw on bmp, with the color under the mouse position). 1.When the MouseMove happens once, draws perfectly, with the Alpha as given from tbTrackBar.Position. 2.When MouseMove happens for the second time, the color on bmp (bmp: TBGRABitmap;) is darker (while tbTrackBar.Position (=Alpha) remains the same). 3.When MouseMove happens for the third, 4th etc time, bmp gets darker and darker (tbTrackBar.Position remains the same). BUT, if I click btnDestroyCreateClick, and do a MouseMove, then again draws correctly, correct Alpha. It's like if the code in btnDestroyCreateClick works, while the same code in drawSelectedColor does not work? What's happening? What Should I do? Code: [Select]procedure  myForm.Image1MouseMove((Shift: TShiftState; X, Y: integer);) begin      selectedColor := Image1.Canvas.Pixels[X,Y];      drawSelectedColor(selectedColor); end; procedure myForm.drawSelectedColor(col:TColor); var    r, g, b: Byte;    pxl: TBGRAPixel; begin      r := col;      g := col shr 8;      b := col shr 16;           bmp.destroy;          bmp := TBGRABitmap.Create(40,22);      pxl.blue:=b;      pxl.green:=g;      pxl.red:=r;      pxl.alpha:=tbTrackBar.Position;      bmp.Fill(pxl);      bmp.Draw(Canvas, 0, 0, false);    end;     procedure myForm.btnDestroyCreateClick(Sender: TObject); begin      bmp.destroy;          bmp := TBGRABitmap.Create(40,22); end;             bmp.Draw(Canvas, 0, 0, false); draws on the myForm.Canvas? And :  bmp.destroy;  destroys the bmp? (and not what is drawn on the  myForm.Canvas?). Am I correct? ---------------------------------------------------------------------- circular · 2011-02-24 20:15 Let's say the color is black with an alpha of 128. Each time it is drawn, it will darken the area. It's normal. It's hard to draw a transparent color ! What you can do for exemple is first draw a checkerboard of white and gray, and then do an alpha FillRect over it. Maybe when there is btnDestroyCreateClick the background is cleared. Anyway you don't need to Destroy and create again each time the bmp. The fill procedure replaces everything in the picture. ---------------------------------------------------------------------- panoss · 2011-02-25 06:52 I was right: the line 'bmp.Draw(Canvas, 0, 0, false)' DOES draw on the myForm.Canvas. Quote from: circular on February 24, 2011, 09:15:24 pmMaybe when there is btnDestroyCreateClick the background is cleared. So, when I click on the btnDestroyCreateClick, the Paint event of the form is eraised. So the part I had drawn, gets cleared! So, that clears the drawing and not the code of the btnDestroyCreateClick. Quote from: circular on February 24, 2011, 09:15:24 pmWhat you can do for exemple is first draw a checkerboard of white and gray, and then do an alpha FillRect over it. How do I do that? Quote from: circular on February 24, 2011, 09:15:24 pmThe fill procedure replaces everything in the picture. Yes, replaces everything on the bmp, you re right. But the drawing happens on the Canvas of the form(a small part of it). This part I must clear before redrawing. ---------------------------------------------------------------------- panoss · 2011-02-25 08:11 Ok, I found in Lazpaint the routine DrawCheckers!!! Code: [Select]procedure myForm.DrawCheckers(bmp: TBGRABitmap); const tx = 8; ty = 8; var xb,yb: integer;     oddColor, evenColor: TBGRAPixel; begin   oddColor := BGRA(220,220,220);   evenColor := BGRA(255,255,255);   for xb := 0 to (bmp.Width-1) div tx do    for yb := 0 to (bmp.Height-1) div ty do      if odd(xb+yb) then       bmp.FillRect(xb*tx,yb*tx,xb*tx+tx,yb*ty+ty,oddColor,dmSet) else         bmp.FillRect(xb*tx,yb*tx,xb*tx+tx,yb*ty+ty,evenColor,dmSet); end; So, the drawSelectedColor now becomes: Code: [Select]procedure myForm.drawSelectedColor(col:TColor); var    r, g, b: Byte;    pxl: TBGRAPixel; begin      r := col;      g := col shr 8;      b := col shr 16;           DrawCheckers(bmp);      bmp.AlphaFill(0);      bmp.Draw(Canvas, 0, 0, false);      pxl.blue:=b;      pxl.green:=g;      pxl.red:=r;      pxl.alpha:=tbTrackBar.Position;      bmp.Fill(pxl);      bmp.Draw(Canvas, 0, 0, false);   end; But, it's not working, something goes wrong   . If I replace the lines:      DrawCheckers(bmp);      bmp.AlphaFill(0);      bmp.Draw(Canvas, 0, 0, false); with:      Self.refresh; the problem gets solved. But, I don't thing that's the correct way to go. ---------------------------------------------------------------------- circular · 2011-02-25 09:45 Ok, here is the effect of the code : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i DrawCheckers(bmp);     bmp.AlphaFill(0);     bmp.Draw(Canvas, 0, 0, false);  This draws a checkerboard, then, make it totally transparent, and then draw it. But if it's totally transparent, it does not draw anything. Alpha 0 means transparent, 255 means opaque. Anyway, the checkerboard is already opaque. So you can simply write : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i DrawCheckers(bmp);     bmp.Draw(Canvas, 0, 0, true);  //use opaque drawing here (faster) But in fact, this requires two Draw on canvas, and one of these must be a transparent drawing. It's faster and more portable to do an opaque drawing. So instead, I recommend you to do a FillRect on the checkerboard, and only one opaque drawing at the end. ---------------------------------------------------------------------- panoss · 2011-02-25 10:24 Draws the color properly BUT I See the checkers under it !!! Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure myForm.drawSelectedColor(col:TColor);var   r, g, b: Byte;   pxl: TBGRAPixel;  begin     r := col;     g := col shr 8;     b := col shr 16;          DrawCheckers(bmp);          bmp.Draw(Canvas, 0, 0, true);        pxl.blue:=b;     pxl.green:=g;     pxl.red:=r;     pxl.alpha:=tbTrackBar.Position;     bmp.Fill(pxl);     bmp.Draw(Canvas, 0, 0, false);    end;  Sorry, I don't understand what you mean by this: Quote from: circular on February 25, 2011, 10:45:16 amSo instead, I recommend you to do a FillRect on the checkerboard, and only one opaque drawing at the end. ---------------------------------------------------------------------- panoss · 2011-02-25 13:18 I think you mean something like that: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i      DrawCheckers(bmp);     bmp.FillRect( 0, 0, bmp.Width, bmp.Height,pxl, dmSet);     bmp.Draw(Canvas, 4, 4, true);  //use opaque drawing here  It LOOKS ok, is that the correct? ---------------------------------------------------------------------- circular · 2011-02-25 15:55 Quote from: panoss on February 25, 2011, 11:24:16 amDraws the color properly BUT I See the checkers under it !!!Yes, that's on purpose. The screen cannot contain transparent pixels. There are always opaque. So if you want to represent an opaque color, you must draw it on something. Here on checkers. If the checkers is totally invisible, that means that the color is opaque. Quote from: panoss on February 25, 2011, 02:18:31 pmI think you mean something like that: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i      DrawCheckers(bmp);     bmp.FillRect( 0, 0, bmp.Width, bmp.Height,pxl, dmSet);     bmp.Draw(Canvas, 4, 4, true);  //use opaque drawing here  It LOOKS ok, is that the correct? That's right, except that the last parameter of FillRect must be dmDrawWithTransparency or dmFastBlend. dmSet replaces totally the pixels. ---------------------------------------------------------------------- panoss · 2011-02-25 17:06 Thank you for your help, finally I will let it with the checkers visible. Thank you very much my friend! You helped me a lot! ---------------------------------------------------------------------- circular · 2011-02-25 21:24 You're welcome. ---------------------------------------------------------------------- circular · 2011-03-04 20:40 Hello, I've created a test application for the last version of BGRABitmap. It's on LazPaint subversion: https://lazpaint.svn.sourceforge.net/svnroot/lazpaint in the folder testbgrafunc. I guess I should also write some tutorial.