Working with TCameraComponent
I am trying to resize a captured image TCameraComponent
using the following code:
procedure TForm1.GetImage;
begin
imagec.SampleBufferToBitmap(img.Bitmap, True);
with resizedimg.Bitmap do // Resize the image to another bitmap
begin
SetSize(300, 160);
if Canvas.BeginScene then
try
Canvas.DrawBitmap(img.Bitmap, TRectF.Create(0, 0, 300, 160), TRectF.Create(0, 0, 300, 160), 1.0);
finally
Canvas.EndScene;
end;
end;
end;
But every time I turn off the camera and reopen it, the resized image captures the enlarged portion of the actual one TImage
. Why is this behavior happening? What am I doing wrong?
The goal is to resize it img.Bitmap
to 300x160 pixels.
source to share
The second parameter DrawBitmap()
should be the original size img.Bitmap
that is being drawn, not the size that you are trying to resize.
Canvas.DrawBitmap(img.Bitmap, TRectF.Create(0, 0, img.Bitmap.Width, img.Bitmap.Height), TRectF.Create(0, 0, 300, 160), 1.0);
In Berlin and later it TBitmap
has a property BoundsF
that you can use instead.
Canvas.DrawBitmap(img.Bitmap, img.Bitmap.BoundsF, TRectF.Create(0, 0, 300, 160), 1.0);
source to share