public static DrawingImage Open(string fileName) { using (FileStream stream = new FileStream(imagesPath + fileName, FileMode.Open, FileAccess.Read)) { return(SvgReader.Load(stream)); } }
private void LoadImage(Uri uri) { string path = uri.AbsolutePath; curr = uri; try { if (path.EndsWith(".svg")) { using (FileStream stream = File.OpenRead(path)) { //Convert svg to xaml so Image knows how to display it //Only takes one line--now that's my kind of library var img = SvgReader.Load(stream); display.Source = img; } } else { //Raster images aren't the point of this, but if we get one for some reason, we might as well do something useful display.Source = new BitmapImage(uri); } Title = $"SVG Viewer - {Path.GetFileName(path)}"; } catch (Exception) { //YOLO //But seriously, the only error handling it's worth it for us to bother with is keeping the program from crashing //so the user can try another file. Title = $"Could not show: {Path.GetFileName(path)}"; } }
/// <summary> /// 将SVG矢量图转换为可用资源 /// 用法<Image Source="{Binding ConverterParameter=pc_Button_search.svg, Converter={StaticResource SvgToXamlConverter}, Mode=OneWay}" /> /// </summary> /// <param name="value"></param> /// <param name="targetType"></param> /// <param name="parameter"></param> /// <param name="culture"></param> /// <returns></returns> public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { DrawingImage svg_image; string file_name = AppDomain.CurrentDomain.BaseDirectory + @"\Resources\" + parameter.ToString(); using (FileStream file_stream = new FileStream(file_name, FileMode.Open, FileAccess.Read)) svg_image = SvgReader.Load(file_stream, new SvgReaderOptions(false)); return(svg_image); }
private DrawingImage SvgToimge(string name) { DrawingImage svg_image; string file_name = AppDomain.CurrentDomain.BaseDirectory + @"\Resources\" + name.ToString(); using (FileStream file_stream = new FileStream(file_name, FileMode.Open, FileAccess.Read)) svg_image = SvgReader.Load(file_stream, new SvgReaderOptions(false)); return(svg_image); }
public static DrawingImage SvgIcon(String path) { string filePath = System.IO.Path.GetFullPath(path); using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { DrawingImage image = SvgReader.Load(stream); return(image); } }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (parameter == null) { return(null); } var directory = AppDomain.CurrentDomain.BaseDirectory; var path = directory + parameter; using var stream = new FileStream(path, FileMode.Open, FileAccess.Read); return(SvgReader.Load(stream)); }
public static ImageSource LoadSvg(string filename) { var info = Load(filename); if (info == null) { return(null); } using (var stream = info.Stream) { return(SvgReader.Load(stream)); } }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { // here you can return DrawingImage based on value that represents name field of your structure // just for example the piece of your code: if (value is string && !String.IsNullOrEmpty(value as string)) { return(SvgReader.Load(new MemoryStream(new System.Net.WebClient().DownloadData(value as string)))); } else { // if value is null or not of string type return(yourDefaultImage); } }
private async void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) { if (DataContext is WeatherForecastItem data) { WebClient wc = new WebClient(); using (MemoryStream stream = new MemoryStream(await wc.DownloadDataTaskAsync(new Uri(data.IconUrl)))) { imgWeather.OpacityMask = new ImageBrush(SvgReader.Load(stream)) { Stretch = Stretch.Uniform }; imgWeather.SetResourceReference(Image.SourceProperty, "EmptyImageDrawing"); } } }
//========================================================================== void Load(object state) { string file_name = state as string; try { DrawingImage svg_image; string svg; if (file_name.EndsWith("svgz")) { using (FileStream file_stream = new FileStream(file_name, FileMode.Open, FileAccess.Read)) using (GZipStream gzip_stream = new GZipStream(file_stream, CompressionMode.Decompress)) svg_image = SvgReader.Load(gzip_stream, new SvgReaderOptions(false)); using (FileStream file_stream = new FileStream(file_name, FileMode.Open, FileAccess.Read)) using (GZipStream gzip_stream = new GZipStream(file_stream, CompressionMode.Decompress)) svg = FormatXml(LoadXml(gzip_stream)); } else { using (FileStream file_stream = new FileStream(file_name, FileMode.Open, FileAccess.Read)) svg_image = SvgReader.Load(file_stream, new SvgReaderOptions(false)); using (FileStream file_stream = new FileStream(file_name, FileMode.Open, FileAccess.Read)) svg = FormatXml(LoadXml(file_stream)); } if (svg_image != null) { svg_image.Freeze(); string xaml = FormatXml(XamlWriter.Save(svg_image)); Dispatcher.Invoke((Action) delegate { PreviewImage.Source = svg_image; SvgTextBox.Text = svg; XamlTextBox.Text = xaml; }, DispatcherPriority.Background); } } catch (Exception exception) { } }
//========================================================================== public MainWindow() { InitializeComponent(); using (FileStream stream = new FileStream(FileName, FileMode.Open, FileAccess.Read)) //try { Image.Source = SvgReader.Load(stream); } /* * catch(Exception exception) * { * TextBlock error_text_block = new TextBlock(); * error_text_block.Text = exception.Message; * Content = error_text_block; * } * */ }
private DrawingImage LoadIcon(string iconPath) { string m_IconFontColor = "#FFFFFF"; XmlDocument svgXml = new XmlDocument(); //svgXml.Load("Icons/blizzard-f.svg"); svgXml.Load(iconPath); foreach (XmlElement pathElement in svgXml.GetElementsByTagName("path")) { pathElement.SetAttribute("fill", m_IconFontColor); } foreach (XmlElement pathElement in svgXml.GetElementsByTagName("circle")) { pathElement.SetAttribute("fill", m_IconFontColor); } using (XmlReader iconStream = XmlReader.Create(new System.IO.StringReader(svgXml.InnerXml))) { return(SvgReader.Load(iconStream)); } }
public void ShowTwoStepSecretKeyScreen(object obj) { dynamic data = obj; //result from async method if (data.IsMasterPassValid) { HideAllStepScreens(); TwoStepSecretKeyScreenVisibility = true; IncorrectMasterPasswordVisibility = false; } else { if (data.ForceShowingControl) { HideAllStepScreens(); TwoStepSecretKeyScreenVisibility = true; IncorrectMasterPasswordVisibility = false; } else { IncorrectMasterPasswordVisibility = true; } } SecretKey = data.TwoStepResp.auth.multi_factor_code; this.TwoStepBackupSecurityCode = data.TwoStepResp.auth.multi_factor_one_time_code; string twoStepQRResp = data.TwoStepResp.auth.qr; using (XmlReader xmlReader = XmlReader.Create(new StringReader(twoStepQRResp))) { var source = SvgReader.Load(xmlReader); source.Freeze(); BarcodeSource = source; } SecretKeyLoaded = true; }
private void LoadFiles(object arg) { int thumbnail_size = default(int); Dispatcher.Invoke((Action) delegate { thumbnail_size = ThumbnailSize; }); m_LoadThread = Thread.CurrentThread; int successful = 0; int errorneous = 0; while (true) { string file_name = null; int count; lock (m_LoadFiles) { count = m_LoadFiles.Count; if (count > 0) { file_name = m_LoadFiles.Dequeue(); } } if (file_name == null) { break; } try { SetProgress(100.0 * DirectoryEntriesListBox.Items.Count / (DirectoryEntriesListBox.Items.Count + count), "Loading {0}...", System.IO.Path.GetFileName(file_name)); DrawingImage svg_image = null; if (file_name.EndsWith("svgz")) { using (FileStream file_stream = new FileStream(file_name, FileMode.Open, FileAccess.Read)) using (GZipStream gzip_stream = new GZipStream(file_stream, CompressionMode.Decompress)) svg_image = SvgReader.Load(gzip_stream, new SvgReaderOptions(true)); } else { using (FileStream file_stream = new FileStream(file_name, FileMode.Open, FileAccess.Read)) svg_image = SvgReader.Load(file_stream, new SvgReaderOptions(true)); } DrawingVisual visual = new DrawingVisual(); using (DrawingContext drawing_context = visual.RenderOpen()) drawing_context.DrawRectangle( new ImageBrush(svg_image) { Stretch = Stretch.Uniform }, null, new Rect(0, 0, thumbnail_size, thumbnail_size)); RenderTargetBitmap render_bitmap = new RenderTargetBitmap(thumbnail_size, thumbnail_size, 96, 96, PixelFormats.Pbgra32); render_bitmap.Render(visual); PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(render_bitmap)); using (MemoryStream stream = new MemoryStream()) { encoder.Save(stream); BitmapImage bmp_image = new BitmapImage(); bmp_image.BeginInit(); bmp_image.StreamSource = stream; bmp_image.CacheOption = BitmapCacheOption.OnLoad; bmp_image.EndInit(); bmp_image.Freeze(); Dispatcher.Invoke((Action) delegate { Image image = new Image(); image.Width = thumbnail_size; image.Height = thumbnail_size; image.Source = bmp_image; ListBoxItem item = new ListBoxItem(); item.Content = image; item.ToolTip = System.IO.Path.GetFileName(file_name); item.Tag = file_name; item.PreviewMouseDoubleClick += DirectoryEntriesListBox_Item_PreviewMouseDoubleClick; DirectoryEntriesListBox.Items.Add(item); }, DispatcherPriority.Background); ++successful; } } catch (Exception exception) { Dispatcher.Invoke((Action) delegate { Border border = new Border(); border.Width = thumbnail_size; border.Height = thumbnail_size; border.Background = Brushes.Red; border.IsHitTestVisible = false; ListBoxItem item = new ListBoxItem(); item.Content = border; item.ToolTip = System.IO.Path.GetFileName(file_name); item.Tag = file_name; DirectoryEntriesListBox.Items.Add(item); JournalListBox.Items.Add(String.Format("{0}: {1}", file_name, exception.Message)); }, DispatcherPriority.Background); ++errorneous; } } Dispatcher.Invoke((Action) delegate { StatusProgressBar.Visibility = Visibility.Collapsed; StatusProgressBar.Value = 0; StatusTextBlock.Text = String.Format("Successfully loaded {0} files, {1} files could not be loaded properly", successful, errorneous); }, DispatcherPriority.Background); m_LoadThread = null; }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { // here you can return DrawingImage based on value that represents name field of your structure // just for example the piece of your code: return(SvgReader.Load(new MemoryStream(new System.Net.WebClient().DownloadData("http://upload.wikimedia.org/wikipedia/commons/c/c5/Logo_FC_Bayern_München.svg")))); }