ReplaceImage.cs
//
// This code is part of GrapeCity Documents for Word samples.
// Copyright (c) GrapeCity, Inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Word;

namespace GcWordWeb.Samples
{
    // This sample loads an existing document, and replaces
    // the (only) image in it with a different one.
    public class ReplaceImage
    {
        public GcWordDocument CreateDocx()
        {
            // The new image data:
            var picBytes = File.ReadAllBytes(Path.Combine("Resources", "ImagesBis", "DianeForsyth.jpg"));

            // Load the document:
            var doc = new GcWordDocument();
            doc.Load(Path.Combine("Resources", "WordDocs", "ProcurementLetter.docx"));

            // Access the image to replace:
            var pic = doc.Body.Sections.First.GetRange().Pictures.First;
            // Replace the image data in place:
            pic.ImageData.SetImage(picBytes, "image/jpeg");

            // Done:
            return doc;
        }
    }
}