winforms - Attempting to Drawing A Dropshadow-Like Gradient on the Edge of a Control in C# -
i'm trying create function generate dropshadow-like gradient on given edge of control. i'm not extremely familiar system.drawing
, have used once before, , used experience try make this. drawing gradient, one-pixel line one-pixel line (through loop uses value called length
determine number of lines), changes based on alpha
value of pen
i'm using.
the issue either i'm incorrectly calculating how alpha
value next line, or i'm using system.drawing
incorrectly. think may latter; can't figure out how bitmap
i'm generating display, whether using graphics graphics = this.creategraphics()
or setting graphics
bitmap
.
i'm not sure how break code question, it's relatively short function anyhow. see documentation more details on how function supposed work.
/// <summary> /// generates simple drop shadow @ rectangular angle along edge of control of choice. /// </summary> /// <param name="startalpha">the starting alpha (transparency) value of gradient of shadow.</param> /// <param name="fourthsrotationdirection">the rotation (in 90 degree values) of shadow.</param> /// <param name="control">the control given shadow.</param> /// <param name="length">the length or depth of shadow.</param> /// <param name="clearshadow">determines whether or not function should clear graphics rid of preexisting shadows, regeneration purposes.</param> void dropshadowgenerator(int startalpha, fourthsrotationdirection fourthsrotationdirection, control control, int length, bool clearshadow) { picturebox image = new picturebox(); image.location = new point(0, 0); image.size = this.size; this.controls.add(image); bitmap bmp = new bitmap(this.width, this.height); graphics graphics = graphics.fromimage(bmp); pen pen = (fourthsrotationdirection == fourthsrotationdirection.zero || fourthsrotationdirection == fourthsrotationdirection.oneeighty) ? new pen(color.fromargb(startalpha), control.height) : new pen(color.fromargb(startalpha), control.width); point startpoint = new point(); point endpoint = new point(); float shadowfactor = 255 / length; float currentalpha = startalpha; if (clearshadow) { graphics.clear(color.transparent); } if (fourthsrotationdirection == fourthsrotationdirection.zero) { startpoint = new point(control.location.x + control.width, control.location.y); endpoint = new point(startpoint.x, startpoint.y + control.height); } else if (fourthsrotationdirection == fourthsrotationdirection.oneeighty) { startpoint = new point(control.location.x, control.location.y); endpoint = new point(startpoint.x, startpoint.y + control.height); } else if (fourthsrotationdirection == fourthsrotationdirection.ninety) { startpoint = new point(control.location.x, control.location.y); endpoint = new point(startpoint.x + control.width, startpoint.y); } else if (fourthsrotationdirection == fourthsrotationdirection.twoseventy) { startpoint = new point(control.location.x, control.location.y + control.height); endpoint = new point(startpoint.x + control.width, startpoint.y); } (int = 1; <= length; i++) { graphics.drawline(pen, startpoint, endpoint); currentalpha = currentalpha - shadowfactor; pen.color = color.fromargb((int)currentalpha, color.black); switch (fourthsrotationdirection) { case fourthsrotationdirection.zero: startpoint = new point(startpoint.x++, startpoint.y); endpoint = new point(endpoint.x++, endpoint.y); break; case fourthsrotationdirection.oneeighty: startpoint = new point(startpoint.x--, startpoint.y); endpoint = new point(endpoint.x--, endpoint.y); break; case fourthsrotationdirection.ninety: startpoint = new point(startpoint.x, startpoint.y--); endpoint = new point(endpoint.x, endpoint.y--); break; case fourthsrotationdirection.twoseventy: startpoint = new point(startpoint.x, startpoint.y++); endpoint = new point(endpoint.x, endpoint.y++); break; } } image.image = bmp; }
how can display on form? mentioned before, using system.drawing incorrectly, or gradient generation incorrect?
your main problem way calculate new coordinates:
startpoint = new point(startpoint.x++, startpoint.y); endpoint = new point(endpoint.x++, endpoint.y);
that not change coordinates, because assign value first , increase it. try:
startpoint = new point(++startpoint.x, startpoint.y); endpoint = new point(++endpoint.x, endpoint.y);
or, find more readable:
startpoint = new point(startpoint.x+1, startpoint.y); endpoint = new point(endpoint.x+1, endpoint.y);
or without creating new point, can did:
startpoint.x++; endpoint.x++;
Comments
Post a Comment