Note that there are some explanatory texts on larger screens.

plurals
  1. PO.NET invalid cast exception on double division
    primarykey
    data
    text
    <p>I am working on a .NET application to reduce image file sizes using <a href="http://en.wikipedia.org/wiki/ImageMagick" rel="nofollow noreferrer">ImageMagick</a>. My application stores some "settings" in a <code>Hashtable</code> for use in various areas of the application. I am now adding a feature to reduce image size based on desired <a href="http://en.wikipedia.org/wiki/Dots_per_inch" rel="nofollow noreferrer">DPI</a> in the transformed image. Basically, I am looking up the DPI in the unaltered image, calculating the percentage of the desired DPI in relation to the current DPI, and then I will resize the image dimensions by this proportion.</p> <p>ImageMagick reports image DPI as floating point values. So 200x200 DPI is really 199.975x199.975. Thus I use <code>Math.Ceiling()</code> to get the values into my application. When I try to use the desired DPI from my settings <code>Hashtable</code> to do the percentage calculation I get an invalid cast exception. I don't know why this is happening.</p> <p>Here is a test case that fails in the same manner as my actual code:</p> <pre><code> using System; using System.Collections; namespace typetest { class Program { private struct DPI { public double x; public double y; } public static void Main(string[] args) { Hashtable vars = new Hashtable(); DPI dpi; string dpiString = "199.547:199.547"; string[] ret; vars["newDpiX"] = 150; vars["newDpiY"] = 150; ret = dpiString.Split(':'); dpi.x = ( (double)vars["newDpiX"] / Math.Ceiling(double.Parse(ret[0])) ) * 100; dpi.y = ( (double)vars["newDpiY"] / Math.Ceiling(double.Parse(ret[1])) ) * 100; Console.WriteLine("New DPI percentage = " + dpi.x + "%x" + dpi.y +"%"); } } } </code></pre> <p>If I change the division to</p> <pre><code> double newDpiX = 150.0; dpi.x = ( newDpiX / Math.Ceiling(double.Parse(ret[0])) ) * 100; </code></pre> <p>it will work as expected. What is going on here?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload