1

Resolved

data: URI support for drawImage

description

When drawImage is called with a DOM Image that was loaded with a data: URI, the call fails because support for this scheme is not built in to the BitmapImage class. I've implemented a fix for this, but because I am not on Windows, I cannot run the CodePlex client. Basically, the following 2 methods can be added to the Utility class, and the 3 drawImage overloads can then call DomImageSource instead of instantiating BitmapImage directly. Hope this proves helpful for someone else!
 
<code>
    public static BitmapSource DomImageSource(HtmlElement img)
    {
        Uri src = new Uri(HtmlPage.Document.DocumentUri, img.GetAttribute("src"));

        if (src.Scheme == "data")
            return BitmapSourceForDataUri(src);
        else
            return new BitmapImage(src);
    }

    public static BitmapSource BitmapSourceForDataUri(Uri dataUri)
    {
        string dataString = dataUri.OriginalString;

        int b64Start = dataString.IndexOf(",");
        if (b64Start == -1) return null;
        string base64 = dataString.Substring(b64Start + 1);

        Stream stream = new MemoryStream(Convert.FromBase64String(base64));
        BitmapImage image = new BitmapImage();
        image.SetSource(stream);
        return image;
    }
</code>

file attachments

comments

stimpy77 wrote May 31, 2010 at 10:44 PM

Thanks. FYI, CodePlex has a Subversion interface (although you'd still have to be added as a contributor in order to check in), or you can produce patch files and upload them.

wrote Jun 3, 2010 at 2:04 AM

corrado wrote Jun 3, 2010 at 2:04 AM

Thanks for the tip on the SVN interface. That makes things much easier! Here's a patch for what I tried to explain above.

stimpy77 wrote Jun 3, 2010 at 3:38 AM

It was just a suggestion. You can upload patches here: http://slcanvas.codeplex.com/SourceControl/UploadPatch.aspx or by clicking on the Source Code tab and then clicking "Upload patch". However, I put it there already and someone (probably myself) will review your contribution soon.

wrote Jun 4, 2010 at 7:45 AM

stimpy77 wrote Jun 4, 2010 at 7:46 AM

Merged.

wrote Feb 13, 2013 at 8:42 PM

wrote May 16, 2013 at 3:10 AM

wrote May 16, 2013 at 3:10 AM

wrote Jun 14, 2013 at 7:29 AM