MathGroup Archive 2005

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

Search the Archive

Re: How to change the ResultFormatType of the MathKernel in a ASP.NET Web Application?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg55767] Re: How to change the ResultFormatType of the MathKernel in a ASP.NET Web Application?
  • From: "Javier Chicote" <goraperas at hotmail.com>
  • Date: Tue, 5 Apr 2005 06:10:49 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Hi Hans,

This is a response for the following topics, where using NETLink and the
examples included in Mathematica, I wanted to change the ResultFormatType of
my ASP.NET Web Application, so that I could show the result of the Compute()
method in a smart format. (For example, TraditionalForm instead of
OutputForm).

http://forums.wolfram.com/mathgroup/archive/2005/Mar/msg00707.html
http://forums.wolfram.com/mathgroup/archive/2005/Mar/msg00256.html

I have been working with the same idea but I haven't solved the solution to
my problem yet. I have 3 WebForms in my web application:

WebForm1 --> Main
Graphics --> Used to see IMAGE returned by mathKernel.Link.EvaluateToImage()
Results ---> Used to see IMAGE returned by mathKernel.Result() when
ResulFormatType is TraditionalForm

The Graphics.aspx form works properly, and I can see the image returned by
mathKernel.Link.EvaluateToImage(), but the Results.aspx doesn't work, and I
cannot see the image returned by mathKernel.Result() when ResulFormatType is
TraditionalForm:

 private void Page_Load(object sender, System.EventArgs e)
 {
  string input = WebForm1.mathKernel.Input.ToString();

  WebForm1.mathKernel.CaptureGraphics = true;
  WebForm1.mathKernel.ResultFormat =
Wolfram.NETLink.MathKernel.ResultFormatType.TraditionalForm;
  WebForm1.mathKernel.Compute(input);

  image = (System.Drawing.Image)WebForm1.mathKernel.Result;
  image.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg);
 }

I enclose here the full code of the following files:

1.- WebForm1.aspx
2.- WebForm1.aspx.cs
3.- Graphics.aspx
4.- Graphics.aspx.cs
5.- Results.aspx
6.- Results.aspx.cs

Thank you very much,

Javier Chicote

P.D. I also tried to do this in the WIN application, with WinForms instead
of WebForms, but there is an error when executing mathKernelCompute() after
changing the ResultFormatType to TraditionalForm. (In my Web Application
there is no errors but the image is not shown)

P.D.2 Other types of ResultFormat, such as InputForm and Expr works
perfectly (I suppose that because a string is returned, like with the
default OutputForm).


//=========================================================================
// 1.- WebForm1.aspx
//=========================================================================

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="MathKernelApp2.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" bgColor="#cccccc">
  <form id="Form1" method="post" runat="server">
   <asp:textbox id="resultBox" style="Z-INDEX: 103; LEFT: 8px; POSITION:
absolute; TOP: 72px" runat="server"
    Font-Size="9pt" Font-Names="Lucida Console" TextMode="MultiLine"
Height="56px" Width="464px"></asp:textbox>
   <asp:image id="resultsImage" style="Z-INDEX: 110; LEFT: 8px; POSITION:
absolute; TOP: 136px"
    runat="server" Height="56px" Width="465px"
ImageUrl="Results.aspx"></asp:image>
   <asp:label id="graphicsLabel" style="Z-INDEX: 109; LEFT: 8px; POSITION:
absolute; TOP: 200px"
    runat="server" Font-Size="10pt" Font-Names="Microsoft Sans Serif"
Font-Bold="True">Graphics:</asp:label>
   <asp:image id="graphicsImage" style="Z-INDEX: 107; LEFT: 8px; POSITION:
absolute; TOP: 216px"
    runat="server" Height="200px" Width="200px"
ImageUrl="Graphics.aspx"></asp:image>
   <asp:button id="closeButton" style="Z-INDEX: 106; LEFT: 400px; POSITION:
absolute; TOP: 384px"
    runat="server" Font-Size="10pt" Font-Names="Microsoft Sans Serif"
Height="32px" Width="74px"
    ToolTip="Obtener el resultado" Text="Close"
Enabled="False"></asp:button>
   <asp:button id="connectButton" style="Z-INDEX: 105; LEFT: 320px;
POSITION: absolute; TOP: 384px"
    runat="server" Font-Size="10pt" Font-Names="Microsoft Sans Serif"
Height="32px" Width="74px"
    ToolTip="Obtener el resultado" Text="Connect"></asp:button>
   <asp:label id="resultLabel" style="Z-INDEX: 102; LEFT: 8px; POSITION:
absolute; TOP: 56px"
    runat="server" Font-Size="10pt" Font-Names="Microsoft Sans Serif"
Font-Bold="True">Resultado:</asp:label>
   <asp:label id="inputLabel" style="Z-INDEX: 101; LEFT: 8px; POSITION:
absolute; TOP: 8px" runat="server"
    Font-Size="10pt" Font-Names="Microsoft Sans Serif"
Font-Bold="True">Entrada:</asp:label>
   <asp:textbox id="inputBox" style="Z-INDEX: 100; LEFT: 8px; POSITION:
absolute; TOP: 24px" runat="server"
    Font-Size="10pt" Font-Names="Microsoft Sans Serif" Height="24px"
Width="376px"> Plot3D[Sin[x y], {x, -2, 2}, {y, 0-2,2}]</asp:textbox>
   <asp:button id="computeButton" style="Z-INDEX: 104; LEFT: 400px;
POSITION: absolute; TOP: 20px"
    runat="server" Font-Size="10pt" Font-Names="Microsoft Sans Serif"
Height="32px" Width="74px"
    ToolTip="Obtener el resultado" Text="Calcular"
Enabled="False"></asp:button>
  </form>
 </body>
</HTML>



//=========================================================================
// 2.- WebForm1.aspx.cs
//=========================================================================

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 MathKernelApp2
{
 /// <summary>
 /// Descripción breve de WebForm1.
 /// </summary>
 public class WebForm1 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Button computeButton;
  protected System.Web.UI.WebControls.TextBox inputBox;
  protected System.Web.UI.WebControls.Label inputLabel;
  protected System.Web.UI.WebControls.Label resultLabel;
  protected System.Web.UI.WebControls.Button connectButton;
  protected System.Web.UI.WebControls.Button closeButton;
  protected System.Web.UI.WebControls.TextBox resultBox;
  protected System.Web.UI.WebControls.Image graphicsImage;
  protected System.Web.UI.WebControls.Label graphicsLabel;
  protected System.Web.UI.WebControls.Image resultsImage;

  // Interface for interacting with Mathematica
  public 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
  }

  #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.closeButton.Click += new
System.EventHandler(this.closeButton_Click);
   this.connectButton.Click += new
System.EventHandler(this.connectButton_Click);
   this.computeButton.Click += new
System.EventHandler(this.computeButton_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void connectButton_Click(object sender, System.EventArgs e)
  {
   // Create and connect the link to MathKernel for interacting with
Mathematica
   string PATH = "C:/Archivos de programa/Wolfram
Research/Mathematica/5.0/mathkernel.exe";
   mathKernel = new Wolfram.NETLink.MathKernel();
   mathKernel.LinkArguments = "-linkmode launch -linkname \"" + PATH + "\"";
   mathKernel.Connect();
   mathKernel.CaptureGraphics = true;

   connectButton.Enabled = false;
   computeButton.Enabled = true;
   closeButton.Enabled = true;
  }

  private void computeButton_Click(object sender, System.EventArgs e)
  {
   if (mathKernel.IsComputing)
   {
    // Send a request to abort the computation currently in progress
    mathKernel.Abort();
   }
   else
   {
    // Clear out results from previous computation
    resultBox.Text = "";

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

    // Perform the computation
    mathKernel.ResultFormat =
Wolfram.NETLink.MathKernel.ResultFormatType.OutputForm;
    mathKernel.Compute(inputBox.Text);

    // Show the results of the computation
    resultBox.Text = mathKernel.Result.ToString();
   }
  }

  private void closeButton_Click(object sender, System.EventArgs e)
  {
   // Release the unmanaged resources and optionally release the managed
resoruces
   mathKernel.Dispose();

   connectButton.Enabled = true;
   computeButton.Enabled = false;
   closeButton.Enabled = false;

  }
 }
}



//=========================================================================
// 3.- Graphics.aspx
//=========================================================================

<%@ Page language="c#" Codebehind="Graphics.aspx.cs" AutoEventWireup="false"
Inherits="MathKernelApp2.Graphics" contentType="image/jpeg"%>



//=========================================================================
// 4.- Graphics.aspx.cs
//=========================================================================

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 MathKernelApp2
{
 /// <summary>
 /// Descripción breve de Graphics.
 /// </summary>
 public class Graphics : System.Web.UI.Page
 {
  protected System.Drawing.Image image;

  private void Page_Load(object sender, System.EventArgs e)
  {
   string input = WebForm1.mathKernel.Input.ToString();
   image = WebForm1.mathKernel.Link.EvaluateToImage(input,200,200);
   image.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg);
  }

  #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
 }
}



//=========================================================================
// 5.- Results.aspx
//=========================================================================

<%@ Page language="c#" Codebehind="Results.aspx.cs" AutoEventWireup="false"
Inherits="MathKernelApp2.Results" contentType="image/jpeg"%>



//=========================================================================
// 6.- Results.aspx.cs
//=========================================================================

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 MathKernelApp2
{
 /// <summary>
 /// Descripción breve de Results.
 /// </summary>
 public class Results : System.Web.UI.Page
 {
  protected System.Drawing.Image image;

  private void Page_Load(object sender, System.EventArgs e)
  {
   string input = WebForm1.mathKernel.Input.ToString();

   WebForm1.mathKernel.CaptureGraphics = true;
   WebForm1.mathKernel.ResultFormat =
Wolfram.NETLink.MathKernel.ResultFormatType.TraditionalForm;
   WebForm1.mathKernel.Compute(input);

   image = (System.Drawing.Image)WebForm1.mathKernel.Result;
   image.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg);
  }

  #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
 }
}




----- Original Message ----- 
From: "Hans Michel" <hmichel at cox.net>
To: mathgroup at smc.vnet.net
Subject: [mg55767] Re: How to change the ResultFormatType of the MathKernel in a
ASP.NET Web Application?


> Javier:
>
> Since it has been either Todd or I responding to your questions, I left
> this
> one unattended because I just thought it was a repost.
> I a way it is. But you are still having problems.
>
> You are using Visual Studio 2003 and you still write out full Object
> paths
> for lack of a better word.
>
> The sample I sent you in VB makes no reference to Global
>
> I declare a variable MathKernel1 as New Wolfram...MathKernel (Created
> the
> Object called MathKernel1)
>
> From then on I call MathKernel1  methods and set or get Properties.
> See post on 3/9/2005
>
> There is no Global in the VB code.
>
> I think you are missing something fundamental. Copy all your code the
> machine generated stuff also. I'll review and get back to you.
>
> Hans
>
>
> "Javier Chicote" <goraperas at hotmail.com> wrote in message
> news:d1ecl3$esj$1 at smc.vnet.net...
> > Hi,
> >
> > I would like to change the ResultFormatType of my ASP.NET Web
> Application,
> > so that I could show the result of the Compute() method in a smart
> format.
> > (For example, TraditionalForm instead of OutputForm). I looked in the
> API
> > help and saw that the type of Result is a System.Drawing.Image.
> >
> > In the past I learnt how to show the image of the mathKernel.Graphics
> > result
> > in my application, using a WebForm with contentType="image/jpeg". The
> code
> > of the Page_Load was like this:
> >
> >    private void Page_Load(object sender, System.EventArgs e)
> >    {
> >        image = mathKernel.Link.EvaluateToImage(input,200,200);
> >        image.Save(Response.OutputStream,
> > System.Drawing.Imaging.ImageFormat.Jpeg);
> >    }
> >
> > That worked perfectly, and now I would like to show the image of the
> > mathKernel.Result:
> >
> >    private void Page_Load(object sender, System.EventArgs e)
> >    {
> >        mathKernel.ResultFormat =
> > Wolfram.NETLink.MathKernel.ResultFormatType.TraditionalForm;
> >        mathKernel.Compute(input);
> >        image = (System.Drawing.Image)Global.mathKernel.Result;
> >        image.Save(Response.OutputStream,
> > System.Drawing.Imaging.ImageFormat.Jpeg);
> >    }
> >
> > And this does NOT work, and I cannot see the image. How can I solve my
> > problem and show the result of the Compute() method using the
> > TraditionalForm of the ResultFormatType.
> >
> > Thanks,
> >
> > Javier Chicote
> >
> >
> >
>
>
>



  • Prev by Date: Re: file corrupted
  • Next by Date: Re: Use of mathlink in C++
  • Previous by thread: Re: Re: immediate abort at error messages
  • Next by thread: Re: Y axis labelling on OS X. new (to me) development.