Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

How to copy an image to a file?

Solved!
Go to solution

I am coding under .NET Framework 3.5 2008, in C#.

 

I have a collection of plots displayed using the ScatterGraph class. I have to include these plots into a a word document; it is a large collection (several hundreds).

 

Therefore I am considering saving the plots as images and then include the images into the report. 

 

First basic question: how to save a plot to a file

Second: is there a way to copy all the plots together into a word document?

 

Thanks a lot in advance for your precious help,

 

GL

0 Kudos
Message 1 of 4
(4,822 Views)

I have found this great suggestion

 

http://forums.ni.com/t5/Measurement-Studio-for-NET/saving-graph-as-image/m-p/1115427 meanwhile.

 

Where can I find the documentation about the ScatterGraph class?

 

Basically my main question now is:  how to incorporate a large amount of plots ( ScatterGraph plots with labels axes etc) into a word document automatically? (in .NET 3.5 (C#) ).

 

 

 

Thanks again.

0 Kudos
Message 2 of 4
(4,820 Views)
Solution
Accepted by actarus

Hi actarus,

 

Here's what I did to create a word document with text and the scattergraph images.

 

1. Right-click your project in the Solution Explorer and choose Add Reference...

2. On the COM tab, select Microsoft Word 12.0 Object Library

3. Click OK

 

The Form I created had a scatterGraph and a button. Here's the code that was added to the top C# source file:

 

           using Word = Microsoft.Office.Interop.Word;
           using System.Reflection;

 

This code was added to the Button's callback:

 

  private void button1_Click(object sender, EventArgs e)
        {
            //save the picture
            scatterGraph1.ToFile("GraphImage.png", ImageType.Png);

            /*
             * http://support.microsoft.com/kb/316384
             */

            //create the document
            object oMissing = System.Reflection.Missing.Value;
            object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */

            //Start Word and create a new document.
            Word._Application oWord;
            Word._Document oDoc;
            oWord = new Word.Application();
            oWord.Visible = true;
            oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
                ref oMissing, ref oMissing);

            //Insert a paragraph at the beginning of the document.
            Word.Paragraph oPara1;
            oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
            oPara1.Range.Text = "Heading 1";
            oPara1.Range.Font.Bold = 1;
            oPara1.Format.SpaceAfter = 24;    //24 pt spacing after paragraph.
            oPara1.Range.InsertParagraphAfter();

            //Insert a Picture
            string fileName = "C:\\_support\\_reports\\project\\project\\bin\\Debug\\GraphImage.png";
            Object oMissed = oPara1.Range;
            Object oLinkToFile = false;  //default
            Object oSaveWithDocument = true;//default
            oDoc.InlineShapes.AddPicture(fileName, ref  oLinkToFile, ref  oSaveWithDocument, ref  oMissed);
            oDoc.InlineShapes.AddPicture(fileName, ref  oLinkToFile, ref  oSaveWithDocument, ref  oMissed); //image added again for fun
        }

 

 

See the above linked Microsoft KB for more information on formatting.

 

Tanya Visser
National Instruments
LabVIEW Group Manager
Message 3 of 4
(4,788 Views)

Hi Tanya,

 

Thank you so much for your prompt reply and efficient solution.

 

I was heading toward the same methodology, but since my experience in VSTO is nil,

I was getting some troubles in missing references and few other things. Everything worked nicely and the logic behind is crystal clear.

 

I have tried it with a test .png, but I will use my precious ScatterGraphs shortly. I had noticed the ToFile method; definitely very useful.

 

Thank you so much, once again.

 

Have a nice day,

 

Actarus

0 Kudos
Message 4 of 4
(4,784 Views)