none
MSAGL MVC ASP.NET download image and svg example RRS feed

  • General discussion

  • SVG download based on http://social.msdn.microsoft.com/Forums/en-US/automaticgraphlayout/thread/e312e5bc-c0e7-4d05-b941-c9bd3a26febc

    Create a standard Visual Studio 2010 web site application. Convert MSAGL Drawing and GraphViewerGDI projects to VS 2010. Change MSAGL projects properties to target framework .NET 4.0. Add those projects to MvcApplication1 solution, check references.

    HomeController.cs

    using System;
    using System.IO;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    using Microsoft.Msagl;
    using Microsoft.Msagl.Drawing;
    using Microsoft.Msagl.GraphViewerGdi;
    //Microsoft.Msagl DLL version 2.1.0.0
    
    namespace MvcApplication1.Controllers
    {
        [HandleError]
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                ViewData["Message"] = "Welcome to ASP.NET MVC!";
                string filepath = @"C:\SVN\trunk\Cimetrics\Analysis\MicrosoftAutomaticGraphLayoutSamples\OlgaWeb\MvcApplication1\Images\testMSAGL.png";
                string filepathXML = @"C:\SVN\trunk\Cimetrics\Analysis\MicrosoftAutomaticGraphLayoutSamples\OlgaWeb\MvcApplication1\Images\testMSAGL.svg";
                
                try
                {
                    //http://research.microsoft.com/en-us/projects/msagl/codesamples.aspx
                    Microsoft.Msagl.Drawing.Graph graph = this.createGraph("testMSAGLSave");
                   
                    Microsoft.Msagl.GraphViewerGdi.GraphRenderer renderer = new Microsoft.Msagl.GraphViewerGdi.GraphRenderer(graph);
                    renderer.CalculateLayout();
    
                    //Save graph to SVG file on the server
                    Microsoft.Msagl.Drawing.SvgGraphWriter.Write(graph, filepathXML);
    
                    //Save image to a bitmap file on the server
                    int width = 150;
                    Bitmap bitmap = new Bitmap(width, (int)(graph.Height * (width / graph.Width)), System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
                    renderer.Render(bitmap);
                    bitmap.Save(filepath);                  
    
                    ViewData["Message"] = filepath;
                }
                catch (Exception ex)
                {
                    ViewData["Message"] = ex.Message;
                }
    
                return View();
            }
            
            [AcceptVerbs(HttpVerbs.Post)]
            [ValidateInput(false)]
            public FileStreamResult OpenImage()
            {
                FileStreamResult result = null;
                string dataType = "image/png";
                //http://research.microsoft.com/en-us/projects/msagl/codesamples.aspx
                Microsoft.Msagl.Drawing.Graph graph = this.createGraph("testMSAGLImage");
    
                Microsoft.Msagl.GraphViewerGdi.GraphRenderer renderer = new Microsoft.Msagl.GraphViewerGdi.GraphRenderer(graph);
                renderer.CalculateLayout();
    
                int width = 500;
                Bitmap bitmap = new Bitmap(width, (int)(graph.Height * (width / graph.Width)), System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
                renderer.Render(bitmap);                        
            
                MemoryStream ms = new MemoryStream();
                bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
              
                ms.Seek(0, SeekOrigin.Begin);
                result = new FileStreamResult(ms, dataType);
                Response.ClearContent();
                Response.AddHeader("content-disposition", "attachment; filename=testMSAGLImage.png");
                Response.ContentType = dataType;
    
                return result;
            }
    
            [AcceptVerbs(HttpVerbs.Post)]
            [ValidateInput(false)]
            public FileStreamResult OpenXML()
            {
                FileStreamResult result = null;
                string dataType = "text/xml";
    
                //http://research.microsoft.com/en-us/projects/msagl/codesamples.aspx
                Microsoft.Msagl.Drawing.Graph graph = this.createGraph("testMSAGLXML"); 
                Microsoft.Msagl.GraphViewerGdi.GraphRenderer renderer = new Microsoft.Msagl.GraphViewerGdi.GraphRenderer(graph);
                renderer.CalculateLayout();
    
                string xml = Microsoft.Msagl.Drawing.SvgGraphWriter.Write(graph);
    
                MemoryStream ms = new MemoryStream();
                StreamWriter sw = new StreamWriter(ms, System.Text.Encoding.UTF8);
                sw.Write(xml);
                sw.Flush();
                ms.Seek(0, SeekOrigin.Begin);
                result = new FileStreamResult(ms, dataType);
                Response.ClearContent();
                Response.AddHeader("content-disposition", "attachment; filename=testMSAGLXML.svg");
                Response.ContentType = dataType;
               
                return result;
            }
                 
    
            public ActionResult About()
            {
                return View();
            }
    
            Microsoft.Msagl.Drawing.Graph createGraph(string id)
            {
                Microsoft.Msagl.Drawing.Graph graph = new Microsoft.Msagl.Drawing.Graph(id);
    
                graph.Directed = true;
                graph.AddEdge("Linden", "Birch");
                graph.AddEdge("Linden", "Cypress");
                graph.AddEdge("Linden", "Oak");
                graph.AddEdge("Birch", "Cypress");
                graph.AddEdge("Birch", "Maple");
                graph.AddEdge("Maple", "Oak");
    
                graph.FindNode("Birch").Attr.FillColor = Microsoft.Msagl.Drawing.Color.AliceBlue;
                graph.FindNode("Linden").Attr.FillColor = Microsoft.Msagl.Drawing.Color.MistyRose;
    
                Microsoft.Msagl.Drawing.Node c = graph.FindNode("Cypress");
                c.Attr.FillColor = Microsoft.Msagl.Drawing.Color.PaleGreen;
                c.Attr.Shape = Microsoft.Msagl.Drawing.Shape.Diamond;
    
                Microsoft.Msagl.Drawing.Node m = graph.FindNode("Maple");
                m.Attr.FillColor = Microsoft.Msagl.Drawing.Color.Beige;
                m.Attr.Shape = Microsoft.Msagl.Drawing.Shape.Ellipse;
    
                //LAYOUT
                graph.Attr.LayerDirection = LayerDirection.TB;
                
                return graph;
            }
    
        }
    }
    

    Index.aspx

    <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
        Test MSAGL July 2012
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">   
        
        <h2>http://research.microsoft.com/en-us/projects/msagl/default.aspx</h2>
        
        <%using (Html.BeginForm("OpenImage", "Home", new { ws = "" }, FormMethod.Post, new { @id = "form_submit_image", @title = "" }))
        {%>         
            <input type="submit" value="Download Image..." />                         
        <%} %><br />
       
        <%using (Html.BeginForm("OpenXML", "Home", new { ws = "" }, FormMethod.Post, new { @id = "form_submit_xml", @title = "" }))
        {%>         
            <input type="submit" value="Download SVG..." />                         
        <%} %><br />
       
        <h3><%: ViewData["Message"] %></h3>
        <img src="./Images/testMSAGL.png" />
     
    </asp:Content>


    OlgaD

    Tuesday, July 10, 2012 5:31 PM