I'm trying to crop an image in a rectangular area, to fill a circle with it, but I cannot find any way to do that. Tt says that the result of my code is not correct and the image is not well placed. I don't know what is wrong in my code.
I saw some examples , but they are not working for me.
Here are the sites I visited:
crop and align inserted BMP in Delphi
https://www.experts-exchange.com/questions/22142653/Crop-Bitmap-Image1.html
Delphi - how do I crop a bitmap "in place"?
Note: the code should be wrote in FMX whit C++, it should run in all platforms. The TCircle have the WrapMode assign to TileStrech.
void __fastcall TForm9::Button1Click(TObject *Sender)
{
int X , Y , W , H;
if(Image1->Bitmap->Width >= Image1->Bitmap->Height)
{
H = Image1->Bitmap->Height;
X = (Image1->Bitmap->Width - Image1->Bitmap->Height) / 2;
Y = 0;
W = H;
}
else
{
W = Image1->Bitmap->Width;
X = 0;
Y = (Image1->Bitmap->Width - Image1->Bitmap->Height) / 2;
H = W;
}
TRect rect(X, Y, W, H);
TBitmap * bm = new TBitmap(W,H);
bm->CopyFromBitmap(Image1->Bitmap ,rect ,X,Y);
Circle1->Fill->Bitmap->Bitmap->Assign(bm);
Image2->Bitmap->Assign(bm);
delete bm;
}
The result of this code is: The bitmap is not well placed in a Circle
My objetive is get an crop image like this : image croped
Solved
I have one solution for this problem
int CropBitmap(TBitmap* in, TBitmap* out)
{
if (!in or !out)
{
return 1;
}
TBitmap * bm = new TBitmap();
int origH = in->Height;
int origW = in->Width;
TRect rect;
int defH, defW;
if (origH > origW)
{
bm->Height = origW;
bm->Width = origW;
int factor = (origH - origW) / 2;
rect.Top = factor;
rect.Left = 0;
rect.Right = origW;
rect.Bottom = factor + origW;
bm->CopyFromBitmap(in, rect, 0, 0);
}
else if (origW > origH)
{
bm->Height = origH;
bm->Width = origH;
int factor = (origW - origH) / 2;
rect.Top = 0;
rect.Left = factor;
rect.Right = factor + origH;
rect.Bottom = origH;
bm->CopyFromBitmap(in, rect, 0, 0);
}
else
{
bm->Assign(in);
}
out->Assign(bm);
delete bm;
return 0;
}
It run well for me.