PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Problem with setting image resolution when render to rtf
https://forum.pdfsharp.net/viewtopic.php?f=3&t=3095
Page 1 of 1

Author:  Vita [ Thu Apr 23, 2015 10:16 am ]
Post subject:  Problem with setting image resolution when render to rtf

Hello everybody,

If I set image resolution in code:

Code:
MigraDoc.DocumentObjectModel.Document document = new MigraDoc.DocumentObjectModel.Document();
var section = AddSection(document, pageSetup);

var image = section.AddImage("image.png");
image.Resolution = 300;

rtfRenderer.Render(document, test.rtf, ".")


then I get error
Quote:
Specified cast is not valid
in method
Quote:
MigraDoc.RtfRendering.ImageRenderer.CalculateImageDimensions
on line
Code:
horzResolution = (float)GetValueAsIntended("Resolution");


Problem is in cast double result GetValueAsIntended("Resolution") to float. I solved problem with following proposed patch.

Code:
 .../MigraDoc.RtfRendering/ImageRenderer.cs             | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/pdfsharp/MigraDoc/code/MigraDoc.RtfRendering/MigraDoc.RtfRendering/ImageRenderer.cs b/pdfsharp/MigraDoc/code/MigraDoc.RtfRendering/MigraDoc.RtfRendering/ImageRenderer.cs
index 273cbe9..f824631 100644
--- a/pdfsharp/MigraDoc/code/MigraDoc.RtfRendering/MigraDoc.RtfRendering/ImageRenderer.cs
+++ b/pdfsharp/MigraDoc/code/MigraDoc.RtfRendering/MigraDoc.RtfRendering/ImageRenderer.cs
@@ -205,23 +205,23 @@ namespace MigraDoc.RtfRendering
         //System.Drawing.Bitmap bip2 = new System.Drawing.Bitmap(imageFile);
         XImage bip = XImage.FromFile(this.filePath);
 
-        float horzResolution;
-        float vertResolution;
+        double horzResolution;
+        double vertResolution;
         string ext = Path.GetExtension(this.filePath).ToLower();
-        float origHorzRes = (float)bip.HorizontalResolution;
-        float origVertRes = (float)bip.VerticalResolution;
+      double origHorzRes = bip.HorizontalResolution;
+      double origVertRes = bip.VerticalResolution;
 
         this.originalHeight = bip.PixelHeight * 72 / origVertRes;
         this.originalWidth = bip.PixelWidth * 72 / origHorzRes;
 
         if (this.image.IsNull("Resolution"))
         {
-          horzResolution = (ext == ".gif") ? 72 : (float)bip.HorizontalResolution;
-          vertResolution = (ext == ".gif") ? 72 : (float)bip.VerticalResolution;
+         horzResolution = (ext == ".gif") ? 72 : bip.HorizontalResolution;
+         vertResolution = (ext == ".gif") ? 72 : bip.VerticalResolution;
         }
         else
         {
-          horzResolution = (float)GetValueAsIntended("Resolution");
+          horzResolution = (double)GetValueAsIntended("Resolution");
           vertResolution = horzResolution;
         }
 
@@ -233,9 +233,9 @@ namespace MigraDoc.RtfRendering
 
         bool scaleWidthIsNull = this.image.IsNull("ScaleWidth");
         bool scaleHeightIsNull = this.image.IsNull("ScaleHeight");
-        float sclHeight = scaleHeightIsNull ? 1 : (float)GetValueAsIntended("ScaleHeight");
+      double sclHeight = scaleHeightIsNull ? 1 : (double)GetValueAsIntended("ScaleHeight");
         this.scaleHeight = sclHeight;
-        float sclWidth = scaleWidthIsNull ? 1 : (float)GetValueAsIntended("ScaleWidth");
+      double sclWidth = scaleWidthIsNull ? 1 : (double)GetValueAsIntended("ScaleWidth");
         this.scaleWidth = sclWidth;
 
         bool doLockAspectRatio = this.image.IsNull("LockAspectRatio") || this.image.LockAspectRatio;

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/