delphi - Creating a transparent custom bitmap brush -
problem definition
i trying create custom bitmap brush transparency doesn't seem working expected. if @ example. add code , hook paint, create , destroy events.
type tform3 = class(tform) procedure formdestroy(sender: tobject); procedure formcreate(sender: tobject); procedure formpaint(sender: tobject); private fbitmap: tbitmap; end; // implementation function createblockbitmap(const apencolor: tcolor): tbitmap; begin result := tbitmap.create; result.transparent := true; result.canvas.brush.color := clwhite; result.canvas.brush.style := bsclear; result.pixelformat := pf32bit; result.setsize(20, 20); result.canvas.brush.color := apencolor; result.canvas.brush.style := bssolid; result.canvas.fillrect(rect(0,0,10,10)); end; procedure tform3.formdestroy(sender: tobject); begin fbitmap.free; end; procedure tform3.formcreate(sender: tobject); begin fbitmap := createblockbitmap(clred); end; procedure tform3.formpaint(sender: tobject); var colnum: integer; rownum: integer; begin // paint rectangle using brush canvas.pen.color := clgreen; canvas.brush.bitmap := fbitmap; // using bitmap canvas.rectangle(50, 50, 250, 250); // draw block using canvas.draw rownum := 0 9 colnum := 0 9 canvas.draw(350 + rownum * 20, 50 + colnum * 20, fbitmap); end; this code produces 2 painted blocks. left 1 painted using bitmap brush , right hand side 1 painted using number of canvas.draw calls.

i need brush painted transparency similar happen if used hatch brush. answer seems indicate it's possible:
how can draw patternbrush transparent backround (gdi)?
what have tried
1) tried using solid background color instead of using bsclear. makes background white.
result.canvas.brush.color := clwhite; result.canvas.brush.style := bssolid; if use clfuchsia color fuchsia. tried painting background clfuchsia , setting transparentcolor clfuchsia. canvas.draw option paints transparency , brush doesn't.
2) tried setting alpha channel directly following code:
procedure setalphabitmap(const dest: tbitmap;color : tcolor;alpha:byte); type trgb32 = record b, g, r, a: byte; end; prgbarray32 = ^trgbarray32; trgbarray32 = array[0..0] of trgb32; var x, y: integer; line, delta: integer; colorrgb : tcolor; begin if dest.pixelformat<>pf32bit exit; colorrgb := colortorgb(color); line := integer(dest.scanline[0]); delta := integer(dest.scanline[1]) - line; y := 0 dest.height - 1 begin x := 0 dest.width - 1 if tcolor(rgb(prgbarray32(line)[x].r, prgbarray32(line)[x].g, prgbarray32(line)[x].b)) = colorrgb prgbarray32(line)[x].a := alpha; inc(line, delta); end; end; and calling routine after rectangle has been painted using background color.
result.canvas.brush.style := bssolid; result.canvas.fillrect(rect(0,0,10,10)); setalphabitmap(result, clblack, 0); // set alpha channel end; i know alpha channel working because if pass in alpha value of 255 shows in black in canvas.draw too.
setalphabitmap(result, clblack, 255); 3) tried testing creating pattern brush , assigning instead of bitmap. produces same results. fbrush hbrush.
fbrush := createpatternbrush(fbitmap.handle); and setting brush this:
canvas.brush.handle := fbrush; 4) tried calling setbkmode indicated in answer above. made no difference @ all.
canvas.pen.color := clgreen; canvas.brush.bitmap := fbitmap; setbkmode(canvas.handle, transparent); // doesn't make difference canvas.rectangle(50, 50, 250, 250); edit
5) tested monochrome bitmap , has same problem. image painted white background , black foreground brush , transparent canvas.draw.
function createmonochromebitmap: tbitmap; begin result := tbitmap.create; result.transparent := true; result.canvas.brush.color := clwhite; result.canvas.brush.style := bssolid; result.pixelformat := pf1bit; result.setsize(20, 20); result.canvas.brush.color := clblack; result.canvas.brush.style := bssolid; result.canvas.fillrect(rect(0,0,10,10)); end; and in constructor:
fbitmap := createmonochromebitmap; fbrush := createpatternbrush(fbitmap.handle); in paint set handle rather bitmap property.
canvas.brush.handle := fbrush;
try clear canvas null color before drawing loop.
canvas.clear(talphacolorrec.null);
greetings. pau.
Comments
Post a Comment