本文介绍了如何在表单上绘制透明图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是原始的PNG:



我已经在TImage ::

  Image1.Transparent:= True; 
Form1.Color:= clWhite;
Form1.TransparentColor:= True;
Form1.TransparentColorValue:= clWhite;


应用程序:




图像不完全透明。

图像可以由任何控件绘制,也可以由画布。

我想使用BMP图像。
也许我做错了什么?
请帮忙!

解决方案

我找到了一个解决方案,让您将Alpha通道的BMP图像仅使用Windows API绘制到表单上:

  const 
AC_SRC_OVER = 0;
AC_SRC_ALPHA = 1;

类型
BLENDFUNCTION =打包记录
BlendOp,
BlendFlags,
SourceConstantAlpha,
AlphaFormat:byte;
结束

函数WinAlphaBlend(hdcDest:HDC; xoriginDest,yoriginDest,wDest,hDest:integer;
hdcSrc:HDC; xoriginSrc,yoriginSrc,wSrc,hSrc:integer; ftn:BLENDFUNCTION):LongBool;
stdcall;外部'Msimg32.dll'名称'AlphaBlend';

程序TForm4.FormClick(Sender:TObject);
var
hbm:HBITMAP;
bm:BITMAP;
bf:BLENDFUNCTION;
dc:HDC;
begin
hbm:= LoadImage(0,
'C:\Users\Andreas Rejbrand\Skrivbord\RatingCtrl.bmp',
IMAGE_BITMAP,
0,
0,
LR_LOADFROMFILE);
如果hbm = 0,那么
RaiseLastOSError;
尝试
如果GetObject(hbm,sizeof(bm),@bm)= 0,那么RaiseLastOSError;
dc:= CreateCompatibleDC(0);
如果dc = 0,则RaiseLastOSError;
尝试
如果SelectObject(dc,hbm)= 0,则RaiseLastOSError;
bf.BlendOp:= AC_SRC_OVER;
bf.BlendFlags:= 0;
bf.SourceConstantAlpha:= 255;
bf.AlphaFormat:= AC_SRC_ALPHA;
如果不是WinAlphaBlend(Canvas.Handle,
10,
10,
bm.bmWidth,
bm.bmHeight,
dc,
0,
0,
bm.bmWidth,
bm.bmHeight,
bf)然后RaiseLastOSError;
finally
DeleteDC(dc);
结束
finally
DeleteObject(hbm);
结束
结束

使用GIMP,我转换了PNG图像





将找到32位RGBA位图,发现,结果非常好:






I want to a draw transparent image on Delphi form, but it is not working.

Here is original PNG:

I have loaded image in a TImage::

Image1.Transparent := True;
Form1.Color := clWhite;
Form1.TransparentColor := True;
Form1.TransparentColorValue := clWhite;

The application:


The image isnt fully transparent.
The image can be drawed by any control or just by canvas.
I would like to use BMP images.
Maybe I am doing something wrong?
Please help!

解决方案

I found a solution that will let you draw a BMP image with an alpha channel onto a form using only the Windows API:

const
  AC_SRC_OVER = 0;
  AC_SRC_ALPHA = 1;

type
  BLENDFUNCTION = packed record
    BlendOp,
    BlendFlags,
    SourceConstantAlpha,
    AlphaFormat: byte;
  end;

function WinAlphaBlend(hdcDest: HDC; xoriginDest, yoriginDest, wDest, hDest: integer;
  hdcSrc: HDC; xoriginSrc, yoriginSrc, wSrc, hSrc: integer; ftn: BLENDFUNCTION): LongBool;
  stdcall; external 'Msimg32.dll' name 'AlphaBlend';

procedure TForm4.FormClick(Sender: TObject);
var
  hbm: HBITMAP;
  bm: BITMAP;
  bf: BLENDFUNCTION;
  dc: HDC;
begin
  hbm := LoadImage(0,
    'C:\Users\Andreas Rejbrand\Skrivbord\RatingCtrl.bmp',
    IMAGE_BITMAP,
    0,
    0,
    LR_LOADFROMFILE);
  if hbm = 0 then
    RaiseLastOSError;
  try
    if GetObject(hbm, sizeof(bm), @bm) = 0 then RaiseLastOSError;
    dc := CreateCompatibleDC(0);
    if dc = 0 then RaiseLastOSError;
    try
      if SelectObject(dc, hbm) = 0 then RaiseLastOSError;
      bf.BlendOp := AC_SRC_OVER;
      bf.BlendFlags := 0;
      bf.SourceConstantAlpha := 255;
      bf.AlphaFormat := AC_SRC_ALPHA;
      if not WinAlphaBlend(Canvas.Handle,
        10,
        10,
        bm.bmWidth,
        bm.bmHeight,
        dc,
        0,
        0,
        bm.bmWidth,
        bm.bmHeight,
        bf) then RaiseLastOSError;
    finally
      DeleteDC(dc);
    end;
  finally
    DeleteObject(hbm);
  end;
end;

Using The GIMP, I converted the PNG image

http://privat.rejbrand.se/RatingCtrl.png

found here to a 32-bit RGBA bitmap, found here, and the result is very good:

http://privat.rejbrand.se/gdiblend1.pnghttp://privat.rejbrand.se/gdiblend2.pnghttp://privat.rejbrand.se/gdiblend3.png

这篇关于如何在表单上绘制透明图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 04:16
查看更多