Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It works for me:</p> <pre><code>procedure TForm1.Button1Click(Sender: TObject); var bm1, bm2: TBitmap; begin bm1 := TBitmap.Create; bm1.LoadFromFile('C:\Users\Andreas Rejbrand\Pictures\portrait.bmp'); bm2 := TBitmap.Create; bm2.SetSize(bm1.Width, bm1.Height); bm2.Canvas.Brush.Color := clRed; bm2.Canvas.Pen.Style := psClear; bm2.Canvas.Ellipse(0, 0, bm2.Width, bm2.Height); Canvas.Draw(100, 100, bm1); Canvas.Draw(100, 100, bm2, 127); end; </code></pre> <p><a href="http://privat.rejbrand.se/blendimg1.png" rel="noreferrer">Sample 1 http://privat.rejbrand.se/blendimg1.png</a></p> <p>If you want more control, you can always do the processing manually:</p> <pre><code>procedure TForm1.Button1Click(Sender: TObject); type TRGB32Array = packed array[0..MaxInt div SizeOf(TRGBQuad)-1] of TRGBQuad; PRGB32Array = ^TRGB32Array; TScanline = TRGB32Array; PScanline = ^TScanline; var bm1, bm2, bm3: TBitmap; sc1, sc2, sc3: PScanline; i: Integer; j: Integer; var transp: real; const opacity = 0.29; begin transp := 1 - opacity; bm1 := TBitmap.Create; bm1.LoadFromFile('C:\Users\Andreas Rejbrand\Pictures\portrait.bmp'); bm2 := TBitmap.Create; bm2.SetSize(bm1.Width, bm1.Height); bm2.Canvas.Brush.Color := clRed; bm2.Canvas.Pen.Style := psClear; bm2.Canvas.Ellipse(0, 0, bm2.Width, bm2.Height); bm3 := TBitmap.Create; bm3.SetSize(bm1.Width, bm1.Height); bm1.PixelFormat := pf32bit; bm2.PixelFormat := pf32bit; bm3.PixelFormat := pf32bit; for i := 0 to bm1.Height - 1 do begin sc1 := bm1.ScanLine[i]; sc2 := bm2.ScanLine[i]; sc3 := bm3.ScanLine[i]; for j := 0 to bm1.Width - 1 do with sc3^[j] do begin rgbBlue := round(transp*sc1^[j].rgbBlue + opacity*sc2^[j].rgbBlue); rgbGreen := round(transp*sc1^[j].rgbGreen + opacity*sc2^[j].rgbGreen); rgbRed := round(transp*sc1^[j].rgbRed + opacity*sc2^[j].rgbRed); end; end; Canvas.Draw(100, 100, bm3); end; </code></pre> <p><a href="http://privat.rejbrand.se/blendimg2.png" rel="noreferrer">Sample 2 http://privat.rejbrand.se/blendimg2.png</a></p> <p>You can for example let the background image be at 100 % opacity outside the ellipse:</p> <pre><code> ... for i := 0 to bm1.Height - 1 do begin sc1 := bm1.ScanLine[i]; sc2 := bm2.ScanLine[i]; sc3 := bm3.ScanLine[i]; for j := 0 to bm1.Width - 1 do if sc2^[j].rgbBlue + sc2^[j].rgbGreen + sc2^[j].rgbRed = 3*255 then sc3^[j] := sc1^[j] else with sc3^[j] do begin rgbBlue := round(transp*sc1^[j].rgbBlue + opacity*sc2^[j].rgbBlue); rgbGreen := round(transp*sc1^[j].rgbGreen + opacity*sc2^[j].rgbGreen); rgbRed := round(transp*sc1^[j].rgbRed + opacity*sc2^[j].rgbRed); end; end; ... </code></pre> <p><a href="http://privat.rejbrand.se/blendimg3.png" rel="noreferrer">Sample 3 http://privat.rejbrand.se/blendimg3.png</a></p> <p>Not to mention <a href="http://english.rejbrand.se/algosim/manual/pmproc/pmproc.html" rel="noreferrer">all other cool stuff you can do with pixmap manipulation</a>:</p> <pre><code> ... for i := 0 to bm1.Height - 1 do begin sc1 := bm1.ScanLine[i]; sc2 := bm2.ScanLine[i]; sc3 := bm3.ScanLine[i]; for j := 0 to bm1.Width - 1 do if sc2^[j].rgbBlue + sc2^[j].rgbGreen + sc2^[j].rgbRed = 3*255 then sc3^[j] := sc1^[j] else with sc3^[j] do begin rgbBlue := round(sin(transp*sc1^[j].rgbBlue + opacity*sc2^[j].rgbBlue)); rgbGreen := round(transp*sc1^[j].rgbGreen + opacity*sc2^[j].rgbGreen); rgbRed := round(transp*sc1^[j].rgbRed + opacity*sc2^[j].rgbRed); end; end; ... </code></pre> <p><a href="http://privat.rejbrand.se/blendimg4.png" rel="noreferrer">Sample 4 http://privat.rejbrand.se/blendimg4.png</a></p> <p>If you really don't want to do it manually, I just figured out, you can draw the ellipse on a copy of the first bitmap, and then blend these two bitmaps:</p> <pre><code>procedure TForm1.Button1Click(Sender: TObject); var bm1, bm2: TBitmap; begin bm1 := TBitmap.Create; bm1.LoadFromFile('C:\Users\Andreas Rejbrand\Pictures\portrait.bmp'); bm2 := TBitmap.Create; bm2.LoadFromFile('C:\Users\Andreas Rejbrand\Pictures\portrait.bmp'); bm2.Canvas.Brush.Color := clRed; bm2.Canvas.Pen.Style := psClear; bm2.Canvas.Ellipse(0, 0, bm2.Width, bm2.Height); Canvas.Draw(100, 100, bm1); Canvas.Draw(100, 100, bm2, 127); end; </code></pre> <p><a href="http://privat.rejbrand.se/blendimg5.png" rel="noreferrer">Sample 5 http://privat.rejbrand.se/blendimg5.png</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload