private void LoadSvg(string svg) { // 1. Create conversion options var settings = new WpfDrawingSettings { IncludeRuntime = true, TextAsGeometry = false }; // 2. Select a file to be converted byte[] stringBytes = Encoding.UTF8.GetBytes(svg); using (var sourceStream = new MemoryStream(stringBytes)) // 3. Create a file reader using (var converter = new StreamSvgConverter(settings)) // 4. convert the SVG file using (var resultStream = new MemoryStream()) { if (converter.Convert(sourceStream, resultStream)) { BitmapImage bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.CacheOption = BitmapCacheOption.OnLoad; bitmap.StreamSource = resultStream; bitmap.EndInit(); // Set the image source. BitmapIcon = bitmap; } } }
private void OnWindowLoaded(object sender, RoutedEventArgs e) { // 1. Create conversion options WpfDrawingSettings settings = new WpfDrawingSettings(); settings.IncludeRuntime = true; settings.TextAsGeometry = false; // 2. Select a file to be converted string svgTestFile = "Test.svg"; // 3. Create a file reader StreamSvgConverter converter = new StreamSvgConverter(settings); // 4. convert the SVG file MemoryStream memStream = new MemoryStream(); if (converter.Convert(svgTestFile, memStream)) { BitmapImage bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.CacheOption = BitmapCacheOption.OnLoad; bitmap.StreamSource = memStream; bitmap.EndInit(); // Set the image source. svgImage.Source = bitmap; } memStream.Close(); }
/// <summary> /// 指定したインデックスのSVG画像をDrawingImageで返す /// </summary> /// <param name="index"></param> /// <returns></returns> private DrawingImage GetDrawingImage(string targetImagePath) { _currentBitmapImage = null; if (_drawingSettings == null) { _drawingSettings = new WpfDrawingSettings(); _drawingSettings.IncludeRuntime = true; _drawingSettings.TextAsGeometry = false; } using (var converter = new StreamSvgConverter(_drawingSettings)) using (MemoryStream ms = new MemoryStream()) { if (converter.Convert(targetImagePath, ms)) { _currentDrawingImage = new DrawingImage(converter.Drawing); return(_currentDrawingImage); } } return(null); }
private void SetImageElement(dynamic allData) { WpfDrawingSettings settings = new WpfDrawingSettings(); settings.IncludeRuntime = true; settings.TextAsGeometry = false; StreamSvgConverter converter = new StreamSvgConverter(settings); WebRequest request; var imagesElement = weatherInfoGrid.Children.OfType <Image>(); for (int i = 0; i < imagesElement.Count(); i++) { string iconDataRequestUriStr = "https://yastatic.net/weather/i/icons/blueye/color/svg/" + allData.forecasts[i].parts.day_short.icon + ".svg"; request = WebRequest.Create(iconDataRequestUriStr); using (MemoryStream memStream = new MemoryStream()) { using (Stream stream = request.GetResponse().GetResponseStream()) { if (converter.Convert(stream, memStream)) { BitmapImage bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.CacheOption = BitmapCacheOption.OnLoad; bitmap.StreamSource = memStream; bitmap.EndInit(); imagesElement.ElementAt(i).Source = bitmap; } } } } }