Looking at the code for the image importer
Looking at the code for the image importer doesn't show many changes from the text importer. A few are worth calling out, though.
I decided to reuse the design of creating new pages, so getting the XML correctly formatted is the first step:
private string m_xmlImageContent = //page level image
"<one:Image><one:Size width=\"{1}\" height=\"{2}\" isSetByUser=\"true\" /><one:Data>{0}</one:Data></one:Image>";
private string m_xmlNewOutline =
"<?xml version=\u00221.0\u0022?>" +
"<one:Page xmlns:one=\u0022{2}\u0022 ID=\u0022{1}\u0022>" +
"<one:Title><one:OE><one:T><![CDATA[{3}]]></one:T></one:OE></one:Title>" +
"{0}</one:Page>";//the {0} here will become the page level image object
The comment about the image being a page level object is important. When you add an image (or some other content such as ink) to a page, it can be placed in an outline or directly on the page. If it shows in an outline, it will have the container drawn around it, like this:
If it is placed directly on the page, you won't see the outline container when you select it, which looks like this visually:
I decided to go with the page level item to make sizing it easier. If I wanted to put the image in an outline, I would need to resize the outline and potentially the image as well depending on the image size and whether it was larger than a default outline.
I did a little refactoring to get the naming converted from Text Importer to Image Importer. This project initially started as a JPG only importer so there are a few instances of JPG only naming left.
I also knew I would need to Base64 data for the image to import correctly through our API. A quick check of MSDN showed me this capability is built into the .net framework on the bitmap item:
Bitmap bm = new Bitmap(pathToJPGFiles[i]);
MemoryStream ms = new MemoryStream();
{
bm.Save(ms, ImageFormat.Jpeg);
pText= Convert.ToBase64String(ms.ToArray());
}
Once I saw this was built into the "bitmap" object, I figured it should be easy to get the code to work for more than simply JPG files. The code was easy (just update to use BMP, PNG and GIF. It may be just as easy to extend this to DIB and TIFF - I did not try.
The hard part was using the choose file dialog instead of the folder picker. The advantage of the folder picker for text files was that I could iterate through the folder and look only for files that have a TXT extension and import them. The file picker does not return a folder - it actually returns a list of files. So I could have done a loop (essentially) for each file type, or just let the user pick the files to import. I chose the latter.
Finally, I added a little code to get the height and width from the image and use that to set the height and width for OneNote as attributes of the image object.
Then it is just the same steps from the text importer - build the XML, create a new page and add the content to the page.
Questions, comments, concerns and criticisms always welcome,
John
Comments
- Anonymous
January 04, 2011
I don't know about the exact functionality of OneNote at this point but since you are only considering the import of raster images I wonder: what about importing vector drawings? I hardly ever use raster images these days, I almost always use vector drawings (I get them as PDFs but might be able to convert them to other formats - for example, I am converting my PDFs to a form of XAML that I can load into a shared whiteboard of my own design). Is there a way to import vector drawings (starting from PDF, say)? In OneNote 2007 there was no support for that. Why is it that something like XAML cannot be imported, isn't it a native graphics format for Vista and Windows 7? (At least it could not be imported in OneNote 2007). Regards, Christian