Images are generally used to illustrate important information in your document and highlight points raised in the text. In DsWord, an image or a picture element is represented by the Picture class which allows you to access all the image properties. DsWord allows you to add an image on a word document using Add method of the PictureCollection class that represents a collection of picture elements.
To add an image in a document from file:
C# |
Copy Code |
---|---|
GcWordDocument doc = new GcWordDocument(); // Load picture data: var picBytes = File.ReadAllBytes(Path.Combine("Resources", "Images", "road.jpg")); //Add a paragraph var pars = doc.Body.Sections.First.GetRange().Paragraphs; pars.Add("A JPEG image:"); // Add picture, specifying its mime type: var pic = pars.First.GetRange().Runs.First.GetRange().Pictures.Add(picBytes, "image/jpeg"); // Set picture size pic.Size.Width.Value = 500; pic.Size.Height.Value = 400; //Save the document doc.Save("AddFromFile.docx"); |
To add an image in a document with same size as the input image, you can use System.Drawing.Image namespace:
C# |
Copy Code |
---|---|
using DrawingImage = System.Drawing.Image; GcWordDocument doc = new GcWordDocument(); //Load image file var fileName = Path.Combine(@"cat.png"); using (var image = DrawingImage.FromFile(fileName)) { var pic = doc.Body.Paragraphs.Add("").GetRange().Runs.First.GetRange().Pictures.Add(image); //Output: Original Image width is 400 and Added image width is 400 Console.WriteLine(String.Format(@"Original Image width is {0} and Added image width is {1}", image.Size.Width, pic.Size.Width.Value)); //Output: Original Image height is 300 and Added height is 300 Console.WriteLine(String.Format(@"Original Image height is {0} and Added height width is {1}", image.Size.Height, pic.Size.Height.Value)); doc.Save("ImageAdded.docx"); } |
Limitation
The emf, wmf and ico file formats are not supported while adding images using System.Drawing.Image namespace.
Similarly, you can add an image in a document with same size as the input image using Image class of GrapeCity.Documents.Drawing namespace as shown below:C# |
Copy Code |
---|---|
using GcImage = GrapeCity.Documents.Drawing.Image; GcWordDocument doc = new GcWordDocument(); //Load image file var fileName = Path.Combine(@"cat.png"); using (var image = GcImage.FromFile(fileName)) { var pic2 = doc.Body.Paragraphs.Add("").GetRange().Runs.First.GetRange().Pictures.Add(image); //Output: Original Image width is 400 and Added image width is 400 Console.WriteLine(String.Format(@"Original Image width is {0} and Added image width is {1}", image2.Width, pic2.Size.Width.Value)); //Output: Original Image height is 300 and Added height is 300 Console.WriteLine(String.Format(@"Original Image height is {0} and Added height width is {1}", image2.Height, pic2.Size.Height.Value)); doc.Save("ImageAdded.docx"); } |
Limitation
The emf and wmf file formats are not supported while adding images using GrapeCity.Documents.Drawing namespace.
To add an image in a document from memory stream:
C# |
Copy Code |
---|---|
//Load image to MemoryStream MemoryStream ms = new MemoryStream(); System.Drawing.Image imageIn = System.Drawing.Image.FromFile(Path.Combine( "Resources", "Images", "road.jpg")); imageIn.Save(ms, imageIn.RawFormat); //Convert MemoryStream to byte array var picBytes = ms.ToArray(); //Add a paragraph var pars = doc.Body.Sections.First.GetRange().Paragraphs; pars.Add("A JPEG image:"); // Add picture, specifying its mime type: var pic = pars.First.GetRange().Runs.First.GetRange().Pictures.Add(picBytes, "image/jpeg"); // Set picture size pic.Size.Width.Value = 500; pic.Size.Height.Value = 400; //Save the document doc.Save("AddFromStream.docx"); |
To get an image:
C# |
Copy Code |
---|---|
doc.Load("AddFromFile.docx"); //Extract image from existing document Picture oldpic = doc.Body.Paragraphs.First.GetRange().Runs.First.GetRange().Pictures[0]; byte[] picBytes = oldpic.ImageData.ImageBytes; //Add extracted image from old document to new document GcWordDocument testDocument = new GcWordDocument(); var pars = testDocument.Body.Sections.First.GetRange().Paragraphs; pars.Add("An old JPEG image:"); // Add picture, specifying its mime type: var pic = pars.First.GetRange().Runs.First.GetRange().Pictures.Add(picBytes, "image/jpeg"); // Set picture size pic.Size.Width.Value = 500; pic.Size.Height.Value = 400; //Save the document testDocument.Save("ExtractImage.docx"); |
To edit an image:
C# |
Copy Code |
---|---|
doc.Load("AddFromFile.docx"); //Edit image from existing document Picture pic = doc.Body.Paragraphs.First.GetRange().Runs.First.GetRange().Pictures[0]; //Set the rotation properties pic.Rotation.Angle = 45; pic.Rotation.VerticalFlip = true; //Save the document doc.Save("EditIma" + "ge.docx"); |
To delete an image from a document, access the image from the picture collection using Pictures property of the RangeBase class and delete it using the Delete method.
C# |
Copy Code |
---|---|
doc.Load("AddFromFile.docx"); //Delete image from existing document Picture pic = doc.Body.Paragraphs.First.GetRange().Runs.First.GetRange().Pictures[0]; pic.Delete(); //Save the document doc.Save("DeleteImage.docx"); |
For more information on how to work with images in a Word document using DsWord, see DsWord sample browser.