问题描述
我想在Delphi表单上绘制半透明图像,但由于某种原因它不起作用。
I want to a draw a translucent image on a Delphi form, but for some reason it is not working.
这是原始的PNG(边界为半透明) ):
Here is the original PNG (border is semi transparent):
我将图像加载到 TImage
对象中:
I load the image in a TImage
object:
Image1.Transparent := True;
Form1.Color := clWhite;
Form1.TransparentColor := True;
Form1.TransparentColorValue := clWhite;
应用程序:
The application:
图片不是半透明的。我正在使用包含Alpha通道的BMP图像。我错过了什么吗?
The image isn't translucent. I am working with a BMP image that contains the alpha channel. Am I missing something?
推荐答案
我找到了一种解决方案,可以使用以下方法在窗体上绘制带有alpha通道的BMP图像仅Windows API:
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;
使用GIMP,我转换了PNG图片
Using The GIMP, I converted the PNG image
找到了到一个32位RGBA位图,在找到,并且结果非常好:
found here to a 32-bit RGBA bitmap, found here, and the result is very good:
这篇关于如何在表单上绘制半透明图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!