1

I am creating a simple DXF generator (used by CNC machines) which works with letters only and I am having a problem using the PathGradientBrush with the GraphicsPath class. I'm drawing the outline of a letter using the GraphicsPath afterwhich I need the corresponding GraphicsPath filled with a gradient, something similar to this:
Letter "I" filled with gradient

As you can see, the gradient needs to start from the edges and then transition to white color in the center. This is code that I am currently using:

  private void Form1_Paint(object sender, PaintEventArgs e) {
   e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
   e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
   e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

   using(GraphicsPath Path = new GraphicsPath()) {
    Path.AddString("I", Font.FontFamily, (int) FontStyle.Regular, 250f, Point.Empty, StringFormat.GenericTypographic);
    using(Pen OutlinePen = new Pen(Color.Black, 1f)) {
     e.Graphics.DrawPath(OutlinePen, Path);
     using(PathGradientBrush FillBrush = new PathGradientBrush(Path)) {
      FillBrush.SurroundColors = new Color[] {
       Color.FromArgb(0, 0, 0),
       Color.FromArgb(51, 51, 51),
       Color.FromArgb(102, 102, 102),
       Color.FromArgb(173, 173, 173)
      };
      FillBrush.CenterColor = Color.FromArgb(255, 255, 255);
      e.Graphics.FillPath(FillBrush, Path);
     }
    }
   }
  }

, which produces this output:
enter image description here

Also, I've tried using FocusScales, but got no success from it either. In the example the letter "I" is shown, but this needs to work with all letters. Any help is highly appreciated.

P.S. Letter "N" as reference, sorry for the bad drawing, but this just a quick and dirty example. The letter needs to be filled with a 5 color gradient (from black at the edges to white in the middle), but taking into account the letter width. And if someone is interested, this is needed for simulating the depth dimension for the toolpath of a CNC machine.The white - middle part has the maximum depth.

Letter N
The whole letter should be filled with the gradient(s), please zoom-in to get the idea.

SkyFlyer
  • 202
  • 3
  • 10
  • Do have a look [at this post](http://stackoverflow.com/questions/39710996/polygon-with-blended-edges/39712131?s=5|0.2395#39712131). It solves a very similar problem.. – TaW Mar 04 '17 at 23:04
  • @TaW, thank you for your response. Yes, actually I've seen that, but the problem is that, that it only works with a fixed border (edge) size and that's not applicable in this case, because the lines that make up a letter have different height. Take for example the letter "N". The vertical lines are thicker than the diagonal one, so that solution does not work. – SkyFlyer Mar 04 '17 at 23:08
  • Hm, that is right. But how should the result look for this example then?? An image would be helpful.. – TaW Mar 04 '17 at 23:14
  • @TaW, I've added another picture as a reference. – SkyFlyer Mar 04 '17 at 23:38
  • Thanks; the sketch is really a bit dirty ;-) Frankly I'm not sure this is even possible, read: I'm not sure the task is actually well defined; also: I think the problem is not a geometrical one but a typographical one. Therefore it is realated to beauty and art and this makes a fully automated solution a little unlikley.. But maybe I'm wrong here. – TaW Mar 04 '17 at 23:46
  • @TaW, ahahaha, well, yes, I wanted to get back to you as soon as possible. ;-) – SkyFlyer Mar 04 '17 at 23:48
  • @TaW, and also: this is not needed for doing something fancy with the letters, as a matter of fact, this doesn't even need to be shown to the user. This technique is used to simulate the depth at which a CNC machine works, which is used when generating a DXF file. The lighter parts are deeper (white being the deepest one) and the darker being more let's say shallow. – SkyFlyer Mar 04 '17 at 23:51
  • This I understood right from your post. The problem is the a font, at least once it is transformed to a GraphicsPath is no longer consisting of __strokes__ of various directions and strenghts; instead it is an __outline__. Therefore at no point can one see how thick the stroke is to which the points belongs nor what direction it has; not even if is a stroke or two, a line or even a curve. - I still wonder if my post could help solve the problem, not in one step but maybe as a start., but atm my mind keeps hitting a brick wall ;-) – TaW Mar 05 '17 at 01:40
  • The cleanest solution might even be the other way around: Learn how to render a Font. Tough task, I'd say. And the only really practical one is imo to limit yourself either to a few letters or simpler fonts and/or a semi-automatic process, where you help with the shading to make sure it looks/works well. – TaW Mar 05 '17 at 01:45
  • @TaW, thanks for your response, I'm currently exploring different solutions from this one. – SkyFlyer Mar 05 '17 at 18:52
  • If you find a good one do consider writing it in an answer!! – TaW Mar 05 '17 at 18:54
  • @TaW, of course! ;) – SkyFlyer Mar 05 '17 at 18:55

0 Answers0