Ticket T710963
Visible to All Users

WPF SVG Converter

created 6 years ago

Hi DevExpress Team,
i have the following problem. I use the svg converter that you show in this support ticket:T602474. It worked well, when I put the images in the same directory as the view. But as u can see in the picture i have the views in another folder than the svg files. When I manipulate the Uri in the converter like this:

C#
public class SvgImageSourceConverterExtension : MarkupExtension, IValueConverter { class UriStreamHelper : SvgImageSourceExtension { public static Stream GetStream(Uri uri) { return CreateRequestAndGetResponseStream(uri); } } readonly Uri baseUri; readonly UriTypeConverter uriConverter; public static Uri Parent(Uri uri) { var newUri= new Uri(uri, Path.GetDirectoryName(uri.LocalPath) + "/"); return new Uri(newUri.AbsoluteUri.Remove(newUri.AbsoluteUri.Length - newUri.Segments[newUri.Segments.Length -1].Length - newUri.Query.Length) + "Rescources/"); } public SvgImageSourceConverterExtension() : this(null) { } public SvgImageSourceConverterExtension(Uri baseUri) { if (baseUri == null) { this.baseUri = baseUri; } else { var uri = new Uri(baseUri, "."); this.baseUri = Parent(uri); } uriConverter = new UriTypeConverter(); } public override object ProvideValue(IServiceProvider serviceProvider) { return new SvgImageSourceConverterExtension((serviceProvider.GetService(typeof(IUriContext)) as IUriContext)?.BaseUri); } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var uri = uriConverter.ConvertFrom(value) as Uri; if (uri == null) return null; var absoluteUri = uri.IsAbsoluteUri ? uri : new Uri(baseUri, uri); using (var stream = UriStreamHelper.GetStream(absoluteUri)) { object unused = null; var image = SvgImageHelper.GetOrCreateSvgImage(stream, ref unused); return WpfSvgRenderer.CreateImageSource(image, 1d, null, null, true); } } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }

It wanna find the file in the debug bin. The xaml, which use the converter is in the view folder. I use the converter to change the svg image if a property in my TableView change to false.I hope you can help me to fix this behaviour.

Answers approved by DevExpress Support

created 6 years ago

Hi,
I suggest that you use the absolute Uri path. In this case, you will be able to access your images regardless of the current location:

C#
new Uri("pack://application:,,,/WpfApp285;component/Images/img.png");

Please note that creating Uris is not directly related to our components. To learn more about this functionality, please refer to the Pack URIs in WPF MSDN article. Just in case, I have created a simple example to demonstrate that absolute Uris work even if the target image is located in a separate folder.

Thanks,
Alexander

    Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

    Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.