Inserting a Picture content control
within C# and VB.NET.


    Fills the content control with a single image. Prohibits users from inserting any other Word objects or any text.

Complete code

using System;
using System.Threading.Tasks;
using SautinSoft.Document;
using SautinSoft.Document.CustomMarkups;
using SautinSoft.Document.Drawing;
using SautinSoft.Document.Tables;
using System.Net;
using System.Net.Http;
using System.IO;



namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            InsertPicture();
        }
        /// <summary>
        /// Inserting a Picture content control.
        /// </summary>
        /// <remarks>
        /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/content-controls-insert-picture-net-csharp-vb.php
        /// </remarks>

        static void InsertPicture()
        {
            // Let's create a simple document.
            DocumentCore dc = new DocumentCore();
            Picture pict, pict1, pict2;

            // See the remarks on https://docs.microsoft.com/en-us/dotnet/api/system.net.webclient?view=net-5.0
            using (WebClient client = new WebClient())
            {
                byte[] imageBytes = client.DownloadData("https://www.sautinsoft.com/images/banner_sautinsoft.jpg");
                using (MemoryStream ms = new MemoryStream(imageBytes))
                {
                    pict = new Picture(dc, new InlineLayout(new Size(400, 100)), ms);
                }
            }

            using (HttpClient client = new HttpClient())
            {
                Stream firstImageStream = client.GetStreamAsync("https://sautinsoft.com/images/developer.png").Result;
                var ms = new MemoryStream();
                firstImageStream.CopyTo(ms);
                pict1 = new Picture(dc, new InlineLayout(new Size(50, 50)), ms);
            }

            Section section = new Section(dc);
            dc.Sections.Add(section);

            section.Blocks.Add(new Paragraph(dc, "Picture below is inside the block-level content control:"));

            // Create a picture content control.
            BlockContentControl control = new BlockContentControl(dc, ContentControlType.Picture, new Paragraph(dc, pict));
            section.Blocks.Add(control);

            Paragraph par = new Paragraph(dc,
                new Run(dc, "Following picture is inside the inline-level content control: "),
                new InlineContentControl(dc, ContentControlType.Picture, pict1));
            section.Blocks.Add(par);

            section.Blocks.Add(new Paragraph(dc, "Insert a picture content control from the local disk:"));
            string pictPath = @"..\..\picture.jpg";
            pict2 = new Picture(dc, new InlineLayout(new Size(100, 100)), pictPath);
            BlockContentControl localpict = new BlockContentControl(dc, ContentControlType.Picture, new Paragraph(dc, pict2));
            section.Blocks.Add(localpict);

            // Save our document into DOCX format.
            string resultPath = @"result.docx";
            dc.Save(resultPath);

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(resultPath) { UseShellExecute = true });
        }
    }
}

Download.

            Option Infer On
Imports System.Threading.Tasks
Imports SautinSoft.Document
Imports SautinSoft.Document.CustomMarkups
Imports SautinSoft.Document.Drawing
Imports SautinSoft.Document.Tables
Imports System.Net
Imports System.Net.Http
Imports System.IO



Namespace Example
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			InsertPicture()
		End Sub
		''' <summary>
		''' Inserting a Picture content control.
		''' </summary>
		''' <remarks>
		''' Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/content-controls-insert-picture-net-csharp-vb.php
		''' </remarks>

		Private Shared Sub InsertPicture()
			' Let's create a simple document.
			Dim dc As New DocumentCore()
			Dim pict, pict1, pict2 As Picture

			' See the remarks on https://docs.microsoft.com/en-us/dotnet/api/system.net.webclient?view=net-5.0
			Using client As New WebClient()
				Dim imageBytes() As Byte = client.DownloadData("https://www.sautinsoft.com/images/banner_sautinsoft.jpg")
				Using ms As New MemoryStream(imageBytes)
					pict = New Picture(dc, New InlineLayout(New Size(400, 100)), ms)
				End Using
			End Using

			Using client As New HttpClient()
				Dim firstImageStream As Stream = client.GetStreamAsync("https://sautinsoft.com/images/developer.png").Result
				Dim ms = New MemoryStream()
				firstImageStream.CopyTo(ms)
				pict1 = New Picture(dc, New InlineLayout(New Size(50, 50)), ms)
			End Using

			Dim section As New Section(dc)
			dc.Sections.Add(section)

			section.Blocks.Add(New Paragraph(dc, "Picture below is inside the block-level content control:"))

			' Create a picture content control.
			Dim control As New BlockContentControl(dc, ContentControlType.Picture, New Paragraph(dc, pict))
			section.Blocks.Add(control)

			Dim par As New Paragraph(dc, New Run(dc, "Following picture is inside the inline-level content control: "), New InlineContentControl(dc, ContentControlType.Picture, pict1))
			section.Blocks.Add(par)

			section.Blocks.Add(New Paragraph(dc, "Insert a picture content control from the local disk:"))
			Dim pictPath As String = "..\picture.jpg"
			pict2 = New Picture(dc, New InlineLayout(New Size(100, 100)), pictPath)
			Dim localpict As New BlockContentControl(dc, ContentControlType.Picture, New Paragraph(dc, pict2))
			section.Blocks.Add(localpict)

			' Save our document into DOCX format.
			Dim resultPath As String = "result.docx"
			dc.Save(resultPath)

			' Open the result for demonstration purposes.
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(resultPath) With {.UseShellExecute = True})
		End Sub
	End Class
End Namespace

Download.


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.