none
weird FormatException on ColorTranslator.FromHtml, please help! RRS feed

  • Question

  • I have a weird problem that i cant seem to find a solution for.

    Some of our clients get a System.Formatexception from a piece of code in our system.

    I have tracked down the line that gives the problem and made a small test application as shown below.

    try
    {
    Color ApplicationColor = ColorTranslator.FromHtml("236; 239; 255");
    Console.WriteLine("Could get the Color: " + ApplicationColor.Name);
    }
    catch (Exception ex)
    {
    Console.WriteLine("Caught an exception: " + ex.ToString());
    }

    That code generate this exception on a very small number of computers.

    Caught an exception: System.Exception: 236; 239; 255 is not a valid value for Int32. 

    ---> System.FormatException: Input string was not in a correct format.
    at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
    at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
    at System.ComponentMode1.Int32Converter.FromString(String value, NumberFormat Info formatlnfo)
    at System.ComponentMode1.BaseHNmberConverter.ConvertFrom(ITypeDescriptorContext context, Culturelnfo culture, Object ualue)
    --- End of inner exception stack trace ---
    at System.ComponentMode1.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, Culturelnfo culture, Object ualue)
    at System.ComponentMode1.TypeConverter.ConvertFromString(ITypeDescriptorContext context, Culturelnfo culture, String text)
    at System.Drawing.Co1orConverter.ConvertFrom(ITypeDescriptorContext context, Culturelnfo culture, Object ualue)
    at System.ComponentMode1.TypeConverter.ConvertFromString(String text)
    at System.Drawing.Co1orTranslator.FromHtml(String htm1Color)
    at Testing32Hnd64bitDifference.Program.Hain(String[] args)

    Why does this Exception get thrown??? On all but two computers we have installed our system on the exception isn't thrown?!?!

    How do i fix it? What causes it?

    We have tried to reinstall .net but that didn't help at all.

    I tried google but found nothing useful :(

    I would really appriciate a solution or atleast some tips on where to look for the problem source.


    "Life would be much easier if i only had the sourcecode"

    Monday, June 11, 2012 6:25 PM

Answers

  • Ohh! That one worked for me.

    Ok.. I got it. In my culture, under regional settings, List Seperator is comma. Hence, my code is working. If I change it to something else, the code is not working. Check whether this is the issue.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Tuesday, June 12, 2012 12:10 PM
  • Actually the way you are using to represent color is a culture specific that would depend upon computer configuration, the one I suggested isn't a PC specific, it's generic, you wouldn't be facing those issues.

    Every component of color is passed as an integer. Have a look at the link


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Wednesday, June 13, 2012 4:49 AM

All replies

  • Check the value of sPositiveSign value in registry under the HKEY_CURRENT_USER\Control Panel\International as adviced in this article. It also might be some other internationalization issue.
    Monday, June 11, 2012 7:12 PM
  • Hi,

     What is the operating system and is it a 32 bit or 64 bit  that this works on? What system does it fail on?  Please include the type displays and their color resolution etc.

    This appears to be a difference in the color displays. Because the operating system will use the display settings for color information. If the system that this code fails on is a lower color resolution then that may be the problem, because that system may not have that color to translate too.

    Curtis

    Monday, June 11, 2012 7:17 PM
  • If you know the values of RGB components , I would suggest to use System.Drawing.Color.FromArgb method.

    Looks like you are not supplying valid input for ColorTranslator.FromHtml method.

    http://msdn.microsoft.com/en-us/library/system.drawing.colortranslator.fromhtml.aspx

     



    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Tuesday, June 12, 2012 7:20 AM
  • Yes, that is one possibility, but i really dont want to do a workaround for this if there is a problem on some computers. I want to know what that problem is. Otherwise some other random line of code might fail next time on those computers that are affected now.

    If I can be sure that it's just that specific function that is failing i would have done the workaround right away...


    "Life would be much easier if i only had the sourcecode"

    Tuesday, June 12, 2012 10:03 AM
  • Check the value of sPositiveSign value in registry under the HKEY_CURRENT_USER\Control Panel\International as adviced in this article. It also might be some other internationalization issue.

    This was, unfortunately, not the case. The value on that key was empty.

    What other internationalization issues might there be?

    I assume that all the computers that my clients have have been configured the same way by their IT department but i dont know for sure.



    "Life would be much easier if i only had the sourcecode"

    Tuesday, June 12, 2012 10:05 AM
  • Hi,

     What is the operating system and is it a 32 bit or 64 bit  that this works on? What system does it fail on?  Please include the type displays and their color resolution etc.

    This appears to be a difference in the color displays. Because the operating system will use the display settings for color information. If the system that this code fails on is a lower color resolution then that may be the problem, because that system may not have that color to translate too.

    Curtis

    The Operating system is Windows 7. Not sure if its a 32bit or 64 bit.

    But i have myself tried the code on both a win7 32 bit and 64 bit and i don't get the exception. Therefore i assumed that it would not be the operating system that was the problem.

    I thought that by the exception it was int32 that throw the exception, and that doesn't have anything with resolution to do? Anyway i just now tried the testcode on my computer with the resolution set to minimum (800x600) and i still didn't get the Exception.



    "Life would be much easier if i only had the sourcecode"

    Tuesday, June 12, 2012 10:06 AM
  • I think the format you are using is invalid. Use comma instead of semicolon like the below.

    Color ApplicationColor = ColorTranslator.FromHtml("236,239,255");
    Console.WriteLine("Could get the Color: " + ApplicationColor.Name);
    

    I hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Tuesday, June 12, 2012 10:26 AM
  • I think the format you are using is invalid. Use comma instead of semicolon like the below.

    Color ApplicationColor = ColorTranslator.FromHtml("236,239,255");
    Console.WriteLine("Could get the Color: " + ApplicationColor.Name);

    I hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!

    No, that was not it. In fact i get the same error as the affected computers when i exchange the ";" to a ",".

    Caught an exception: System.Exception: 236, 239, 255 is not a valid value for Int32. 
    ---> System.FormatException: Input string was not in a correct format.
       at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
       at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
       at System.ComponentModel.Int32Converter.FromString(String value, NumberFormatInfo formatInfo)
       at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
       --- End of inner exception stack trace ---
       at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
       at System.Drawing.ColorConverter.ConvertFrom(ITypeDescriptorContext context,CultureInfo culture, Object value)
       at System.Drawing.ColorTranslator.FromHtml(String htmlColor)


    "Life would be much easier if i only had the sourcecode"

    Tuesday, June 12, 2012 11:36 AM
  • Ohh! That one worked for me.

    Ok.. I got it. In my culture, under regional settings, List Seperator is comma. Hence, my code is working. If I change it to something else, the code is not working. Check whether this is the issue.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Tuesday, June 12, 2012 12:10 PM
  • Ohh! That one worked for me.

    Ok.. I got it. In my culture, under regional settings, List Seperator is comma. Hence, my code is working. If I change it to something else, the code is not working. Check whether this is the issue.


    Please mark this post as answer if it solved your problem. Happy Programming!

    I think you are on to something!

    We have checked with one of the affected users and he has manually changed the list separator. That probably why it doesn't work on his computer.

    So now is the question...how do i solve this for all users?

     CultureInfo applicationCulture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
                    applicationCulture.TextInfo.ListSeparator = ";";
                    Thread.CurrentThread.CurrentCulture = applicationCulture;
                    Thread.CurrentThread.CurrentUICulture = applicationCulture;
                    Color ApplicationColor = ColorTranslator.FromHtml("236;239;255");
    Like this?


    "Life would be much easier if i only had the sourcecode"

    Tuesday, June 12, 2012 5:18 PM
  • Actually the way you are using to represent color is a culture specific that would depend upon computer configuration, the one I suggested isn't a PC specific, it's generic, you wouldn't be facing those issues.

    Every component of color is passed as an integer. Have a look at the link


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Wednesday, June 13, 2012 4:49 AM