I have some XAML based diagrams that consist of Paths embedded in Canvas objects e.g.
<Canvas x:Name="c1" Width="55.2533" Height="18.2933" Canvas.Left="194.606" Canvas.Top="194.131">
<Path x:Name="Path_5" Width="8.02666" Height="13.44" Canvas.Left="0" Canvas.Top="0.559998" Stretch="Fill" Fill="#FF000000" Data="......etc"/>
</Canvas>
Is there a way/tool to convert the XAML to either a ShapeFile or SqlGeometry data? I need to convert as I want to display the graphic in a third party map control that only binds to ShapeFiles or SqlGeometry data.
Any help would be appreciated.
The first thing to do would be to extract the path points. Then you need to produce your shape.
GETTING POINTS:
this stackoverflow answer describes altering a shape, but you see how to get the points from a path.
BUILDING SHAPE:
Cant' use SqlGeometry with Silverlight:
I think part of the answer is you can't go to SqlGeometry directly in silverlight. Your example is using Microsoft.SqlServer.Types, which was not built for the silverlight runtime. If you were going to a service it would be no problem, the service could use the full .NET framework an you can use the path points to build a well-known-text string, and then go directly to a sqlGeometry using
SqlGeometry newGeom = SqlGeometry.STGeomFromText(wktstring, srid).MakeValid();
Best best is to try to create a shapefile
I'm afraid I can't help you with creating a shapefile, sorry. Haven't used them much.
Related
I want to use a SVG image format as an button image because I want these images is shown with high quality so I have decided to use SVG format.
I have searched about it and There are some people that say SVG format can be used as WPF image source.
But when I use SVG image Like this:
<Image Source="Images/hard.svg"/>
I have error.
Absolutely Microsoft website says I can use SVG file as Image source.
https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.media.imaging.svgimagesource?view=winrt-19041
How can I use SVG image format as my WPF image souse?
You have to convert svg to xaml using this library https://github.com/BerndK/SvgToXaml
Sample output:
<DrawingImage x:Key="ic_close">
<DrawingImage.Drawing>
<DrawingGroup ClipGeometry="M0,0 V24 H24 V0 H0 Z">
<GeometryDrawing Brush="Red"
Geometry="F0 M24,24z M0,0z M5.21723,16.4509C4.64003,17.0905 4.68203,17.8189 5.21723,18.3421 5.75123,18.8653 6.65362,18.8329 7.15522,18.3421 7.52928,17.977 10.3498,15.0233 11.7599,13.5448 13.1706,15.0243 15.9893,17.977 16.3633,18.3402 16.8649,18.8298 17.7673,18.8634 18.3013,18.3402 18.8365,17.8206 18.8785,17.091 18.3013,16.4514L13.8049,11.7618 18.3013,7.07225C18.8785,6.43265 18.8365,5.70425 18.3013,5.18105 17.7673,4.65785 16.8649,4.69025 16.3633,5.18105 15.9892,5.54616 13.1687,8.49985 11.7585,9.97833 10.3479,8.4988 7.52916,5.54607 7.15523,5.18287 6.65363,4.69327 5.75123,4.65967 5.21723,5.18287 4.68203,5.70247 4.64003,6.43207 5.21723,7.07167L9.71363,11.7613 5.21723,16.4509z" />
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
Sample usage:
<Image Width="14"
Height="14"
Source="{StaticResource ic_close}" />
If you look at the namespace of that page you linked, you will see:
Windows.UI.Xaml.Media.Imaging
Meaning this is uwp rather than wpf.
You could use SharpVectors
https://github.com/ElinamLLC/SharpVectors
Or you could manually extract the path or paths from in that svg and use those in wpf. Since this is a manual conversion that might not be attractive if there are a number of svg you wish to use or appropriate if they are somehow dynamic.
Another option to consider is xaml islands.
You could host a uwp image control in a xaml island
https://blogs.windows.com/windowsdeveloper/2018/11/02/xaml-islands-a-deep-dive-part-1/#:~:text=XAML%20Islands%20is%20a%20technology,Windows%20Forms%20and%20WPF%20technologies.
Note that this introduces a dependency on win 10 creators edition or later.
WPF renders vector based but cannot render SVG files directly. You would have to use a vector based image processing application to convert the SVG to XAML.
Generally, the XAML obtained from the exported SVG usually consists of Path or Geometry elements, which you wrap into a Viewbox for scaling.
Adobe Illustrator (not free, trial available) yields the best results. You use it to convert the SVG image to .ai file and then Blend or a plug-in to export the .ai file as XAML.
AB4D (not free, trial available) is another application which also outputs very good results and allows to export directly to XAML.
InkScape is free and works too, but the results are not very good and most of the time require manual post-processing. Complex graphics never look good out of the box.
There are more tools like SvgToXaml, but I don't remember the quality.
I have a bit of a problem with XAML and Blend. We;ve converted the company logo from AI -> SVG -> XAML, cleaned it up a bit and its has produced a very peculiar source. In essence it is applying a "flip" transformation to a collection of paths:
<Canvas Width="640" Height="200" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Canvas Width="640" Height="200" RenderTransform="1,0,0,-1,0,200" >
<!-- L -->
<Canvas RenderTransform="1,0,0,1,317.0645,64.3652">
<Path>
<Path.Data>
<PathGeometry FillRule="Nonzero" Figures="M0,0L0,75.065 18.743,76.83 18.743,17.066 43.553,17.066 38.564,0 0,0z" />
</Path.Data>
</Path>
</Canvas>
... etc
Note the RenderTransform="1,0,0,-1,0,200" node in the enclosing Canvas. It flips the canvas upside down and lowers it 200 down to adjust to root canvas position. Given taht Logo looks ok when rendered means that all graphic elements in the xaml are actually upside down :). Maybe its an AI thing, I don't know.
The problem is that I now use Blend to convert this canvas into a GraphicBrush. The blend omits the transform and the logo looks upiside down when brush is applied. Rather then again transforming at each brush recipient I was thinking that maybe we should just fix it at the root, e.g. convert all paths so that they render properly to begin with avoiding a need for root transform.
Question: is there a programmatic (or any other way) to do this? Meaning, can I render this canvas and then serialize a rendered (after all transforms applied) DOM into an XAML?
Or to make the question clearer: there are two transforms applied to each path (as seen above, one global flip and one local shift for each path), can I somehow apply those transforms to each path/its points so that paths render without them.
You can use the Export to XAML feature built in to Expression Design in the Microsoft Expression suite.
Or personally I normally use Mike Swanson's AI to XAML converter since I'm normally already working in Illustrator for more complex vector graphics.
Both would provide a better result than the converter you used. Not sure where that original conversion got so wacky with the RenderTransforms etc but as you've found, it helps to have a good conversion to start with then going through the trouble of fixing a bad one haha.
Cheers!
I have some images (32x32 .png) that I want to display in my Windows Phone 7 application. Right, now, I am able to scale them, etc... I was wondering if there was a way to scale them without any smoothing algorithms applied (so when I double the image size, it creates a blocky image look).
Right now in my XAML I have the following:
<Image Height="64" Width="64" Margin="12,0,9,0" Name="{Binding itemName}"
Stretch="Uniform" VerticalAlignment="Center" Source="{Binding imageName}" />
Where imageName is just a path to the .png images. Is there a simple way to do this in just XAML, or should I be loading the image into a different format to play with it in the code (while keeping the transparency of the png).
Thanks in advance,
-Jeff
Theres currently no way to do this in XAML. The only way to achieve a nearest neighbor scaling without any interpolation (smoothing) is the open source library WriteableBitmapEx.
http://writeablebitmapex.codeplex.com
First load the PNG into a WriteableBitmap, then use the Resize extension method the WriteableBitmapEx provides. Use the NearestNeighbor as value for the last parameter of the method. Then assign the return value of the Resize method to the Source property of your Image control.
You can wrap this functionality in an IValueConverter implementation so you can use it in XAML.
I have a few 3D meshes in my WPF application, and I need to add some animations to them, not the typical animations, but rather a sequence of PNG images for creating a graphical animation.
Think of it like I need to add a cartoon animation to the side of a Cube.
I know very well about the Viewport2DVisual3D, but when I replace my normal ModelVisual3D with a Viewport2DVisual3D, I get horrible performance! Around the 5 FPS mark.
As soon as I remove the material with IsVisualHostMaterial set to true, the frame rate is restored to a normal state.
Performance is always a tricky subject, but what I was thinking was creating a Visual Brush with an image source of a WriteableBitmap or RenderTargetBitmap and then draw my PNG's to that sequence.
Does this sound OK, or should I not be getting the poor performance that I'm getting?
Actually, come to think of it, have you tried using this?
<DiffuseMatrial>
<DiffuseMaterial.Brush>
<VisualBrush ...>
<VisualBrush.Visual>
...
I know that MILCore handles VisualBrush by rendering the backing Visual as a separate operation, so I wouldn't be surprised if it worked very efficiently with 3D.
Update
It also occurs to me you might try:
<DiffuseMaterial>
<DiffuseMaterial.Brush>
<DrawingBrush ...>
<DrawingBrush.Drawing>
<ImageDrawing ImageSource="{Binding ...} />
This would bypass the use of Visual entirely and possibly run much faster than Viewport2DVisual3D or VisualBrush.
I haven't yet had a need to dig deeply into WPF's 3D optimizations, but I know that Direct3D is capable of rendering using a writable buffer so if MILCore implements it correctly your idea of WritableBitmap or RenderTargetBitmap have a reasonable chance of working. Vista's Flip 3D is able to make this work with high performance using arbitrary applications (even GDI applications) and also uses a writable buffer.
If that doesn't work, another idea for you is to convert your animation into a video, either the traditional way or by creating a DirectShow stream from a sequence of BitmapFrames.
Hopefully someone else can come along and give you a better answer.
If your PNGs are representing a video stream, why not convert them to a video format at the outset? Creating an AVI from frames is easy. Horses for courses, as they say. It could be the PNG decoder slowing you down.
Our graphics person uses Adobe Illustrator and we'd like to use her images inside our WPF application as paths. Is there a way to do this?
You can go from AI to SVG to XAML.
From Adobe Illustrator: File -> Save As -> *.SVG.
SVG "Profile 1.1" seems to be sufficient.
Note that to preserve path/group names in XAML you should enable "Preserve Illustrator Editing Capabilities" (or at least as it's called in CS4).
SharpVectors can convert SVG data to XAML data. This will produce a fragment of XAML with root <DrawingGroup>.
Do what you need to do to copy-paste and otherwise use the XAML, such as placing it into an Image like below. Named objects or groups in the AI file should still have their names in the XAML i.e. via x:Name="...".
<Image>
<Image.Source>
<DrawingImage>
<DrawingImage.Drawing>
<DrawingGroup ... the output from step #2 ...>...</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
</Image.Source>
</Image>
Coordinate systems can be a pain if you want to be animating things. There are some other posts such as this which may have insights.
Get her to export the illustrations as some other format (recent versions of Illustrator support SVG) that you can use or convert to something that will work.
The detour suggested by #ConcernedOfTunbridgeWells is starting to make sense now. Other solutions are not being maintained and do not work anymore.
Hence, you can use this option as workaround:
Save files as svg.
Convert them to XAML using Inkscape.
This solution even has the advantage of text will stay text and is not converted to a path.
How to convert many files?
Inkscape also supports a batch mode to convert many files at once. I took a great script (by Johannes Deml) for batch conversions on Windows that takes vectors files and converts them to various other formats using Inkscapes batch mode. I adapted it to convert to XAML, too.
You can find the script that includes XAML on Github. Some instructions on how to use the script are provided by the original author.