Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Scatter Graph to Bitmap or JPG Image

Hi all,

I'm trying to convert a ScatterGraph control into a bitmap or jpg file so that the image can be saved.  After some searching, I can't seem to track down the nature of the problem I'm having.  Any help would be appreciated.

The current code that I have is this:

        Dim image As Bitmap = New Bitmap(ScatterGraph1.Width, ScatterGraph1.Height)
        Dim target_bounds As Rectangle

        target_bounds.Width = ScatterGraph1.Width
        target_bounds.Height = ScatterGraph1.Height
        target_bounds.X = 0
        target_bounds.Y = 0

        ScatterGraph1.DrawToBitmap(image, target_bounds)

        image.Save("test.jpg", Drawing.Imaging.ImageFormat.Jpeg)

This code works to some extent.  If I call this code before any data is graphed, then the file is saved with a blank graph, which makes sense.  But once I add data to the ScatterGraph, the code doesn't seem to do anything.  No file is saved and no error or exception is thrown.  I've tried doing things in different orders and I can't seem to track down why the image will save the graph with no data, but it won't save the graph with data.

I'm sure this is probalby something simple, but please let me know if you have any suggestions.

Thanks,
Dave
0 Kudos
Message 1 of 5
(4,377 Views)
Hey Dave,

Which version of Measurement Studio and Visual Studio are you using?  I believe as of 7.1 there is a ToImage function that converts a graph to an image object. We also added a ToFile function which takes a control and saves the image directly to file.  Do you have access to these functions if you check the Measurement Studio help for the Scattergraph? Hope this helps!
Pat P.
Software Engineer
National Instruments
0 Kudos
Message 2 of 5
(4,367 Views)
I'm currently using Measurment Studio 8.1 with Visual Studio 2005.

I tried the functions you suggested and they're behaving exactly the same as the method I had.  The file will save if there is no data loaded onto the plot, but it won't save once data has been loaded.

I've tried it two different ways:

        image = ScatterGraph1.ToImage()

        image.Save("test.jpg", Drawing.Imaging.ImageFormat.Jpeg
---------------------------------------------------
        ScatterGraph1.ToFile("test.jpg", ImageType.Jpeg)

Both of these should have the same outcome, but both different ways seem to not work once any data is loaded onto the scatter graph.  No file is written once data is loaded and there are no errors while debugging.

Let me know if you have any other suggestions.

Thanks,
Dave
0 Kudos
Message 3 of 5
(4,355 Views)
Hi David,

I didn't have any problems saving to a bitmap file using the steps you mentioned.

Check out the attached modified example program in which you click a button to plot the data and then click another button to save the data.

Let us know if the example helps!

Best Regards,

Jonathan N.
National Instruments
0 Kudos
Message 4 of 5
(4,343 Views)
Jonathan,

Thanks for the test app.  It helped me track down the problem so that everything is working perefectly now.  For some reason if you specify a relative file path such as "test.jpg" it will save the ScatterPlot with no data but will not function with data on the plot.  If you specify an absolute path such as "C:\folder\blah\test.jpg" then everything works as planned.  Just thought I would post the solution I came up with for others that find this thread.

                myBitmap = ScatterGraph1.ToImage(imagesize)
                myBitmap.Save(SaveFileDialog1.FileName, Imaging.ImageFormat.Gif)      'The Filename argument must be an absolute path
0 Kudos
Message 5 of 5
(4,325 Views)