none
Adding Image from Webcam to OneNote Page C# RRS feed

  • Question

  • Hey all,

    I am  a young sysadmin and need a little bit help with my code.

    I am accesing my webcam and want to save the current frame to my currently opened onenote page.

    I already have access to the picture and am converting it for the xml file.

    private void GetNameSpace() { MemoryStream stream = new MemoryStream(); pictureBoxVideo.BackgroundImage.Save(stream, ImageFormat.Jpeg); string fileString = Convert.ToBase64String(stream.ToArray()); string xml; string thisPage = oneNoteApp.Windows.CurrentWindow.CurrentPageId; string toAdd = @"<one:Image><one:Data>" + fileString +"</one:Data></one:Image>"; oneNoteApp.GetPageContent(thisPage, out xml, PageInfo.piAll, XMLSchema.xs2010); }



    Now i am stuck. I want to add the string toAdd to the XML file.

    I probably have to use UpdatePageContent now.

    I hope you can help me.

    Thanks,

    Ennui

    • Edited by Ennui Sys Wednesday, November 2, 2016 1:36 PM
    Wednesday, November 2, 2016 1:35 PM

Answers

  • Hi Ennui,

    You could try to get the page name from ID.

    Here is a simple code:

    string strNamespace = "http://schemas.microsoft.com/office/onenote/2013/onenote";
                string m_xmlImageContent =
    "<one:Image><one:Size width=\"{1}\" height=\"{2}\" isSetByUser=\"true\" /><one:Data>{0}</one:Data></one:Image>";
                string m_xmlNewOutline =
                   "<?xml version=\"1.0\"?><one:Page xmlns:one=\"{2}\" ID=\"{1}\"><one:Title><one:OE><one:T><![CDATA[{3}]]></one:T></one:OE></one:Title>{0}</one:Page>";         
    
    
                Bitmap bitmap = new Bitmap(@"C:\Users\xx\Desktop\xx.png");
                MemoryStream stream = new MemoryStream();
                bitmap.Save(stream, ImageFormat.Jpeg);
                string fileString = Convert.ToBase64String(stream.ToArray());
                string notebookXml;
                OneNote.Application onenoteApp = new OneNote.Application();
    
                onenoteApp.GetHierarchy(null, HierarchyScope.hsPages, out notebookXml);
                var doc = XDocument.Parse(notebookXml);
                var ns = doc.Root.Name.Namespace;
                
                var existingPageId = onenoteApp.Windows.CurrentWindow.CurrentPageId; //pageNode.Attribute("ID").Value;
                var pageNodeName = doc.Descendants(ns + "Page").Where(n => n.Attribute("ID").Value == existingPageId).FirstOrDefault();
                string pageToBeChange = pageNodeName.Attribute("name").Value;
               
                string imageXmlStr = string.Format(m_xmlImageContent, fileString, bitmap.Width, bitmap.Height);
                string pageChangesXml = string.Format(m_xmlNewOutline, new object[] { imageXmlStr, existingPageId, strNamespace, pageToBeChange });
                onenoteApp.UpdatePageContent(pageChangesXml.ToString(), DateTime.MinValue);
    

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 4, 2016 9:22 AM

All replies

  • Hi Ennui,

    Based on your description, you have got the picture, am I right? If so, I suggest you refer the link below for UpdatePageContent to insert the picture.

    # Problem programmatically inserting image in onenote 2010 using C#

    https://social.msdn.microsoft.com/Forums/office/en-US/4596510a-6509-4e3a-be08-c11131fa4663/problem-programmatically-inserting-image-in-onenote-2010-using-c?forum=officegeneral

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 3, 2016 6:37 AM
  • Hey Edward Z,

    thank you for the help.

    I already found the thread you linked but i have a problem there.

    I want to add a picture to the current opened page and the script in the thread hardcoded the name of the page.

    I have to somehow get the name of the page now. I can retrieve the ID of the current opened page but not the name.

    Thursday, November 3, 2016 8:04 AM
  • Hi Ennui,

    You could try to get the page name from ID.

    Here is a simple code:

    string strNamespace = "http://schemas.microsoft.com/office/onenote/2013/onenote";
                string m_xmlImageContent =
    "<one:Image><one:Size width=\"{1}\" height=\"{2}\" isSetByUser=\"true\" /><one:Data>{0}</one:Data></one:Image>";
                string m_xmlNewOutline =
                   "<?xml version=\"1.0\"?><one:Page xmlns:one=\"{2}\" ID=\"{1}\"><one:Title><one:OE><one:T><![CDATA[{3}]]></one:T></one:OE></one:Title>{0}</one:Page>";         
    
    
                Bitmap bitmap = new Bitmap(@"C:\Users\xx\Desktop\xx.png");
                MemoryStream stream = new MemoryStream();
                bitmap.Save(stream, ImageFormat.Jpeg);
                string fileString = Convert.ToBase64String(stream.ToArray());
                string notebookXml;
                OneNote.Application onenoteApp = new OneNote.Application();
    
                onenoteApp.GetHierarchy(null, HierarchyScope.hsPages, out notebookXml);
                var doc = XDocument.Parse(notebookXml);
                var ns = doc.Root.Name.Namespace;
                
                var existingPageId = onenoteApp.Windows.CurrentWindow.CurrentPageId; //pageNode.Attribute("ID").Value;
                var pageNodeName = doc.Descendants(ns + "Page").Where(n => n.Attribute("ID").Value == existingPageId).FirstOrDefault();
                string pageToBeChange = pageNodeName.Attribute("name").Value;
               
                string imageXmlStr = string.Format(m_xmlImageContent, fileString, bitmap.Width, bitmap.Height);
                string pageChangesXml = string.Format(m_xmlNewOutline, new object[] { imageXmlStr, existingPageId, strNamespace, pageToBeChange });
                onenoteApp.UpdatePageContent(pageChangesXml.ToString(), DateTime.MinValue);
    

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 4, 2016 9:22 AM
  • Hey Edward,

    thank you very much. It is working now. 

    I couldn't be happier.

    Have a nice weekend!

    Friday, November 4, 2016 1:00 PM
  • Hi Ennui,

    I am glad it works, and I would suggest you mark the reply as answer, and then others who run into the same issue would find the solution easily.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Saturday, November 5, 2016 4:35 AM