This project has moved and is read-only. For the latest updates, please go here.

FileSvgReader image locked

Topics: SVG, SVG# and Parsing
Oct 23, 2013 at 5:29 PM
Hi,
I am using FileSvgReader to read an svg file that include a reference to external image jpg:
<image x="0.0" y="0.0" width="640px" height="480px" xlink:href="camera2_807.jpg"/>

After read svg file, I am not able to access to camera2_802.jpg file (delete or overwrite functions), I receive an exception that file is locked from an other process.

My goal is create an jpg file with same name, using FileSvgReader.SaveImage, but I not able to access to camera2_807.jpg.

someone can help me ?
Coordinator
Oct 24, 2013 at 3:10 PM
Hello,
Try this. In the SharpVectors.Rendering.Wpf project, open the file WpfImageRendering.cs and replace the GetBitmap method with this code shown below:
        private BitmapSource GetBitmap(SvgImageElement element, WpfDrawingContext context)
        {
            if (element.IsSvgImage)
            {
                return null;
            }

            if (!element.Href.AnimVal.StartsWith("data:"))
            {
                SvgUriReference svgUri = element.UriReference;
                string absoluteUri = svgUri.AbsoluteUri;
                if (String.IsNullOrEmpty(absoluteUri))
                {
                    return null; // most likely, the image does not exist...
                }

                Uri imageUri = new Uri(svgUri.AbsoluteUri);
                if (imageUri.IsFile)
                {
                    if (File.Exists(imageUri.LocalPath))
                    {
                        BitmapImage imageSource = new BitmapImage();

                        imageSource.BeginInit();
                        imageSource.CacheOption = BitmapCacheOption.OnLoad;
                        imageSource.CreateOptions = BitmapCreateOptions.IgnoreImageCache 
                            | BitmapCreateOptions.PreservePixelFormat;
                        imageSource.UriSource     = imageUri;
                        imageSource.EndInit();

                        return imageSource;
                    }

                    return null;
                }
                else
                {
                    Stream stream = svgUri.ReferencedResource.GetResponseStream();

                    BitmapImage imageSource = new BitmapImage();
                    imageSource.BeginInit();
                    imageSource.CacheOption   = BitmapCacheOption.OnLoad;
                    imageSource.CreateOptions = BitmapCreateOptions.IgnoreImageCache
                        | BitmapCreateOptions.PreservePixelFormat;
                    imageSource.StreamSource = stream;
                    imageSource.EndInit();

                    return imageSource;
                }
            }
            else
            {
                WpfEmbeddedImageVisitor imageVisitor = context.ImageVisitor;
                if (imageVisitor != null)
                {
                    BitmapSource visitorSource = imageVisitor.Visit(element, context);
                    if (visitorSource != null)
                    {
                        return visitorSource;
                    }
                }

                string sURI    = element.Href.AnimVal;
                int nColon     = sURI.IndexOf(":");
                int nSemiColon = sURI.IndexOf(";");
                int nComma     = sURI.IndexOf(",");

                string sMimeType = sURI.Substring(nColon + 1, nSemiColon - nColon - 1);

                string sContent  = sURI.Substring(nComma + 1);
                byte[] bResult   = Convert.FromBase64CharArray(sContent.ToCharArray(),
                    0, sContent.Length);

                BitmapImage imageSource = new BitmapImage();
                imageSource.BeginInit();
                imageSource.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
                imageSource.StreamSource  = new MemoryStream(bResult);
                imageSource.EndInit();

                return imageSource;
            }
        }
Please let me know if this solves the problem, so that I could update the sources accordingly.
Best regards,
Paul.