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
b96c50c3
Commit
b96c50c3
authored
Jul 27, 2015
by
ubbn
Browse files
Add new Draw() method that can take raster size and render image in that given size
parent
2c3265fc
Changes
1
Hide whitespace changes
Inline
Side-by-side
Source/SvgDocument.cs
View file @
b96c50c3
...
...
@@ -490,6 +490,61 @@ namespace Svg
//Trace.TraceInformation("End Render");
}
/// <summary>
/// Renders the <see cref="SvgDocument"/> in given size and returns the image as a <see cref="Bitmap"/>.
/// </summary>
/// <returns>A <see cref="Bitmap"/> containing the rendered document.</returns>
public
virtual
Bitmap
Draw
(
int
rasterWidth
,
int
rasterHeight
)
{
var
size
=
GetDimensions
();
RasterizeDimensions
(
ref
size
,
rasterWidth
,
rasterHeight
);
if
(
size
.
Width
==
0
||
size
.
Height
==
0
)
return
null
;
var
bitmap
=
new
Bitmap
((
int
)
Math
.
Round
(
size
.
Width
),
(
int
)
Math
.
Round
(
size
.
Height
));
try
{
Draw
(
bitmap
);
}
catch
{
bitmap
.
Dispose
();
throw
;
}
//Trace.TraceInformation("End Render");
return
bitmap
;
}
/// <summary>
/// If both or one of raster height and width is not given (0), calculate that missing value from original SVG size
/// while keeping original SVG size ratio
/// </summary>
/// <param name="size"></param>
/// <param name="rasterWidth"></param>
/// <param name="rasterHeight"></param>
public
virtual
void
RasterizeDimensions
(
ref
SizeF
size
,
int
rasterWidth
,
int
rasterHeight
)
{
if
(
size
==
null
||
size
.
Width
==
0
)
return
;
// Ratio of height/width of the original SVG size, to be used for scaling transformation
float
ratio
=
size
.
Height
/
size
.
Width
;
size
.
Width
=
rasterWidth
>
0
?
(
float
)
rasterWidth
:
size
.
Width
;
size
.
Height
=
rasterHeight
>
0
?
(
float
)
rasterHeight
:
size
.
Height
;
if
(
rasterHeight
==
0
&&
rasterWidth
>
0
)
{
size
.
Height
=
(
int
)(
rasterWidth
*
ratio
);
}
else
if
(
rasterHeight
>
0
&&
rasterWidth
==
0
)
{
size
.
Width
=
(
int
)(
rasterHeight
/
ratio
);
}
}
public
override
void
Write
(
XmlTextWriter
writer
)
{
//Save previous culture and switch to invariant for writing
...
...
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