compact framework - Drawing control with transparent background -
i've been trying display image has transparent border background control.
unfortunately, transparent area creates hole in parent form follows:
in above image, form has red background i'd hoped see behind control in transparent areas.
the code used follows:
protected override void onpaint(system.windows.forms.painteventargs e) { if (this.image != null) { graphics g = graphics.fromimage(this.image); imageattributes attr = new imageattributes(); //set transparency based on top left pixel attr.setcolorkey((this.image bitmap).getpixel(0, 0), (this.image bitmap).getpixel(0, 0)); //draw image using image attributes. rectangle dstrect = new rectangle(0, 0, this.image.width, this.image.height); e.graphics.drawimage(this.image, dstrect, 0, 0, this.image.width, this.image.height, graphicsunit.pixel, attr); } else { base.onpaint(e); } } protected override void onpaintbackground(system.windows.forms.painteventargs e) { //base.onpaintbackground(e); }
this class inherited picturebox because needed control implements onmousemove , onmouseup events.
i've been researching of day without success testing out different ideas unfortunately work on full framework , not .net cf.
any ideas appreciated.
ah joys of cf transparency. go on , on (and have in blog , project resistance code did ages ago).
the gist this. child control has paint it's areas, first has call it's parent (the form in case) , tell redraw it's background image everywhere except in child's clipping region , draw on top of that. if sounds bit confusing it's because is.
for example, if @ project resistance, view (which control) draws resistor , bands. lies in form has image background, , background needs "show through" transparent areas of resistor:
so in drawing code of resistor this:
protected override void onpaint(painteventargs e) { base.onpaint(e); try { rect rect = new rect(this.bounds); // draw blank infrastructure.graphictools.drawtransparentbitmap(e.graphics, m_blankimage, bounds, new rectangle(0, 0, m_blankimage.width, m_blankimage.height)); if (m_bandsimage != null) { // draw bands infrastructure.graphictools.drawtransparentbitmap(e.graphics, m_bandsimage, bounds, new rectangle(0, 0, m_bandsimage.width, m_bandsimage.height)); } } { } if (!controller.touchmode) { // todo: draw in selection arrow // controller.selectedband } }
which simple enough. key calls it's base onpaint, this:
protected override void onpaint(system.windows.forms.painteventargs e) { // assumes we're in workspace, on mainform (the whole parent.parent thing) ibackgroundpaintprovider bgpaintprovider = parent.parent ibackgroundpaintprovider; if (bgpaintprovider != null) { rectangle rcpaint = e.cliprectangle; // use parent, since it's workspace position in form want, // not our position in workspace rcpaint.offset(parent.left, parent.top); bgpaintprovider.paintbackground(e.graphics, e.cliprectangle, rcpaint); } }
you can see it's calling paintbackground
of containing form (it's parent.parent in case becuse control in container called workspace - wouldn't need walk twice in case). draws in background image in area you're seeing "hole"
public void paintbackground(graphics g, rectangle targetrect, rectangle sourcerect) { g.drawimage(m_bmbuffer, targetrect, sourcerect, graphicsunit.pixel); }
Comments
Post a Comment