Documents for Word .NET Edition
Features / Images
In This Topic
    Images
    In This Topic

    Images are generally used to illustrate important information in your document and highlight points raised in the text. In GcWord, an image or a picture element is represented by the Picture class which allows you to access all the image properties. GcWord allows you to add an image on a word document using Add method of the PictureCollection class that represents a collection of picture elements.

    Image in a Word document

    Add Image from File

    To add an image in a document from file:

    1. Create a byte array from the image using the ReadAllBytes method.
    2. Access the picture collection using Pictures property of the RangeBase class.
    3. Add image to the picture collection using Add method of the PictureCollection class, which accepts image as a byte array. For example, insert an image in the first paragraph.
    4. Set properties of the image. For example, set the height and width of the image using Height.Value and Width.Value properties respectively.
      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");
    Back to Top

    Add Image from Stream

    To add an image in a document from memory stream:

    1. Access a memory stream loaded with image data.
    2. Convert the memory stream to byte array using the ToArray method.
    3. Add the image to picture collection using Add method of the PictureCollection class, which accepts image as a byte array.
    4. Set some properties of the image. For example, set Height.Value and Width.Value properties.
      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");
    Back to Top

    Get Image

    To get an image:

    1. Access an image from the picture collection using Pictures property of the RangeBase class.
    2. Get the image data using ImageBytes property of the ImageData class.
    3. Add the fetched image from the document to another document using Add method of the PictureCollection class.
      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");
    Back to Top

    Edit Image

    To edit an image:

    1. Access the image from picture collection using Pictures property of the RangeBase class.
    2. Change the properties of the image. For example, change the rotation properties such as Angle and VerticalFlip properties of the ShapeRotation class.
      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");
    Back to Top

    Delete Image

    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");
    Back to Top

    For more information on how to work with images in a Word document using GcWord, see GcWord sample browser.