{"action":"closed","issue":{"url":"https://api.github.com/repos/vvvv/SVG/issues/207","repository_url":"https://api.github.com/repos/vvvv/SVG","labels_url":"https://api.github.com/repos/vvvv/SVG/issues/207/labels{/name}","comments_url":"https://api.github.com/repos/vvvv/SVG/issues/207/comments","events_url":"https://api.github.com/repos/vvvv/SVG/issues/207/events","html_url":"https://github.com/vvvv/SVG/issues/207","id":118054748,"node_id":"MDU6SXNzdWUxMTgwNTQ3NDg=","number":207,"title":"Image with data URI are not rendered - Possible Fix","user":{"login":"phissionmailed","id":6395013,"node_id":"MDQ6VXNlcjYzOTUwMTM=","avatar_url":"https://avatars1.githubusercontent.com/u/6395013?v=4","gravatar_id":"","url":"https://api.github.com/users/phissionmailed","html_url":"https://github.com/phissionmailed","followers_url":"https://api.github.com/users/phissionmailed/followers","following_url":"https://api.github.com/users/phissionmailed/following{/other_user}","gists_url":"https://api.github.com/users/phissionmailed/gists{/gist_id}","starred_url":"https://api.github.com/users/phissionmailed/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/phissionmailed/subscriptions","organizations_url":"https://api.github.com/users/phissionmailed/orgs","repos_url":"https://api.github.com/users/phissionmailed/repos","events_url":"https://api.github.com/users/phissionmailed/events{/privacy}","received_events_url":"https://api.github.com/users/phissionmailed/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2015-11-20T14:42:10Z","updated_at":"2018-12-22T17:16:58Z","closed_at":"2018-12-22T17:16:58Z","author_association":"NONE","body":"I ran into a problem where an image with a data URI was not getting rendered.\n\nI found that, if the base64 string of the image is too long, there's an error that prevents the image from getting rendered. This error is caught in class SvgElementFactory, method SetPropertyValue.\n\nThis line is the one throwing the error.\n\n``` c#\ndescriptor.SetValue(element, descriptor.Converter.ConvertFrom(document, CultureInfo.InvariantCulture, attributeValue));\n```\n\nI found that SvgImage class has a Href property declared as Uri. Uri doesn't seem to be able to handle long string, like those in data URI's. When .NET tries to convert the data URI into a Uri instance, it throws an error indicating the URI is too long.\n\nTo fix this, I first changed property Href data type to string.\n\nThen I changed method GetImage, also in SvgImage to look like the following, so it can handle data URI's and normal URI's.\n\n``` c#\nprotected object GetImage(string uriString)\n{\n    try\n    {\n        // handle data/uri embedded images (http://en.wikipedia.org/wiki/Data_URI_scheme)\n        if (uriString.StartsWith(\"data\"))\n        {\n            int dataIdx = uriString.IndexOf(\",\") + 1;\n            if (dataIdx <= 0 || dataIdx + 1 > uriString.Length)\n                throw new Exception(\"Invalid data URI\");\n\n            // we're assuming base64, as ascii encoding would be *highly* unsusual for images\n            // also assuming it's png or jpeg mimetype\n            byte[] imageBytes = Convert.FromBase64String(uriString.Substring(dataIdx));\n            using (var stream = new MemoryStream(imageBytes))\n            {\n                var sourceImage = Image.FromStream(stream);\n\n                //Make a copy of the image and return the copy.\n                //Do not return the image gotten from the stream because, after the stream is closed,\n                //the image cannot be used.\n                var result = new Bitmap(sourceImage.Width, sourceImage.Height, sourceImage.PixelFormat);\n\n                result.SetResolution(sourceImage.HorizontalResolution, sourceImage.VerticalResolution);\n\n                using (var graphics = Graphics.FromImage(result))\n                {\n                    graphics.DrawImage(sourceImage, 0, 0);\n                }\n\n                return result;\n            }\n        }\n\n        var uri = new Uri(uriString);\n\n        if (!uri.IsAbsoluteUri)\n        {\n            uri = new Uri(OwnerDocument.BaseUri, uri);\n        }\n\n        // should work with http: and file: protocol urls\n        var httpRequest = WebRequest.Create(uri);\n\n        using (WebResponse webResponse = httpRequest.GetResponse())\n        {\n            using (var stream = webResponse.GetResponseStream())\n            {\n                if (stream.CanSeek)\n                {\n                    stream.Position = 0;    \n                }\n                if (uri.LocalPath.EndsWith(\".svg\", StringComparison.InvariantCultureIgnoreCase))\n                {\n                    var doc = SvgDocument.Open<SvgDocument>(stream);\n                    doc.BaseUri = uri;\n                    return doc;\n                }\n                else\n                {\n                    return Bitmap.FromStream(stream);\n                }\n            }\n        }\n    }\n    catch (Exception ex)\n    {\n        Trace.TraceError(\"Error loading image: '{0}', error: {1} \", uriString, ex.Message);\n        return null;\n    }\n}\n```\n\nAfter this change, the image was rendered correctly and in the right place.\n\nHope this helps! Great job with the library by the way. Will download the new version if this fix is included, as well as the incorrectly placed text one I created in a separate issue.\n"}}
{"actor":{"display_login":"mrbean-bremen"}}