Delphi - Graphics32, draw multiple transparent PNG over JPG as watermak

試著忘記壹切 提交于 2019-12-13 01:40:23

问题


After read Graphics32 documentation I can't find a objetive example of use layers.

I just want to compose the following Image:

  • Layer 1 - Background Image (In JPG) (800x600)
  • Layer 2 - Transparent PNG as a frame border (800x600)
  • Layer 3 - Transparent PNG on right bottom with 25º Rotation (90x90)

And this is the expected result:

// uses => GR32, GR32_Layers, GR32_Png, GR32_Image;
procedure TMain.Button1Click(Sender: TObject);
var
// src, dest: TPNGObject; <-- another frustrating try
// r: TRect;
   bmp: TBitmap32;
   png: TPortableNetworkGraphic32;
   rlayer: TCustomLayer;
   img1, img2, img3: TImgView32;
begin
   bmp := TBitmap32.Create;
   bmp.Assign(imgPreview.Picture); // TImage obj already have a JPG loaded

   img1 := TImgView32.Create(nil);
   img1.Bitmap := bmp;

   img2 := TImgView32.Create(nil);
   img2.Bitmap.LoadFromFile('C:\\layer2.png');

   img3 := TImgView32.Create(nil);
   img3.Bitmap.LoadFromFile('C:\\watermark.png');

   rlayer := TCustomLayer.Create(nil);
   rlayer.LayerCollection.Add(img1.Layers.Items[0]); // [DCC Error]  Incompatible types: 'TLayerClass' and 'TCustomLayer' ????

   ...

How can I add a new layer to collection? And after all, how can I save this?


回答1:


The LayerCollection.Add method expects to receive a value of type TLayerClass. That is, it wants to receive the class, not an instance of a class. To satisfy the compiler, pass it literally TCustomLayer; the collection will instantiate the given class itself. It will return the instance reference. See for yourself in GR32_Layers.pas.

However, you're taking the wrong approach to begin with. TBitmap32 objects don't have layers. A TImage32 component has layers, which is useful if you want to not only display multiple bitmaps layers together, but also allow the user to interact with the layers; you'd detect which layer is which with the HitTest method, as described in the layer overview. Each layer consists of one graphic; for bitmaps, you probably want to use TBitmapLayer, not just TCustomLayer.

Just to create a new bitmap, you don't need layers at all. (And as long as you're using a Delphi version that understands PNG images, I'm pretty sure you don't even need Graphics32.) Instead, just start with a blank bitmap. Paint the main bitmap where it needs to go, then paint the frame bitmap, and then paint the stamp bitmap. Finally, save the bitmap.

Unless you're actually going to display all the separate bitmaps on a form, you don't need those TImgView32 components. That component is for displaying images on the screen with scroll bars.



来源:https://stackoverflow.com/questions/32740928/delphi-graphics32-draw-multiple-transparent-png-over-jpg-as-watermak

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!