Skip to content

Commit

Permalink
Minimal image tag support for raster images
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay committed Dec 17, 2013
1 parent a6baed2 commit fdaeac0
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 11 deletions.
76 changes: 65 additions & 11 deletions Source/Basic Shapes/SvgImage.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
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
{
Expand Down Expand Up @@ -91,14 +94,65 @@ protected set

/// <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
{
// should work with http: and file: protocol urls
var httpRequest = WebRequest.Create(uri);

using (WebResponse webResponse = httpRequest.GetResponse())
{
MemoryStream ms = BufferToMemoryStream(webResponse.GetResponseStream());
Image b = Bitmap.FromStream(ms);
return b;
}
}
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;
}


Expand Down
5 changes: 5 additions & 0 deletions Source/SvgRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public void DrawImageUnscaled(Image image, Point location)
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);
Expand Down

0 comments on commit fdaeac0

Please sign in to comment.