question

WillPittenger avatar image
0 Votes"
WillPittenger asked WillPittenger answered

Setting WPF pen at runtime for System.Windows.Media.GeometryDrawing

I have a project with some SVG files in my App's Resources. I retrieve them with the code in the first sample.

if(svgreader == null)
  svgreader =  new SharpVectors.Converters.FileSvgReader(null);

System.IO.Stream stream = typeof(Doc).Assembly.GetManifestResourceStream(strResourceID);
// This returns a System.Windows.DrawingGroup >> svgreader.Read(stream);

Later, I need to set the pen for some System.Windows.Media.GeometryDrawing that don't have the stroke specified in the SVG. This pen is determined by a different file that isn't a resource and won't be known until runtime. I thought the code below would do it once I have the System.Windows.Media.GeometryDrawing created based on the SVG by SharpVectors. (I'm stepping through all the objects in the drawing and duplicating each. This code shows only the part that duplicates the System.Windows.Media.GeometryDrawing`,)

System.Windows.Media.GeometryDrawing geomdrawingNew = new(geomdrawing.Brush, geomdrawing.Pen, geomdrawing.Geometry);
    
if(geomdrawingNew.Pen == null)
  geomdrawingNew.Pen = new System.Windows.Media.Pen(new System.Windows.Media.SolidColorBrush(rgb), 4);
    
return geomdrawingNew;

Unfortunately, my new pen doesn't have any effect. I don't understand why. Can someone help?

windows-wpf
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Maybe geomdrawingNew.Pen was not null?

0 Votes 0 ·

No. I verified it went into where the pen was set.

0 Votes 0 ·

Maybe the returned geomdrawingNew does not have any further effect, not only its pen. How is it used? Did you try to build and return a GeometryDrawing that uses, for example, a red brush and blue pen unconditionally?



0 Votes 0 ·

I've verified when my function that adds the pen returns a System.Windows.Media.Drawing with the pen set. But after the System.Windows.Media.DrawingImage is created, the pen is unset.

0 Votes 0 ·

@WillPittenger
Could you use Color.FromRgb(255,0,0) to replace your rgb to check if it works? If it doesn't work, could you show me the code snippet to create your System.Windows.Media.DrawingImage? Please show more details for me to create a demo to reproduce your error and analyze it.

0 Votes 0 ·

1 Answer

WillPittenger avatar image
0 Votes"
WillPittenger answered

I verified if I wait until after I create the Drawing that setting the pen works fine. So that's what I'm going with.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.