c# - Set the transparency of a circle on a bitmap -
i'm trying set transparency of circle on bitmap in c# .net winforms. tried doing using graphics.drawellipse this:
private void setcirclealpha(int alpha, ref bitmap b, columnvector2 pos, int diameter) { graphics g = graphics.fromimage(b); solidbrush sb = new solidbrush(color.fromargb(0, 0, 0, 0)); g.fillellipse(sb, pos.x, pos.y, diameter, diameter); }
but not want draws transparent circle on image instead of setting transparency of circle.
i have resorted using extremely slow code:
private void setcirclealpha(int alpha, ref bitmap b, columnvector2 pos, int diameter) { //calculate square root of radius float radius = diameter / 2; float sqradius = radius * radius; //calculate centre of circle columnvector2 centre = pos + new columnvector2(radius, radius); (int x = (int)pos.x; x < pos.x + diameter; x++) { (int y = (int)pos.y; y < pos.y + diameter; y++) { //calculate distance between centre of circle , point being tested columnvector2 vec = new columnvector2(x, y) - centre; //if distance between point , centre of circle less radius of circle point in circle. //calculate distance using pythagoras (a^2 + b^2 = c^2) //square both distance , radius eliminate need square root float sqdist = (vec.x * vec.x) + (vec.y * vec.y); if (sqdist < sqradius) { b.setpixel(x, y, color.fromargb(alpha, b.getpixel(x, y))); } } } }
my question is: is there better/faster way this?
please note i'm not asking faster circle generation algorithms, rather asking alternative graphics options.
using hans passant's comment got working:
private void setcirclealpha(int alpha, ref bitmap b, columnvector2 pos, int diameter) { graphics g = graphics.fromimage(b); g.compositingmode = system.drawing.drawing2d.compositingmode.sourcecopy; solidbrush sb = new solidbrush(color.fromargb(alpha, 0, 0, 0)); g.fillellipse(sb, pos.x, pos.y, diameter, diameter); }
Comments
Post a Comment