Skip to content

Commit cf08dc0

Browse files
authored
Merge pull request #35 from paulushub/master
Bug Fix: ArgumentNullException bug in gradient rendering
2 parents 1cfd10b + 87ef2a1 commit cf08dc0

File tree

8 files changed

+62
-33
lines changed

8 files changed

+62
-33
lines changed

Samples/ClipArtViewer/MainWindow.xaml.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ protected override void OnContentRendered(EventArgs e)
9393

9494
ListFiles();
9595
DataContext = this;
96-
97-
tabPages.SelectedIndex = 0;
9896
}
9997

10098
private void OnMainWindowLoaded(object sender, RoutedEventArgs e)
@@ -109,6 +107,9 @@ private void OnMainWindowLoaded(object sender, RoutedEventArgs e)
109107
}
110108

111109
Trace.WriteLine("");
110+
111+
m_filelist.SelectedIndex = 0;
112+
tabPages.SelectedIndex = 0;
112113
}
113114

114115
private void OnMainWindowClosing(object sender, System.ComponentModel.CancelEventArgs e)

Source/SVGImage/SVG/FileLoaders/FileSystemLoader.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Diagnostics;
34

45
namespace DotNetProjects.SVGImage.SVG.FileLoaders
56
{
@@ -22,6 +23,9 @@ public Stream LoadFile(string hRef, string svgFilename)
2223
string filename = Path.Combine(path, hRef);
2324
if (File.Exists(filename))
2425
return File.OpenRead(filename);
26+
27+
Trace.TraceWarning("Unresolved URI: " + hRef);
28+
2529
return null;
2630
}
2731
}

Source/SVGImage/SVG/PaintServer/GradientColorPaintServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public GradientColorPaintServer(PaintServerManager owner, XmlNode node) : base(o
2828
{
2929
string refid = XmlUtil.AttrValue(node, "xlink:href", string.Empty);
3030
string paintServerKey = owner.Parse(refid.Substring(1));
31-
GradientColorPaintServer refcol = owner.GetServers()[paintServerKey] as GradientColorPaintServer;
31+
GradientColorPaintServer refcol = owner.GetServer(paintServerKey) as GradientColorPaintServer;
3232
if (refcol == null) return;
3333
this.m_stops = new List<GradientStop>(refcol.m_stops);
3434
}

Source/SVGImage/SVG/PaintServer/LinearGradientColorPaintServer.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
using System.Windows;
1+
using System.Xml;
2+
3+
using System.Windows;
24
using System.Windows.Media;
3-
using System.Xml;
45

56
namespace SVGImage.SVG.PaintServer
67
{
@@ -28,6 +29,7 @@ public LinearGradientColorPaintServerPaintServer(PaintServerManager owner, Brush
2829
{
2930
Brush = newBrush;
3031
}
32+
3133
public override Brush GetBrush(double opacity, SVG svg, SVGRender svgRender, Rect bounds)
3234
{
3335
if (this.Brush != null) return this.Brush;
@@ -36,30 +38,30 @@ public override Brush GetBrush(double opacity, SVG svg, SVGRender svgRender, Rec
3638
foreach (GradientStop stop in this.Stops) b.GradientStops.Add(stop);
3739

3840
b.MappingMode = BrushMappingMode.RelativeToBoundingBox;
39-
b.StartPoint = new System.Windows.Point(0, 0);
40-
b.EndPoint = new System.Windows.Point(1, 0);
41+
b.StartPoint = new Point(0, 0);
42+
b.EndPoint = new Point(1, 0);
4143

4244
if (this.GradientUnits == SVGTags.sUserSpaceOnUse)
4345
{
4446
Transform tr = this.Transform as Transform;
4547
if (tr != null)
4648
{
47-
b.StartPoint = tr.Transform(new System.Windows.Point(this.X1, this.Y1));
48-
b.EndPoint = tr.Transform(new System.Windows.Point(this.X2, this.Y2));
49+
b.StartPoint = tr.Transform(new Point(this.X1, this.Y1));
50+
b.EndPoint = tr.Transform(new Point(this.X2, this.Y2));
4951
}
5052
else
5153
{
52-
b.StartPoint = new System.Windows.Point(this.X1, this.Y1);
53-
b.EndPoint = new System.Windows.Point(this.X2, this.Y2);
54+
b.StartPoint = new Point(this.X1, this.Y1);
55+
b.EndPoint = new Point(this.X2, this.Y2);
5456
}
5557
this.Transform = null;
5658
b.MappingMode = BrushMappingMode.Absolute;
5759
}
5860
else
5961
{
6062
this.Normalize();
61-
if (double.IsNaN(this.X1) == false) b.StartPoint = new System.Windows.Point(this.X1, this.Y1);
62-
if (double.IsNaN(this.X2) == false) b.EndPoint = new System.Windows.Point(this.X2, this.Y2);
63+
if (double.IsNaN(this.X1) == false) b.StartPoint = new Point(this.X1, this.Y1);
64+
if (double.IsNaN(this.X2) == false) b.EndPoint = new Point(this.X2, this.Y2);
6365
}
6466

6567
this.Brush = b;

Source/SVGImage/SVG/PaintServer/PaintServerManager.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,21 @@ public string Create(SVG svg, XmlNode node)
3737

3838
public void AddServer(string key, PaintServer server)
3939
{
40+
if (string.IsNullOrWhiteSpace(key))
41+
{
42+
return;
43+
}
44+
4045
m_servers[key] = server;
4146
}
4247

4348
public PaintServer GetServer(string serverKey)
4449
{
50+
if (string.IsNullOrWhiteSpace(serverKey))
51+
{
52+
return null;
53+
}
54+
4555
PaintServer result;
4656
if (serverKey != null && m_servers.TryGetValue(serverKey, out result)) return result;
4757
else return null;

Source/SVGImage/SVG/PaintServer/RadialGradientColorPaintServer.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
using System.Windows;
1+
using System.Xml;
2+
3+
using System.Windows;
24
using System.Windows.Media;
3-
using System.Xml;
45

56
namespace SVGImage.SVG.PaintServer
67
{
@@ -40,15 +41,15 @@ public override Brush GetBrush(double opacity, SVG svg, SVGRender svgRender, Rec
4041
RadialGradientBrush b = new RadialGradientBrush();
4142
foreach (GradientStop stop in this.Stops) b.GradientStops.Add(stop);
4243

43-
b.GradientOrigin = new System.Windows.Point(0.5, 0.5);
44-
b.Center = new System.Windows.Point(0.5, 0.5);
44+
b.GradientOrigin = new Point(0.5, 0.5);
45+
b.Center = new Point(0.5, 0.5);
4546
b.RadiusX = 0.5;
4647
b.RadiusY = 0.5;
4748

4849
if (this.GradientUnits == SVGTags.sUserSpaceOnUse)
4950
{
50-
b.Center = new System.Windows.Point(this.CX, this.CY);
51-
b.GradientOrigin = new System.Windows.Point(this.FX, this.FY);
51+
b.Center = new Point(this.CX, this.CY);
52+
b.GradientOrigin = new Point(this.FX, this.FY);
5253
b.RadiusX = this.R;
5354
b.RadiusY = this.R;
5455
b.MappingMode = BrushMappingMode.Absolute;
@@ -58,12 +59,12 @@ public override Brush GetBrush(double opacity, SVG svg, SVGRender svgRender, Rec
5859
double scale = 1d / 100d;
5960
if (double.IsNaN(this.CX) == false && double.IsNaN(this.CY) == false)
6061
{
61-
//b.GradientOrigin = new System.Windows.Point(this.CX*scale, this.CY*scale);
62-
b.Center = new System.Windows.Point(this.CX /* *scale */, this.CY /* *scale */);
62+
//b.GradientOrigin = new Point(this.CX*scale, this.CY*scale);
63+
b.Center = new Point(this.CX /* *scale */, this.CY /* *scale */);
6364
}
6465
if (double.IsNaN(this.FX) == false && double.IsNaN(this.FY) == false)
6566
{
66-
b.GradientOrigin = new System.Windows.Point(this.FX * scale, this.FY * scale);
67+
b.GradientOrigin = new Point(this.FX * scale, this.FY * scale);
6768
}
6869
if (double.IsNaN(this.R) == false)
6970
{

Source/SVGImage/SVG/Shapes/ImageShape.cs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,44 @@ public class ImageShape : Shape
2222

2323
public ImageShape(SVG svg, XmlNode node) : base(svg, node)
2424
{
25-
this.X = XmlUtil.AttrValue(node, "x", 0, svg.Size.Width);
26-
this.Y = XmlUtil.AttrValue(node, "y", 0, svg.Size.Height);
27-
this.Width = XmlUtil.AttrValue(node, "width", 0, svg.Size.Width);
25+
this.X = XmlUtil.AttrValue(node, "x", 0, svg.Size.Width);
26+
this.Y = XmlUtil.AttrValue(node, "y", 0, svg.Size.Height);
27+
this.Width = XmlUtil.AttrValue(node, "width", 0, svg.Size.Width);
2828
this.Height = XmlUtil.AttrValue(node, "height", 0, svg.Size.Height);
2929
string hRef = XmlUtil.AttrValue(node, "xlink:href", string.Empty);
3030
if (hRef.Length > 0)
3131
{
3232
try
3333
{
34-
BitmapImage b = new BitmapImage();
35-
b.BeginInit();
36-
if (hRef.StartsWith("data:image/png;base64"))
34+
Stream imageStream = null;
35+
if (hRef.StartsWith("data:image/png;base64", StringComparison.OrdinalIgnoreCase))
3736
{
38-
b.StreamSource = new MemoryStream(Convert.FromBase64String(hRef.Substring("data:image/png;base64,".Length)));
37+
var embeddedImage = hRef.Substring("data:image/png;base64,".Length);
38+
if (!string.IsNullOrWhiteSpace(embeddedImage))
39+
{
40+
imageStream = new MemoryStream(Convert.FromBase64String(embeddedImage));
41+
}
3942
}
4043
else
4144
{
4245
if (svg.ExternalFileLoader != null)
43-
b.StreamSource = svg.ExternalFileLoader.LoadFile(hRef, svg.Filename);
46+
{
47+
imageStream = svg.ExternalFileLoader.LoadFile(hRef, svg.Filename);
48+
}
4449
}
50+
if (imageStream != null)
51+
{
52+
BitmapImage b = new BitmapImage();
53+
b.BeginInit();
54+
b.StreamSource = imageStream;
55+
b.EndInit();
4556

46-
b.EndInit();
47-
this.ImageSource = b;
57+
this.ImageSource = b;
58+
}
4859
}
4960
catch (Exception ex)
5061
{
51-
Trace.TraceError(ex.Message);
62+
Trace.TraceError(ex.ToString());
5263
}
5364
}
5465
}

Tests/Images/CodeProjectLogo.jpg

8.18 KB
Loading

0 commit comments

Comments
 (0)