开发者

Draw opacity ellipse in Delphi 2010

开发者 https://www.devze.com 2023-02-09 12:00 出处:网络
I am looking to draw an opacity ellipse in CodeGear Delphi 2010. I had tried to draw to an another bitmap,

I am looking to draw an opacity ellipse in CodeGear Delphi 2010.

I had tried to draw to an another bitmap, I had set the开发者_StackOverflow bitmap transparent color(for background) Call the ellipse method.

And in my image I draw the bitmap with opacity parameter(from overload). But it doesn't work.

I want something like this http://www.java2s.com/Tutorial/VBImages/WPF-UseOpacityMaskAndRadialGradientBrush.PNG

Does anybody know an working method?


It works for me:

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;

Draw opacity ellipse in Delphi 2010

If you want more control, you can always do the processing manually:

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;

Draw opacity ellipse in Delphi 2010

You can for example let the background image be at 100 % opacity outside the ellipse:

  ...
  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;
  ...

Draw opacity ellipse in Delphi 2010

Not to mention all other cool stuff you can do with pixmap manipulation:

  ...
  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;
  ...

Draw opacity ellipse in Delphi 2010

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:

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;

Draw opacity ellipse in Delphi 2010

0

精彩评论

暂无评论...
验证码 换一张
取 消