Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions Source/Demo/WinForms/HtmlRenderingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;
using TheArtOfDev.HtmlRenderer.Core.Entities;
Expand Down Expand Up @@ -71,18 +72,11 @@ public static Image TryLoadResourceImage(string src)
public static XImage TryLoadResourceXImage(string src)
{
var img = TryLoadResourceImage(src);
XImage xImg;

if (img == null)
return null;

using (var ms = new MemoryStream())
{
img.Save(ms, img.RawFormat);
xImg = img != null ? XImage.FromStream(ms) : null;
}

return xImg;
return CreatePdfSharpImage(img);
}

/// <summary>
Expand All @@ -107,20 +101,10 @@ public static void OnImageLoadPdfSharp(object sender, HtmlImageLoadEventArgs e)
public static void ImageLoad(HtmlImageLoadEventArgs e, bool pdfSharp)
{
var img = TryLoadResourceImage(e.Src);
XImage xImg = null;

if (img != null)
{
using (var ms = new MemoryStream())
{
img.Save(ms, img.RawFormat);
xImg = img != null ? XImage.FromStream(ms) : null;
}
}

object imgObj;
if (pdfSharp)
imgObj = xImg;
imgObj = img != null ? CreatePdfSharpImage(img) : null;
else
imgObj = img;

Expand Down Expand Up @@ -157,5 +141,17 @@ public static void ImageLoad(HtmlImageLoadEventArgs e, bool pdfSharp)
if (img != null)
e.Callback(imgObj);
}

/// <summary>
/// Convert image to PNG before creating <see cref="XImage"/> because PdfSharp does not support GIF streams.
/// </summary>
private static XImage CreatePdfSharpImage(Image img)
{
using (var ms = new MemoryStream())
{
img.Save(ms, ImageFormat.Png);
return XImage.FromStream(ms);
}
}
}
}
Loading