Add an Image to HTML with a .NET API
Aspose.HTML for .NET
loads an existing file into an
HTMLDocument
so a C# application can add an image through the DOM. Create an
HTMLImageElement
, assign its src and alt values, append it to the required container, and save the modified HTML.
Insert an Image into an Existing HTML File
The following example loads input.html, creates an <img> element for a local image, and appends it to the document body. The width and height attributes describe the image’s intrinsic dimensions so a browser can reserve layout space before the file loads.
C# Code to Add an Image to HTML
using Aspose.Html;
// Load the source HTML file
using HTMLDocument document = new HTMLDocument("input.html");
// Create and configure an image element
HTMLImageElement image = (HTMLImageElement)document.CreateElement("img");
image.Src = "images/mountain-lake.jpg";
image.Alt = "Mountain lake at sunrise";
image.SetAttribute("width", "1200");
image.SetAttribute("height", "800");
// Append the image and save the modified document
document.Body.AppendChild(image);
document.Save("output.html");
Steps to Add an Image to HTML in C#
- Load the existing HTML file with an
HTMLDocumentconstructor. - Call
CreateElement("img")and cast the result toHTMLImageElement. - Set the image source with
Srcand provide alternative text withAlt. - Add intrinsic
widthandheightvalues when they are known, and set any other required modern attributes withSetAttribute(). - Attach the image to the required parent with
AppendChild(). - Call
Save()to write the modified HTML document.
Build and Insert the Image Element
- Create a typed element. Call
CreateElement("img")and cast the result toHTMLImageElementso image-specific properties such asSrcandAltare available together with inherited DOM methods. - Select the destination container. Append the element to
document.Bodyfor a top-level image or locate a more specific article, figure, card, or other component before insertion. - Control insertion order. Use
AppendChild()to add the image after existing children. UseInsertBefore()when it must appear at a particular position relative to another node. - Replace a placeholder deliberately. When the document already contains an image placeholder, update that element or replace it instead of creating a duplicate node.
- Create repeated structures consistently. For galleries or product lists, create each image inside its corresponding container and assign unique IDs only when later selection requires them.
- Save after all DOM changes. Insert every required node before calling
Save()so the output represents one consistent document update.
To generate and preview an <img> element interactively, use the
HTML Image Code Generator
. For broader DOM changes, see
Edit HTML in C#
or
Create HTML in C#
.
Add Image Documentation and API Reference
Read
How to Add Image to HTML
for examples that create a new document, add an image to existing HTML, and define CSS background images. The
HTMLImageElement
reference lists image properties and inherited DOM methods, while
Document.CreateElement()
creates the element used by the C# workflow.
Get Started with Aspose.HTML for .NET API
Install Aspose.HTML for .NET from NuGet with the .NET CLI:
dotnet add package Aspose.HTML
You can also use the NuGet Package Manager UI or run Install-Package Aspose.HTML in the Package Manager Console. See the
installation guide
for setup details, or request a
temporary license
to evaluate the API without evaluation limitations.
Add Images to HTML FAQ
1. How do I add an image from a local folder to HTML?
Set the image src to a relative path such as images/photo.jpg and place the file at that path relative to the saved HTML document. Use an absolute file URL only when the document is intended for that specific local environment.
2. Can an HTML image use an external URL?
Yes. Set src to an absolute HTTP or HTTPS URL. The browser or renderer must be able to reach that address when it loads the document.
3. Does adding an <img> element embed the image in the HTML file?
No. A normal src value references a separate local or remote resource. Use a data URI when the image bytes must be embedded directly in the HTML, or configure resource handling when saving a complete document package.
4. What should I write in the alt attribute?
Describe the image’s purpose or information in the surrounding context. Use alt="" for a purely decorative image, and avoid repeating nearby text when it already communicates the same information.
5. Can I add responsive images with Aspose.HTML for .NET?
Yes. Set srcset and sizes on an <img> element or create a <picture> structure with <source> elements. CSS can control the displayed size while the browser chooses an appropriate resource.
Related HTML Image Workflows
Continue with another image or HTML creation task.