1

I have a problem with converting *.fig files (from Matlab) into bitmaps (or any type which I could use in pictureBox) using C#.

I need to read this file somehow and then show it in pictureBox.

I tried several ways, for example:

Image img = Image.FromFile(fileName);
Bitmap bmp1 = new Bitmap(img.Width, img.Height);

But I got an "Out of memory" exception thrown in the first step.

Do you have any ideas how to do it??

Kev
  • 118,037
  • 53
  • 300
  • 385
Corridaa
  • 11
  • 1
  • 5
    It would seem that such a task is non-trivial: http://undocumentedmatlab.com/blog/fig-files-format/ The file itself is not a bitmap or any other image format, it contains the actual MATLAB commands inside of it. – Steven Hunt Oct 19 '12 at 19:45
  • 1
    You may want to see if there is some way of integrating with MATLAB itself to have it render you an image of the file. – Steven Hunt Oct 19 '12 at 19:47
  • I got this fig files from somebody else, so I have no control to change some settings in matlab. – Corridaa Oct 19 '12 at 20:00

2 Answers2

1

I have a proposal:

Instead of a picturebox, put a panel on your windows form. Then, use System.Diagnostics.Process to start the matlab viewer (required dependency for this, sorry). Now, you can use the instructions here to get the windows handle for the main window of the viewer:

How to get main window handle from process id?

Next, change its parent to the panel in your windows form.

http://support.microsoft.com/kb/89563

I've used this trick before and it takes some tweaking, but you can capture another process's main window and make it a child control on your form. If you can find a less messy way of doing this, that would be great.

Community
  • 1
  • 1
Steven Hunt
  • 2,321
  • 19
  • 18
  • Your proposal looks very reasonably, but unfortunately I didn't mention about one other functionality that I have in my application... I mean, I have to insert this picture from pictureBox to sql database as a bite[]... I've just noticed strange situation, some of the fig files from internet works using my first idea... – Corridaa Oct 19 '12 at 20:23
0

The out of memory exception also occurs when the image file is not in the correct format. http://msdn.microsoft.com/en-us/library/stf701f5.aspx

It's likely you'll need to use some tool to change the .fig file into an image format. I'm not sure how Matlab saves .fig files, so you'll need to do some research as to how the file is actually stored, or find some third party software to update your file to a viable format.

Corey
  • 398
  • 1
  • 4
  • 18
  • Hmm..I would like to avoid some other tools to change the format.. but thanks for your suggestion – Corridaa Oct 19 '12 at 19:56
  • The issue you're going to keep running into is you don't have a picture. You have a figure from Matlab, which may store data in some different way. According to a bit of research, it's a vector file format. You need it as a picture, somehow you're going to have to render it into an image, whether through Matlab, or through some process to turn it into a picture format. Check out: http://www.mathworks.com/matlabcentral/fileexchange/16906-convert-fig-files-to-images http://homepage.usask.ca/~ijm451/fig/ – Corey Oct 22 '12 at 18:20