Tell me how to trim a PNG image using an arbitrary method using GDI +?
There is a working version of the cropping, but without transparency, and with a monochrome background, I want to create a universal solution so that we get transparency at the output, where we don’t need a picture, for example:
function AACropGDI(const aSrcPic: TPicture; const aRect: TRect; const aPath: IGPGraphicsPath): TPicture; var graphics: IGPGraphics; pathPen: IGPPen; gpBmp: IGPBitmap; vclBmp: TBitmap; trColor: TColor; begin vclBmp:= TBitmap.Create; try vclBmp.Assign(aSrcPic.Graphic); // converting to BMP Result:= TPicture.Create; // creating result object Result.Bitmap.Assign(vclBmp); // copy trColor:= clWhite; Result.Bitmap.TransparentColor:= trColor; pathPen:= TGPPen.Create(TGPColor.CreateFromColorRef(ColorToRGB(Result.Bitmap.TransparentColor))); // pen gpBmp:= TGPBitmap.Create(vclBmp.Handle, vclBmp.Palette); // GDI+ bmp to draw graphics:= TGPGraphics.Create(Result.Bitmap.Canvas.Handle); // GDI+ canvas //graphics.Clear(TGPColor.CreateFromColorRef(ColorToRGB(Result.Bitmap.TransparentColor))); // clearing by transparent color graphics.SmoothingMode:= SmoothingModeAntiAlias; //ANTIALIASED graphics.SetClip(aPath); // setting path border graphics.DrawImage(gpBmp, TGPRectF.Create(0, 0, aRect.Width, aRect.Height)); graphics.DrawPath(pathPen, aPath); // drawing an antialiasied path finally vclBmp.Free; end; end; procedure TForm1.Button2Click(Sender: TObject); var pic, picCroped: TPicture; circle: IGPGraphicsPath; begin pic:= TPicture.Create; try pic.LoadFromFile('D:\image.jpg'); Image1.Picture.Assign(nil); Image1.Picture.Bitmap.SetSize(pic.Width, pic.Height); circle:= TGPGraphicsPath.Create(FillModeWinding); circle.AddEllipse(TGPRectF.Create(0, 0, Image1.Width - 1, Image1.Height - 1)); picCroped:= AACropGDI(pic, Rect(0, 0, Image1.Width, Image1.Height), circle); Image1.Canvas.Draw(0, 0, picCroped.Graphic); Image1.Transparent:= True; finally pic.Free; picCroped.Free; end; end; Problems begin when the picture is drawn on top of another (there is a background), or it must be transparent. In the end, I want to get something like:
Required result:


