Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active December 23, 2021 05:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/12bbcc53310dce4971854eae512f54e8 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/12bbcc53310dce4971854eae512f54e8 to your computer and use it in GitHub Desktop.
Insert or Extract Images from OneNote .one File Programmatically using C#
// Load the document with Document class.
Document oneFile = new Document(dataDir + "Aspose.one");
// Get all Image nodes
IList<Image> nodes = oneFile.GetChildNodes<Image>();
foreach (Image image in nodes)
{
using (MemoryStream stream = new MemoryStream(image.Bytes))
{
using (Bitmap bitMap = new Bitmap(stream))
{
// Save image bytes to a file
bitMap.Save(String.Format("{0}", Path.GetFileName(image.FileName)));
}
}
}
// Initialize LoadOptions class object.
LoadOptions options = new LoadOptions();
Document oneFile = new Document("Sample1.one", options);
// Get the first page of the document.
Page page = oneFile.FirstChild;
// Load an image from the file.
Image image = new Image(oneFile, "Input.jpg");
// Change the image's size according to your needs (optional).
image.Width = 100;
image.Height = 100;
// Set the image's location in the page (optional).
image.VerticalOffset = 400;
image.HorizontalOffset = 100;
// Set image alignment
image.Alignment = HorizontalAlignment.Right;
// Add the image to the page.
page.AppendChildLast(image);
// Save the document in the .one format.
oneFile.Save("InsertImage_out.one", SaveFormat.One);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment