1 | using System;
|
---|
2 | using System.IO;
|
---|
3 | using System.Drawing;
|
---|
4 | using System.Drawing.Imaging;
|
---|
5 | using System.Drawing.Drawing2D;
|
---|
6 |
|
---|
7 | using SharpVectors.Net;
|
---|
8 | using SharpVectors.Dom.Svg;
|
---|
9 |
|
---|
10 | namespace SharpVectors.Renderers.Gdi
|
---|
11 | {
|
---|
12 | /// <summary>
|
---|
13 | /// Summary description for SvgImageGraphicsNode.
|
---|
14 | /// </summary>
|
---|
15 | public sealed class GdiImageRendering : GdiRendering
|
---|
16 | {
|
---|
17 | #region Private Fields
|
---|
18 |
|
---|
19 | private GdiGraphicsRenderer _embeddedRenderer;
|
---|
20 |
|
---|
21 | #endregion
|
---|
22 |
|
---|
23 | #region Constructors and Destructor
|
---|
24 |
|
---|
25 | public GdiImageRendering(SvgElement element)
|
---|
26 | : base(element)
|
---|
27 | {
|
---|
28 | }
|
---|
29 |
|
---|
30 | #endregion
|
---|
31 |
|
---|
32 | #region Public Methods
|
---|
33 |
|
---|
34 | public override void Render(GdiGraphicsRenderer renderer)
|
---|
35 | {
|
---|
36 | GdiGraphicsWrapper graphics = renderer.GraphicsWrapper;
|
---|
37 | SvgImageElement iElement = (SvgImageElement) element;
|
---|
38 |
|
---|
39 | ImageAttributes imageAttributes = new ImageAttributes();
|
---|
40 |
|
---|
41 | string sOpacity = iElement.GetPropertyValue("opacity");
|
---|
42 | if (sOpacity != null && sOpacity.Length > 0)
|
---|
43 | {
|
---|
44 | double opacity = SvgNumber.ParseNumber(sOpacity);
|
---|
45 | ColorMatrix myColorMatrix = new ColorMatrix();
|
---|
46 | myColorMatrix.Matrix00 = 1.00f; // Red
|
---|
47 | myColorMatrix.Matrix11 = 1.00f; // Green
|
---|
48 | myColorMatrix.Matrix22 = 1.00f; // Blue
|
---|
49 | myColorMatrix.Matrix33 = (float)opacity; // alpha
|
---|
50 | myColorMatrix.Matrix44 = 1.00f; // w
|
---|
51 |
|
---|
52 | imageAttributes.SetColorMatrix(myColorMatrix,
|
---|
53 | ColorMatrixFlag.Default,ColorAdjustType.Bitmap);
|
---|
54 | }
|
---|
55 |
|
---|
56 | float width = (float)iElement.Width.AnimVal.Value;
|
---|
57 | float height = (float)iElement.Height.AnimVal.Value;
|
---|
58 |
|
---|
59 | Rectangle destRect = new Rectangle(Convert.ToInt32(iElement.X.AnimVal.Value),
|
---|
60 | Convert.ToInt32(iElement.Y.AnimVal.Value),
|
---|
61 | Convert.ToInt32(width), Convert.ToInt32(height));
|
---|
62 |
|
---|
63 | Image image = null;
|
---|
64 | if (iElement.IsSvgImage)
|
---|
65 | {
|
---|
66 | SvgWindow wnd = GetSvgWindow();
|
---|
67 | _embeddedRenderer.BackColor = Color.Empty;
|
---|
68 | _embeddedRenderer.Render(wnd.Document);
|
---|
69 |
|
---|
70 | image = _embeddedRenderer.RasterImage;
|
---|
71 | }
|
---|
72 | else
|
---|
73 | {
|
---|
74 | image = GetBitmap(iElement);
|
---|
75 | }
|
---|
76 |
|
---|
77 | if (image != null)
|
---|
78 | {
|
---|
79 | graphics.DrawImage(this, image, destRect, 0f, 0f,
|
---|
80 | image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes);
|
---|
81 |
|
---|
82 | image.Dispose();
|
---|
83 | image = null;
|
---|
84 | }
|
---|
85 |
|
---|
86 | if (_embeddedRenderer != null)
|
---|
87 | {
|
---|
88 | _embeddedRenderer.Dispose();
|
---|
89 | _embeddedRenderer = null;
|
---|
90 | }
|
---|
91 |
|
---|
92 | if (imageAttributes != null)
|
---|
93 | {
|
---|
94 | imageAttributes.Dispose();
|
---|
95 | imageAttributes = null;
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | #endregion
|
---|
100 |
|
---|
101 | #region Private Methods
|
---|
102 |
|
---|
103 | private SvgWindow GetSvgWindow()
|
---|
104 | {
|
---|
105 | if (_embeddedRenderer == null)
|
---|
106 | {
|
---|
107 | _embeddedRenderer = new GdiGraphicsRenderer();
|
---|
108 | }
|
---|
109 |
|
---|
110 | SvgImageElement iElm = this.Element as SvgImageElement;
|
---|
111 | SvgWindow wnd = iElm.SvgWindow;
|
---|
112 | wnd.Renderer = _embeddedRenderer;
|
---|
113 |
|
---|
114 | _embeddedRenderer.Window = wnd;
|
---|
115 |
|
---|
116 | return wnd;
|
---|
117 | }
|
---|
118 |
|
---|
119 | private Image GetBitmap(SvgImageElement element)
|
---|
120 | {
|
---|
121 | if (!element.IsSvgImage)
|
---|
122 | {
|
---|
123 | if (!element.Href.AnimVal.StartsWith("data:"))
|
---|
124 | {
|
---|
125 | SvgUriReference svgUri = element.UriReference;
|
---|
126 | Uri imageUri = new Uri(svgUri.AbsoluteUri);
|
---|
127 | if (imageUri.IsFile && File.Exists(imageUri.LocalPath))
|
---|
128 | {
|
---|
129 | return Bitmap.FromFile(imageUri.LocalPath);
|
---|
130 | }
|
---|
131 |
|
---|
132 | Stream stream = svgUri.ReferencedResource.GetResponseStream();
|
---|
133 | return Bitmap.FromStream(stream);
|
---|
134 | }
|
---|
135 | else
|
---|
136 | {
|
---|
137 | string sURI = element.Href.AnimVal;
|
---|
138 | int nColon = sURI.IndexOf(":");
|
---|
139 | int nSemiColon = sURI.IndexOf(";");
|
---|
140 | int nComma = sURI.IndexOf(",");
|
---|
141 |
|
---|
142 | string sMimeType = sURI.Substring(nColon + 1, nSemiColon - nColon - 1);
|
---|
143 |
|
---|
144 | string sContent = sURI.Substring(nComma + 1);
|
---|
145 | byte[] bResult = Convert.FromBase64CharArray(sContent.ToCharArray(),
|
---|
146 | 0, sContent.Length);
|
---|
147 |
|
---|
148 | MemoryStream ms = new MemoryStream(bResult);
|
---|
149 |
|
---|
150 | return Bitmap.FromStream(ms);
|
---|
151 | }
|
---|
152 | }
|
---|
153 | else
|
---|
154 | {
|
---|
155 | return null;
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | #endregion
|
---|
160 | }
|
---|
161 | }
|
---|