ASP.net: Convert SVG to GIF (or JPG)



  • A web application shall output an image that is generated (by some existing program) as an SVG image; since SVG does not work on all browsers, I'd rather like to convert it to GIF or JPG on the fly.

    Is there an easy way to do it? Since the SVG image is very simple (just a bunch of straight lines), I could also parse the image and process it by hand... but I'd rather not want to reinvent the wheel. 



  • If its a static svg, you could always screen shot it.



  • GIF is a patented proprietary format and JPG is lossy.  PNG is a nice open format that is equivalent to GIF.



  • @Hellz99 said:

    If its a static svg, you could always screen shot it.

    Obviously. But as I wrote, the SVG is generated. 



  • @Hellz99 said:

    If its a static svg, you could always screen shot it.
    No, I doubt that the server is set up on a wooden table.

     

    Maybe a silly question, but can the program that generates the SVG also output GIF/JPG/PNG? That would eliminate the whole issue.



  • @RayS said:

    @Hellz99 said:

    If its a static svg, you could always screen shot it.
    No, I doubt that the server is set up on a wooden table.

    Maybe a silly question, but can the program that generates the SVG also output GIF/JPG/PNG? That would eliminate the whole issue.

    Unfortunately not. The SVG is (unfortunately) something I have to live with. Trying to change that part of the system means opening the pandora's box.



  • I hate to say, but I think you'll have to roll your own.  You're best bet would be the Avalon extensions in .Net 3.0, but you probably won't have that on the server.  The only other option is to use Java/Batik if you don't mind having both on the same box.




  • Seems to work with imagemagick, haven't tried it though...

    http://www.imagemagick.org/pipermail/magick-users/2002-March/001787.html 



  • @Obfuscator said:

    Seems to work with imagemagick, haven't tried it though...

    http://www.imagemagick.org/pipermail/magick-users/2002-March/001787.html 

    Good find.  I always forget about ImageMagick.  If the SVG as simple as ammoQ states, it might do the trick.  I had issues with conversions when the SVG is calling out external resources.  If the SVG is just declaring simple lines and paths, I had decent results.



  • Install java/batik and run "java -jar batik-rasterizer.jar images.svg" from your script? :-)



  • I did an app a couple years ago that involved constructing my own bitmaps and outputting them as GIFs/JPGs.   It wasn't all that difficult to do in ASP.NET; you could probably find code that will take an svg and load it as a .NET bitmap, then save the bitmap out as a GIF.  

    Then you could use an ASP.NET handler to even respond to requests like http://yoursite/svgs/foo.jpg and actually read svgs/foo.svg but output it as a jpg.

    The only trick is finding code to read the SVG, but looking at places like codeproject.com, I see a few things that let you use svgs, you might just be able to scavenge code from there :)

    -cw



  • I think I'm going to do it by hand. Reading the SVGs is nearly trivial in this case, since I know the program that generates them will only use produce one kind of element, i.e. straight lines.

    Thanks for all your suggestions. 


Log in to reply