[Date Index] [Thread Index] [Author Index]
Re: how to save a mathematica-made imagebox.image as jpg, gif, bmp in c#/.netlink?
At 03:29 AM 5/7/2004, cdj wrote: >Hi all, > >This has been buggin me for awhile... I'm sure I'm doing something >silly, but I can't figure out what.... > >It suffices to consider the following problem: > >(1) Use the mathematica-supplied c# example (let's you enter a Mathematica >statement, compute, and captures output.) > >How to save a graphicsBox.Image to a file, say as a jpg? > >There's standard code that is available on msdn, to wit: > >(2) >http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtsksavingfilesusingsavefiledialogcomponent.asp > >When I try to join these 2 bits of code together, I get a totally >uninformative GDI+ error of some sort. > >I'm under the impression that the problem is that the Mathematica graphic >isn't being assigned to the picturebox in the "normal" way, or some >such thing. The reason I have this impression is that while the >following code-bits are quite similar, I can save with one, but not >with the other: > >(a) graphicsBox.Image = mathKernel.Graphics; > >(b) graphicsBox.Image = Image.FromFile("pathtomyfile.jpg"); > >Can anybody explain to me why the msdn routine works fine with (b), >but not with (a)? > >I've been brief here with the description of my problem - I'm happy to >expand a bit on anything above, if it would be helpful.... > >thx much for any insights, Thanks for tracking down this bug in the MathKernel class. The problems is that a 'using' statement is closing the MemoryStream that backs up the Image data, which is a no-no (line 693 of MathKernel.cs if you are interested). I have fixed the bug for the next release of .NET/Link, but you can work around it very simply. Just create a new Bitmap out of the image you get from the mathKernel.Graphics array (which is a Bitmap also). This apparently copies the data out of the original bitmap into a new one backed up by more appropriate data storage: graphicsBox.Image = mathKernel.Graphics; // Add a line like this: Bitmap bmp = new Bitmap(graphicsBox.Image); // Now use bmp instead of graphicsBox.Image in the MSDN example code for saving a file. Thanks again for finding this. This problem only affects the very high-level MathKernel class, not code that calls the IKernelLink.evaluateToImage() method directly. Todd Gayley Wolfram Research