Jump to content
FranzB

Draw TBitmap

Recommended Posts

I want  to insert a bitmap on it's own left side again using canvas.drawbitmap function 

but actually this code does not do anything..... can't see my mistake here  😞 

 

 

procedure InsertonMyleft(aBMP : TBitmap);
var
  tempBMP: TBitmap;
  RecFull: TRectF;
  RecLeft: TRectF;
begin
  tempBMP := TBitmap.Create;

  try

    aBMP.Canvas.BeginScene;

    tempBMP.Assign(localBMP);

    tempBMP.SaveToFile('c:\temp\debugme.bmp');

    RecFull := RectF(0, 0, aBMP.Width, aBMP.Height);

    RecLeft := RectF(0, 0, round(aBMP.Width / 3),
      round(aBMP.Height / 1));

    aBMP.Canvas.DrawBitmap(tempBMP, RecFull, RecLeft, 50, True);

    aBMP.Canvas.EndScene;

    aBMP.SaveToFile('c:\temp\debugme2.bmp');

  finally

    tempBMP.Free;

  end;
end;


 

Edited by FranzB

Share this post


Link to post

Where is localBMP coming from? You don't really need to make a copy of a TBitmap just to draw it, so you could just use localBMP as-is and get rid of tempBMP.  Unless localBMP is actually supposed to be aBMP instead (typo?), then using tempBMP makes more sense if you are trying to draw a TBitmap onto itself.

 

When calling DrawBitmap(), RecFull is supposed to be a rectangle within the TBItmap that you are drawing from, but you are setting it to the rectangle of the TBitmap that you are drawing onto.  Change aBMP to tempBMP when populating RecFull.

Edited by Remy Lebeau

Share this post


Link to post

 

I changed  the code  to make it a full separate  process function  like below ,

Trect full has correct size  0,0,500,500

and Trect left size is 0,0,250,500

image size is 501 x501 pixel

but still no effect from this code 😞 

 

 

 

procedure InsertonMyleft(aBMP : TBitmap);
var
  tempBMP: TBitmap;
  RecFull: TRectF;
  RecLeft: TRectF;
begin
  tempBMP := TBitmap.Create;

  try

    aBMP.Canvas.BeginScene;

    tempBMP.Assign(aBMP);

    tempBMP.SaveToFile('c:\temp\debugme.bmp');

    RecFull := RectF(0, 0, aBMP.Width-1, aBMP.Height-1); //   size  of full trect, 0,0,500,500 

    RecLeft := RectF(0, 0, round((aBMP.Width-1) / 2),   //   just lest side 
      round((aBMP.Height-1) / 1));

    aBMP.Canvas.DrawBitmap(tempBMP, RecFull, RecLeft, 50, True);

    aBMP.Canvas.EndScene;

    aBMP.SaveToFile('c:\temp\debugme2.bmp');

  finally

    tempBMP.Free;

  end;
end;

 

Share this post


Link to post

I think 0 ... 500 is not correct.

Your ABmp seems to have 501 pixel with in the first place.

Maybe forgot to set with-1 and height-1 in tue caller of the function ?

Share this post


Link to post

already changed to width-1 ...... 

 

see code above 

 

RecFull := RectF(0, 0, aBMP.Width-1, aBMP.Height-1); //   size  of full trect, 0,0,500,500

Share this post


Link to post

Assign to tempBMP outside of BeginScene / EndScene?

procedure TForm1.InsertOnMyLeft(aBMP: TBitmap);
var
  tempBMP: TBitmap;
  RecFull: TRectF;
  RecLeft: TRectF;
begin
  tempBMP := TBitmap.Create;
  try
    tempBMP.Assign(aBMP); // <--
    aBMP.Canvas.BeginScene;
    RecFull := RectF(0, 0, aBMP.Width-1, aBMP.Height-1);
    RecLeft := RectF(0, 0, Round((aBMP.Width-1) / 2), Round((aBMP.Height-1) / 1));
    aBMP.Canvas.DrawBitmap(tempBMP, RecFull, RecLeft, 0.5, True);
    aBMP.Canvas.EndScene;
  finally
    tempBMP.Free;
  end;
end;


 

Edited by Gustav Schubert

Share this post


Link to post
procedure  InsertonMyleft(aBMP: TBitmap);
var
  tempBMP: TBitmap;
  RecFull: TRectF;
  RecLeft: TRectF;
begin
  tempBMP := TBitmap.Create;

  try

    // tempBMP.Assign(aBMP);

    // tempBMP.SaveToFile('c:\temp\debugme.bmp');

    tempBMP.LOadfromFile('c:\temp\debugme3.bmp');

    RecFull := RectF(0, 0, aBMP.Width - 2, aBMP.Height - 2);

    RecLeft := RectF(0, 0, round(aBMP.Width / 3), round((aBMP.Height - 1) / 1));

    aBMP.Canvas.BeginScene;

    aBMP.Canvas.DrawBitmap(tempBMP, RecFull, RecLeft, 50, True);

    aBMP.Canvas.EndScene;

    aBMP.SaveToFile('c:\temp\debugme2.bmp');

  finally

    tempBMP.Free;

  end;
end;

 

if I load a bitmap from  a file , my code  works fine 

but the assign sequence does not have  the desired function, (insert the bmp on the left side)

even the savetofile saved the correct bitmap content 
 

Share this post


Link to post
9 hours ago, FranzB said:

but the assign sequence does not have  the desired function, (insert the bmp on the left side)

The Opacity parameter should be between 0 and 1.
RectF is not the problem, even if specified too large.
A stretched version of the original (aBMP) is drawn on top of aBMP, as expected.
I can see it in an Image component, and in the file on disk.
TempBMP, a copy of aBMP is needed it seems, ok.
I noticed that the copy of aBMP needs to be made outside of the BeginScene/EndScene block, ok?

 

Can you reproduce a problem without saving to disk?
What exactly is the problem - how do you show the image?

    { rectangle setup }
    SrcFull := RectF(0, 0, tempBMP.Width, tempBMP.Height);
    DstLeft := RectF(0, 0, aBMP.Width / 3, aBMP.Height);
    aBMP.Canvas.DrawBitmap(tempBMP, SrcFull, DstLeft, 1.0, True);


 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×