Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ImportedProjects
SVG
Commits
4cc1d9e7
Commit
4cc1d9e7
authored
Dec 19, 2013
by
Tebjan Halm
Browse files
Merge pull request #38 from planetclegg/master
Minimal image tag support for raster images
parents
a6baed2f
6f53360e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Source/Basic Shapes/SvgImage.cs
View file @
4cc1d9e7
using
System
;
using
System
;
using
System.Diagnostics
;
using
System.Drawing
;
using
System.Drawing.Drawing2D
;
using
Svg.Transforms
;
using
System.Drawing.Drawing2D
;
using
System.IO
;
using
System.Net
;
using
Svg.Transforms
;
namespace
Svg
{
...
...
@@ -91,14 +94,80 @@ namespace Svg
/// <summary>
/// Renders the <see cref="SvgElement"/> and contents to the specified <see cref="Graphics"/> object.
/// </summary>
protected
override
void
Render
(
SvgRenderer
renderer
)
{
if
(
Width
.
Value
>
0.0f
&&
Height
.
Value
>
0.0f
)
{
//TODO:
//base.Render(renderer);
}
/// </summary>
protected
override
void
Render
(
SvgRenderer
renderer
)
{
if
(
Width
.
Value
>
0.0f
&&
Height
.
Value
>
0.0f
&&
this
.
Href
!=
null
)
{
using
(
Image
b
=
GetImage
(
this
.
Href
))
{
if
(
b
!=
null
)
{
this
.
PushTransforms
(
renderer
);
this
.
SetClip
(
renderer
);
RectangleF
srcRect
=
new
RectangleF
(
0
,
0
,
b
.
Width
,
b
.
Height
);
var
destRect
=
new
RectangleF
(
this
.
Location
.
ToDeviceValue
(),
new
SizeF
(
Width
.
ToDeviceValue
(),
Height
.
ToDeviceValue
()));
renderer
.
DrawImage
(
b
,
destRect
,
srcRect
,
GraphicsUnit
.
Pixel
);
this
.
ResetClip
(
renderer
);
this
.
PopTransforms
(
renderer
);
}
}
// TODO: cache images... will need a shared context for this
// TODO: support preserveAspectRatio, etc
}
}
protected
Image
GetImage
(
Uri
uri
)
{
try
{
// handle data/uri embedded images (http://en.wikipedia.org/wiki/Data_URI_scheme)
if
(
uri
.
Scheme
==
"data"
)
{
string
uriString
=
uri
.
OriginalString
;
int
dataIdx
=
uriString
.
IndexOf
(
","
)
+
1
;
if
(
dataIdx
<=
0
||
dataIdx
+
1
>
uriString
.
Length
)
throw
new
Exception
(
"Invalid data URI"
);
// we're assuming base64, as ascii encoding would be *highly* unsusual for images
// also assuming it's png or jpeg mimetype
byte
[]
imageBytes
=
Convert
.
FromBase64String
(
uriString
.
Substring
(
dataIdx
));
Image
image
=
Image
.
FromStream
(
new
MemoryStream
(
imageBytes
));
return
image
;
}
// should work with http: and file: protocol urls
var
httpRequest
=
WebRequest
.
Create
(
uri
);
using
(
WebResponse
webResponse
=
httpRequest
.
GetResponse
())
{
MemoryStream
ms
=
BufferToMemoryStream
(
webResponse
.
GetResponseStream
());
Image
image
=
Bitmap
.
FromStream
(
ms
);
return
image
;
}
}
catch
(
Exception
ex
)
{
Trace
.
TraceError
(
"Error loading image: '{0}', error: {1} "
,
uri
,
ex
.
Message
);
return
null
;
}
}
protected
static
MemoryStream
BufferToMemoryStream
(
Stream
input
)
{
byte
[]
buffer
=
new
byte
[
4
*
1024
];
int
len
;
MemoryStream
ms
=
new
MemoryStream
();
while
((
len
=
input
.
Read
(
buffer
,
0
,
buffer
.
Length
))
>
0
)
{
ms
.
Write
(
buffer
,
0
,
len
);
}
ms
.
Seek
(
0
,
SeekOrigin
.
Begin
);
return
ms
;
}
...
...
Source/SvgRenderer.cs
View file @
4cc1d9e7
...
...
@@ -52,6 +52,11 @@ namespace Svg
this
.
_innerGraphics
.
DrawImageUnscaled
(
image
,
location
);
}
public
void
DrawImage
(
Image
image
,
RectangleF
destRect
,
RectangleF
srcRect
,
GraphicsUnit
graphicsUnit
)
{
_innerGraphics
.
DrawImage
(
image
,
destRect
,
srcRect
,
graphicsUnit
);
}
public
void
SetClip
(
Region
region
)
{
this
.
_innerGraphics
.
SetClip
(
region
,
CombineMode
.
Complement
);
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment