Paul
Paul

Reputation: 26650

Paint on TGPImage programmatically

Could an TGPImage be loaded from PNG file or resource

himg := TGPImage.Create('heading.png');

and then be modified by painting on it like using canvas?

Or, better to say, I would like to paint a background using programmatic methods and then load an image from PNG above my painting in order to operate with this merged image as one solid TGPImage.

I looked at methods and properties of TGPImage and didn't find painting instruments.

Could I probably do this using TBitmap?

Following does not work:

_hbm := TBitmap.Create();
_hbm.Width := 1000;
_hbm.Height := 1000;
_hbm.Canvas.Brush.Color := clBlack;
_hbm.Canvas.Pen.Color := clBlack;
_hbm.Canvas.FillRect(Rect(0, 0, 1000, 1000));
_hgb := TGPBitmap.Create(_hbm.Handle);

....................

GPGraphics.DrawImage(_hgb, 0, 0, _hgb.GetWidth(), _hgb.GetHeight());

Upvotes: 0

Views: 780

Answers (1)

kobik
kobik

Reputation: 21242

You don't need a TBitmap for that.
You simply need to use a TGPGraphics associated with the TGPImage to draw on the TGPImage surface.

Here is a very simple example:

uses GDIPOBJ, GDIPAPI, GDIPUTIL;

procedure TForm1.Button1Click(Sender: TObject);
var
  b: TGPBitmap;
  g: TGPGraphics;
  pen: TGPPen;
  encoderClsid: TGUID;
begin
  b := TGPBitmap.Create('D:\in.png');
  try
    g := TGPGraphics.Create(b);
    try
      pen := TGPPen.Create(MakeColor(255, 255, 0), 3);
      try
        { Draw a yellow Rectangle }
        g.DrawRectangle(pen, MakeRect(0, 0, 200, 200));
        GetEncoderClsid('image/png', encoderClsid);
        b.Save('D:\out.png', encoderClsid);
      finally
        pen.Free;
      end;
    finally
      g.Free;
    end;
  finally
    b.Free;
  end;
end;

Upvotes: 4

Related Questions