my task pretty simple: draw line point point b , set different colors @ point , b, e.g. white @ , black @ b fade out line.
i tried doing using lineargradientbrush:
var brush = new lineargradientbrush( color.fromargb(255, 0, 0, 0), color.fromargb(255, 255, 0, 0), new point(0, 0), new point(1,1)); var pen = new pen(brush, 10.0); dc.drawline(pen, new point (300, 300), new point(300, 100));
but not produce wanted results since gradientbrush mapped according bounding box of object. therefore, results line going 100/100 200/200 , line going opposite direction same (which not want).
how solve using wpf? thanks.
edit: need use low level graphics functions (such drawline) performance reasons because draw lot of lines.
how use lineargradientbrush article examples in both xaml , c# code.
try approach , please tell me need. take attention please lines drawn in opposite directions.
public partial class window1 : window { public window1() { initializecomponent(); var image = new image { source = new drawingimage(createdrawingvisualrectangle(new point(300,300), new point(300,100) ).drawing), stretch = stretch.none, }; var image2 = new image { margin = new thickness(10, 0, 0, 0), source = new drawingimage(createdrawingvisualrectangle(new point(300, 100), new point(300, 300)).drawing), stretch = stretch.none, }; var stackpanel = new stackpanel() {orientation = orientation.horizontal}; stackpanel.children.add(image); stackpanel.children.add(image2); content = stackpanel; } // create drawingvisual contains rectangle. private drawingvisual createdrawingvisualrectangle(point start, point end) { drawingvisual drawingvisual = new drawingvisual(); // retrieve drawingcontext in order create new drawing content. drawingcontext drawingcontext = drawingvisual.renderopen(); // create rectangle , draw in drawingcontext. var gradientstopcollection = new gradientstopcollection { new gradientstop(color.fromargb(255, 0, 0, 0), 0.0), new gradientstop(color.fromargb(255, 255, 0, 0), 0.75) }; var brush = new lineargradientbrush(gradientstopcollection); var pen = new pen(brush, 10.0); var vector1 = new vector(start.x, start.y); var vector2 = new vector(end.x, end.y); if (vector1.length < vector2.length) { brush.startpoint = new point(1, 1); brush.endpoint = new point(0, 0); } drawingcontext.drawline(pen, start, end); drawingcontext.close(); return drawingvisual; } }
Comments
Post a Comment