How to set properties to store images in C# and VB.Net

How to set properties to store images
in C# and VB.Net.


Complete code

using System;
using System.IO;
using System.Text;

namespace SampleConvert
{
	class sample
	{
		static void Main(string[] args)
		{
            string inpFile = @"..\..\images.rtf";

            SautinSoft.RtfToHtml r = new SautinSoft.RtfToHtml();

            try
            {
                r.OpenRtf(inpFile);

                // Case "1" - store image as separate *.png files in subfolder "filename.images".
                string outFile = Path.Combine(Directory.GetCurrentDirectory(), "case 1.html");

                // The image folder must be already existed.
                r.ImageStyle.ImageFolder = Path.GetDirectoryName(outFile);

                // If subfolder is not exist, it will be created by the component.
                r.ImageStyle.ImageSubFolder = "case 1.images";

                // A template name for images.
                r.ImageStyle.ImageFileName = "picture";

                // Don't embed images inside HTML document.
                r.ImageStyle.IncludeImageInHtml = false;

                // Assume, that we already have 100 images in the subfolder, let's start to name images from 101.
                r.ImageStyle.ImageNumStart = 101;
                r.ImageStyle.ImagesFormat = SautinSoft.RtfToHtml.eImageFormat.Png;

                // Case "1" - convert to HTML and Show the result.
                r.TextStyle.Title = "Case 1 - PNG files";
                r.ToHtml(outFile);
                System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile)
                { UseShellExecute = true });

                // Case "2" - store image as separate *.jpeg files in subfolder "filename.images".
                outFile = Path.Combine(Directory.GetCurrentDirectory(), "case 2.html");
                r.ImageStyle.ImagesFormat = SautinSoft.RtfToHtml.eImageFormat.Jpg;
                r.TextStyle.Title = "Case 2 - JPEG files";
                r.ToHtml(outFile);
                System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile)
                { UseShellExecute = true });

                // Case "3" - skip images.
                outFile = Path.Combine(Directory.GetCurrentDirectory(), "case 3.html");
                r.ImageStyle.PreserveImages = false;
                r.TextStyle.Title = "Case 3 - No images";
                r.ToHtml(outFile);
                System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile)
                { UseShellExecute = true });

                // Case "4" - embed images inside HTML document using base64.
                outFile = Path.Combine(Directory.GetCurrentDirectory(), "case 4.html");
                r.ImageStyle.PreserveImages = true;
                r.ImageStyle.IncludeImageInHtml = true;
                r.TextStyle.Title = "Case 4 - Embedded images";
                r.ToHtml(outFile);
                System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile)
                { UseShellExecute = true });
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
                Console.WriteLine("Press any key ...");
                Console.ReadKey();
            }
		}
	}
}

Download.

        
            Imports System
Imports System.IO
Imports System.Text

Namespace SampleConvert
    Friend Class sample
        Shared Sub Main(ByVal args() As String)
            Dim inpFile As String = "..\..\images.rtf"

            Dim r As New SautinSoft.RtfToHtml()

            Try
                r.OpenRtf(inpFile)

                ' Case "1" - store image as separate *.png files in subfolder "filename.images".
                Dim outFile As String = Path.Combine(Directory.GetCurrentDirectory(), "case 1.html")

                ' The image folder must be already existed.
                r.ImageStyle.ImageFolder = Path.GetDirectoryName(outFile)

                ' If subfolder is not exist, it will be created by the component.
                r.ImageStyle.ImageSubFolder = "case 1.images"

                ' A template name for images.
                r.ImageStyle.ImageFileName = "picture"

                ' Don't embed images inside HTML document.
                r.ImageStyle.IncludeImageInHtml = False

                ' Assume, that we already have 100 images in the subfolder, let's start to name images from 101.
                r.ImageStyle.ImageNumStart = 101
                r.ImageStyle.ImagesFormat = SautinSoft.RtfToHtml.eImageFormat.Png

                ' Case "1" - convert to HTML and Show the result.
                r.TextStyle.Title = "Case 1 - PNG files"
                r.ToHtml(outFile)
                System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})

                ' Case "2" - store image as separate *.jpeg files in subfolder "filename.images".
                outFile = Path.Combine(Directory.GetCurrentDirectory(), "case 2.html")
                r.ImageStyle.ImagesFormat = SautinSoft.RtfToHtml.eImageFormat.Jpg
                r.TextStyle.Title = "Case 2 - JPEG files"
                r.ToHtml(outFile)
                System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})

                ' Case "3" - skip images.
                outFile = Path.Combine(Directory.GetCurrentDirectory(), "case 3.html")
                r.ImageStyle.PreserveImages = False
                r.TextStyle.Title = "Case 3 - No images"
                r.ToHtml(outFile)
                System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})

                ' Case "4" - embed images inside HTML document using base64.
                outFile = Path.Combine(Directory.GetCurrentDirectory(), "case 4.html")
                r.ImageStyle.PreserveImages = True
                r.ImageStyle.IncludeImageInHtml = True
                r.TextStyle.Title = "Case 4 - Embedded images"
                r.ToHtml(outFile)
                System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})
            Catch ex As Exception
                Console.WriteLine($"Error: {ex.Message}")
                Console.WriteLine("Press any key ...")
                Console.ReadKey()
            End Try
        End Sub
    End Class
End Namespace

Download.

If you are looking also for a .Net solution to Create or Modify HTML documents, see our Document .Net.


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.