MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Mathematica Graphics output in an ASP.NET WebApplication

  • To: mathgroup at smc.vnet.net
  • Subject: [mg53459] Mathematica Graphics output in an ASP.NET WebApplication
  • From: "Javier Chicote" <goraperas at hotmail.com>
  • Date: Wed, 12 Jan 2005 03:41:40 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

Hi,

I would like to place the Mathematica Graphics output in an ASP.NET
WebApplication using Wolfram.NETLink reference.

I tried to use the mathKernel.Graphics property to get the image from the
Mathematica graphics output during the last call to mathKernel.Compute. The
problem is that I need to refer a URL for my
System.Web.UI.WebControls.Image, and the mathKernel.Graphics property
returns a System.Drawing.Image.

I thought about saving the image returned to a temporary file, so that I
could get the URL, but I don't know how to do this, or if maybe there is an
easier way to place the image in my ASP.NET WebAplication.

----------------------------------------------------------------------------
------------------------

private void computeButton_Click(object sender, System.EventArgs e)
{
      if (kernelLaunched==false)
      {
            // Inicialize a new instance of the MathKernel class
            mathKernel = new Wolfram.NETLink.MathKernel();

            // Set the command string that will be passed to
CreateKernelLink
            string path = "C:/Archivos de programa/Wolfram
Research/Mathematica/5.0/mathkernel.exe";
            mathKernel.LinkArguments = "-linkmode launch -linkname \"" +
path + "\"";

            // It is not neccesary to initialize a new instance of the
MathKernel class
            kernelLaunched = true;
            finishButton.Enabled = kernelLaunched;
      }

      if (mathKernel.IsComputing)
      {
            // Send a request to abort the computation currently in progress
            mathKernel.Abort();
            commentsBox.Text = "COMPUTING";
      }
      else
      {
            // Clear out results from previous computation
            resultBox.Text = "";
            messagesBox.Text = "";
            printBox.Text = "";
            graphicsImage.ImageUrl = null;

            // Create and connect the link, if it has not already been
connected
            mathKernel.Connect();

            // Perform the computation
            mathKernel.Compute(inputBox.Text);

            // Populate the boxes with results
            resultBox.Text = (string)mathKernel.Result;
            foreach (string msg in mathKernel.Messages)
                  messagesBox.Text += (msg + "\r\n");
            foreach (string p in mathKernel.PrintOutput)
                  printBox.Text += p;

            // Populate the image with graphics
                  // ??? = mathKernel.Graphics[0];
//System.Drawing.Image[]
                  // graphicsImage.ImageUrl = ???;
//System.Web.UI.WebControls.Image
      }
}



Thanks,

Javier Chicote

P.D. I already had a look at the topic
http://forums.wolfram.com/mathgroup/archive/2004/Mar/msg00143.html, that may
be an solution to my problem, but I still cannot view the graphic in my web
application. I enclose the code I have written in C# for WebForm1 and
WebForm2:

============================================================================
========================
WebForm1
============================================================================
========================

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="mathGraph.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WebForm1</title>
  <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript">
  <meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5";>
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <img src="WebForm2.aspx" height="200" width="200" alt="" runat="server"
style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 16px"
    ID="Img1">&nbsp;
   <asp:TextBox id="TextBox1" style="Z-INDEX: 102; LEFT: 16px; POSITION:
absolute; TOP: 232px" runat="server"
    Width="200px"></asp:TextBox>
  </form>
 </body>
</HTML>

----------------------------------------------------------------------------
------------------------

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace mathGraph
{
      /// <summary>
      /// Descripción breve de WebForm1.
      /// </summary>
      public class WebForm1 : System.Web.UI.Page
      {
            protected System.Web.UI.HtmlControls.HtmlImage Img1;
            protected System.Web.UI.WebControls.TextBox TextBox1;

            private void Page_Load(object sender, System.EventArgs e)
            {
                  // Introducir aquí el código de usuario para inicializar
la página
            }

            #region Código generado por el Diseñador de Web Forms
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: llamada requerida por el Diseñador de Web
Forms ASP.NET.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }

            /// <summary>
            /// Método necesario para admitir el Diseñador. No se puede
modificar
            /// el contenido del método con el editor de código.
            /// </summary>
            private void InitializeComponent()
            {
                  this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion
      }
}



============================================================================
========================
WebForm2
============================================================================
========================

<%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false"
Inherits="mathGraph.WebForm2" EnableSessionState="False"
enableViewState="False" contentType="image/jpeg"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >


----------------------------------------------------------------------------
------------------------

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using Wolfram.NETLink;
using System.Drawing.Imaging;

namespace mathGraph
{
      /// <summary>
      /// Descripción breve de WebForm2.
      /// </summary>
      public class WebForm2 : System.Web.UI.Page
      {
            protected static Wolfram.NETLink.MathKernel mathKernel;


            private void Page_Load(object sender, System.EventArgs e)
            {
                  // Introducir aquí el código de usuario para inicializar
la página

                  mathKernel = new Wolfram.NETLink.MathKernel();
                  string path = "C:/Archivos de programa/Wolfram
Research/Mathematica/5.0/mathkernel.exe";
                  mathKernel.LinkArguments = "-linkmode launch -linkname \""
+ path + "\"";
                  mathKernel.Connect();
                  mathKernel.GraphicsHeight=200;
                  mathKernel.GraphicsWidth=200;

                  string input = "Plot3D[Sin[x y], {x, -Pi, Pi}, {y, 0
,10}]";
                  mathKernel.Compute(input);
                  Bitmap image = new Bitmap(mathKernel.Graphics[0]);
                  image.Save(Response.OutputStream, ImageFormat.Jpeg);

                  mathKernel.Dispose();
            }

            #region Código generado por el Diseñador de Web Forms
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: llamada requerida por el Diseñador de Web
Forms ASP.NET.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }

            /// <summary>
            /// Método necesario para admitir el Diseñador. No se puede
modificar
            /// el contenido del método con el editor de código.
            /// </summary>
            private void InitializeComponent()
            {
                  this.Load += new System.EventHandler(this.Page_Load);
            }
            #endregion
      }
}






  • Prev by Date: Re: Re:Re: FullSimplify with Assumptions
  • Next by Date: Re: Newbie Programming question
  • Previous by thread: Re: Newbie Limit problem
  • Next by thread: Re: Mathematica Graphics output in an ASP.NET WebApplication