Skip to content

Commit 3e6bf7f

Browse files
committed
Fix GIF loading in the WinForms Demo
1 parent e8d7661 commit 3e6bf7f

1 file changed

Lines changed: 15 additions & 19 deletions

File tree

Source/Demo/WinForms/HtmlRenderingHelper.cs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System;
1515
using System.Collections.Generic;
1616
using System.Drawing;
17+
using System.Drawing.Imaging;
1718
using System.IO;
1819
using System.Threading;
1920
using TheArtOfDev.HtmlRenderer.Core.Entities;
@@ -71,18 +72,11 @@ public static Image TryLoadResourceImage(string src)
7172
public static XImage TryLoadResourceXImage(string src)
7273
{
7374
var img = TryLoadResourceImage(src);
74-
XImage xImg;
7575

7676
if (img == null)
7777
return null;
7878

79-
using (var ms = new MemoryStream())
80-
{
81-
img.Save(ms, img.RawFormat);
82-
xImg = img != null ? XImage.FromStream(ms) : null;
83-
}
84-
85-
return xImg;
79+
return CreatePdfSharpImage(img);
8680
}
8781

8882
/// <summary>
@@ -107,20 +101,10 @@ public static void OnImageLoadPdfSharp(object sender, HtmlImageLoadEventArgs e)
107101
public static void ImageLoad(HtmlImageLoadEventArgs e, bool pdfSharp)
108102
{
109103
var img = TryLoadResourceImage(e.Src);
110-
XImage xImg = null;
111-
112-
if (img != null)
113-
{
114-
using (var ms = new MemoryStream())
115-
{
116-
img.Save(ms, img.RawFormat);
117-
xImg = img != null ? XImage.FromStream(ms) : null;
118-
}
119-
}
120104

121105
object imgObj;
122106
if (pdfSharp)
123-
imgObj = xImg;
107+
imgObj = img != null ? CreatePdfSharpImage(img) : null;
124108
else
125109
imgObj = img;
126110

@@ -157,5 +141,17 @@ public static void ImageLoad(HtmlImageLoadEventArgs e, bool pdfSharp)
157141
if (img != null)
158142
e.Callback(imgObj);
159143
}
144+
145+
/// <summary>
146+
/// Convert image to PNG before creating <see cref="XImage"/> because PdfSharp does not support GIF streams.
147+
/// </summary>
148+
private static XImage CreatePdfSharpImage(Image img)
149+
{
150+
using (var ms = new MemoryStream())
151+
{
152+
img.Save(ms, ImageFormat.Png);
153+
return XImage.FromStream(ms);
154+
}
155+
}
160156
}
161157
}

0 commit comments

Comments
 (0)