Author Topic: How to make 2 images into 1 and have it transparant?  (Read 590 times)

Offline pka4916

  • Imaging User
  • *
  • Posts: 9
    • View Profile
I am trying to get 2 images into 1

1 = normal pic
2 = a watermark.

so that the watermark is transparant on the picture
but all it does, is make it blank. 
How can I do it?



Code: [Select]
var
img  : timagedata;
img1 : timagedata;
img2 : timagedata;
img3 : timagedata;
begin
InitImage(Img);
InitImage(Img1);
InitImage(Img2);
loadimagefromfile(im1,img1);
loadimagefromfile(im2,img2);
newimage(214,384,ifDefault,img);
imaging.CopyRect(img1,0,0,img1.width,img1.Height,img,0,0);
imaging.CopyRect(img2,0,0,img2.width,img2.Height,img,0,192);
saveimagetofile(finalimage,img);
« Last Edit: 8 June 2010, 17:20:59 by pka4916 »

Offline Galfar

  • Administrator
  • Imaging User
  • *****
  • Posts: 312
    • View Profile
    • Galfar's Homepage
Re: How to make 2 images into 1 and have it transparant?
« Reply #1 on: 16 June 2010, 14:42:54 »
You need to use the canvas class which supports alpha blending.
This is my watermarking code, you can use it as inspiration:

Code: Pascal
  1.   var
  2.     Stream: TStream;
  3.     CanvasPage: TImagingCanvas;
  4.     Watermark: TSingleImage;
  5.     CanvasWater: TImagingCanvas;
  6.     R: TRect;
  7.   begin
  8.     Stream := TResourceStream.Create(HInstance, 'ImageWatermark', RT_RCDATA);
  9.     Watermark := TSingleImage.CreateFromStream(Stream);
  10.     Stream.Free;
  11.     CanvasWater := TImagingCanvas.CreateForImage(Watermark);
  12.  
  13.     // Page.Image is a TSingleImage instance
  14.     CanvasPage := TImagingCanvas.CreateForImage(Page.Image);
  15.     if Page.Width > Page.Height then
  16.       R := Rect(Round(Page.Width * 0.6), 0, Page.Width, Page.Height)
  17.     else
  18.       R := Rect(Round(Page.Width * 0.45), 0, Page.Width, Page.Height);
  19.     R := ScaleRectToRect(Watermark.BoundsRect, R);
  20.     R := Rect(R.Left, Page.Height - (R.Bottom - R.Top), R.Right, Page.Height);
  21.  
  22.     CanvasWater.StretchDrawAlpha(Watermark.BoundsRect, CanvasPage, R);    
  23.  
  24.     ... save to file, free canvases etc.
  25.  
« Last Edit: 28 July 2011, 22:15:42 by Galfar »