0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C#コードを使用してHTMLを画像に変換する

0
Posted at

HTMLを画像に変換することで、テキスト、グラフィック、レイアウトなどの動的なWebコンテンツを、PNGやJPEGといった静的な形式に変換できます。この処理は、ドキュメント用にWebページをキャプチャしたり、サムネイルを生成したり、プラットフォーム間で視覚的に一貫したコンテンツを確保したりするのに最適で、高い正確性と柔軟性を提供します。

この記事では、Spire.Doc for .NET を使用して、C#でHTMLファイルやHTML文字列を画像に変換する方法を解説します。

Spire.Doc for .NET のインストール

まずは、Spire.Doc for .NET パッケージに含まれている DLL ファイルを、.NET プロジェクトの参照として追加する必要があります。DLL ファイルは、こちらのリンクからダウンロードするか、NuGet を使用してインストールすることができます。

PM> Install-Package Spire.Doc

C# で HTML ファイルを画像に変換する

Spire.Doc for .NET を使用すると、Document.LoadFromFile() メソッドを利用して HTML ファイルを直接読み込むことができます。読み込み後、Document.SaveToImages() メソッドを使ってドキュメントを Bitmap 画像に変換できます。その後、生成された画像をループ処理し、PNG、JPG、BMP などの一般的な画像形式でそれぞれ保存することが可能です。

サンプルコードは以下のとおりです:

using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
using System.Drawing.Imaging;

namespace ConvertHtmlFileToPng
{
    class Program
    {
        static void Main(string[] args)
        {
            // Document オブジェクトを作成
            Document document = new Document();

            // HTML ファイルを読み込む
            document.LoadFromFile(@"C:\Users\Administrator\Desktop\MyHtml.html", FileFormat.Html, XHTMLValidationType.None);

            // 最初のセクションを取得
            Section section = document.Sections[0];

            // ページの余白を設定
            section.PageSetup.Margins.All = 2;

            // ドキュメントを Bitmap 画像の配列に変換
            Image[] images = document.SaveToImages(ImageType.Bitmap);

            // 画像を順に処理
            for (int index = 0; index < images.Length; index++)
            {
                // 出力ファイル名を指定
                string fileName = string.Format(@"C:\Users\Administrator\Desktop\Output\image_{0}.png", index);

                // 各画像を PNG ファイルとして保存
                images[index].Save(fileName, ImageFormat.Png);
            }

            // リソースを解放
            document.Dispose();
        }
    }
}

C# で HTML 文字列を画像に変換する

特定のケースでは、HTML 文字列を直接画像に変換する必要がある場合があります。この方法は、動的に生成されたコンテンツを扱う場合や、外部の HTML ファイルに依存したくない場合に特に有効です。

サンプルコードは以下のとおりです:

using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
using System.Drawing.Imaging;

namespace ConvertHtmlStringToPng
{
    class Program
    {
        static void Main(string[] args)
        {
            // Document オブジェクトを作成
            Document document = new Document();

            // ドキュメントにセクションを追加
            Section section = document.AddSection();

            // ページの余白を設定
            section.PageSetup.Margins.All = 2;

            // セクションに段落を追加
            Paragraph paragraph = section.AddParagraph();

            // ファイルから HTML 文字列を読み込む
            string htmlFilePath = @"C:\Users\Administrator\Desktop\Html.html";
            string htmlString = File.ReadAllText(htmlFilePath, System.Text.Encoding.UTF8);

            // 段落に HTML 文字列を追加
            paragraph.AppendHTML(htmlString);

            // ドキュメントを Bitmap 画像の配列に変換
            Image[] images = document.SaveToImages(ImageType.Bitmap);

            // 画像を順に処理
            for (int index = 0; index < images.Length; index++)
            {
                // 出力ファイル名を指定
                string fileName = string.Format(@"C:\Users\Administrator\Desktop\Output\image_{0}.png", index);

                // 各画像を PNG ファイルとして保存
                images[index].Save(fileName, ImageFormat.Png);
            }

            // リソースを解放
            document.Dispose();
        }
    }
}

一時ライセンスの申請

生成されたドキュメントから評価メッセージを削除したい場合や、機能制限を解除したい場合は、30 日間有効な試用ライセンスを申請してください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?