public void LoadXml(string xml) { try { // Worry about clearing the graphics nodes map... renderer.ClearMap(); System.GC.Collect(); System.Threading.Thread.Sleep(1); if (xml != null && xml.Length > 0) { if (xml != null && xml.Length > 0) { SvgDocument doc = window.CreateEmptySvgDocument(); doc.LoadXml(xml); window.Document = doc; SetupStyleSheets(); //JR if (this.AutoSize) { ISvgRect r = window.Document.RootElement.GetBBox(); this.Width = (int)r.Width; this.Height = (int)r.Height; } Render(); loaded = true; } } } catch (Exception e) { MessageBox.Show("An error occured while loading the document.\n" + e.Message); } }
protected virtual void Load() { try { if (!string.IsNullOrWhiteSpace(_svgSource)) { // Load the source _svgWindow.Source = _svgSource; // Initialize the style sheets SetupStyleSheets(); // Execute all script elements //UnloadEngines(); //InitializeEvents(); //ExecuteScripts(); SvgSvgElement svgEl = (SvgSvgElement)_svgWindow.Document.RootElement; SvgSizeF r = svgEl.GetSize(); int winWidth = (int)svgEl.Width.BaseVal.Value; int winHeight = (int)svgEl.Height.BaseVal.Value; if (!r.Width.Equals(0.0) && !r.Height.Equals(0.0) && (r.Width * 4 * r.Height) < BitmapLimit) { winWidth = (int)r.Width; winHeight = (int)r.Height; } if ((winWidth * 4 * winHeight) >= BitmapLimit) { winWidth = this.Width; winHeight = this.Height; } _svgWindow.Resize(winWidth, winHeight); _svgRenderer.InvalidRect = SvgRectF.Empty; this.Render(); _isSvgLoaded = true; } else if (!string.IsNullOrWhiteSpace(_xmlSource) && _xmlSource.Length > ValidSVG.Length) { SvgDocument doc = _svgWindow.CreateEmptySvgDocument(); doc.LoadXml(_xmlSource); _svgWindow.Document = doc; SetupStyleSheets(); SvgSvgElement svgEl = (SvgSvgElement)_svgWindow.Document.RootElement; SvgSizeF r = svgEl.GetSize(); int winWidth = (int)svgEl.Width.BaseVal.Value; int winHeight = (int)svgEl.Height.BaseVal.Value; if (!r.Width.Equals(0.0) && !r.Height.Equals(0.0) && (r.Width * 4 * r.Height) < BitmapLimit) { winWidth = (int)r.Width; winHeight = (int)r.Height; } if ((winWidth * 4 * winHeight) >= BitmapLimit) { winWidth = this.Width; winHeight = this.Height; } _svgWindow.Resize(winWidth, winHeight); _svgRenderer.InvalidRect = SvgRectF.Empty; this.Render(); _isSvgLoaded = true; } else if (_uriSource != null) { // Load the source _svgWindow.Source = _uriSource.AbsoluteUri; // Initialize the style sheets SetupStyleSheets(); // Execute all script elements //UnloadEngines(); //InitializeEvents(); //ExecuteScripts(); SvgSvgElement svgEl = (SvgSvgElement)_svgWindow.Document.RootElement; SvgSizeF r = svgEl.GetSize(); int winWidth = (int)svgEl.Width.BaseVal.Value; int winHeight = (int)svgEl.Height.BaseVal.Value; if (!r.Width.Equals(0.0) && !r.Height.Equals(0.0) && (r.Width * 4 * r.Height) < BitmapLimit) { winWidth = (int)r.Width; winHeight = (int)r.Height; } if ((winWidth * 4 * winHeight) >= BitmapLimit) { winWidth = this.Width; winHeight = this.Height; } _svgWindow.Resize(winWidth, winHeight); _svgRenderer.InvalidRect = SvgRectF.Empty; this.Render(); _isSvgLoaded = true; } else if (_streamSource != null) { SvgDocument doc = _svgWindow.CreateEmptySvgDocument(); doc.Load(_streamSource); _svgWindow.Document = doc; SetupStyleSheets(); SvgSvgElement svgEl = (SvgSvgElement)_svgWindow.Document.RootElement; SvgSizeF r = svgEl.GetSize(); int winWidth = (int)svgEl.Width.BaseVal.Value; int winHeight = (int)svgEl.Height.BaseVal.Value; if (!r.Width.Equals(0.0) && !r.Height.Equals(0.0) && (r.Width * 4 * r.Height) < BitmapLimit) { winWidth = (int)r.Width; winHeight = (int)r.Height; } if ((winWidth * 4 * winHeight) >= BitmapLimit) { winWidth = this.Width; winHeight = this.Height; } _svgWindow.Resize(winWidth, winHeight); _svgRenderer.InvalidRect = SvgRectF.Empty; this.Render(); _isSvgLoaded = true; } } catch (Exception ex) { Trace.TraceError(ex.ToString()); if (this.DesignMode) { return; } var errorArgs = new SvgErrorArgs("An error occurred while loading the document", ex); _svgErrors?.Invoke(this, errorArgs); if (!errorArgs.Handled) { MessageBox.Show(errorArgs.Message + ": " + ex.Message, _appTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); } } }