C#-FORM: Problem using the function FromArgb(Int32)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oberon
    New Member
    • Mar 2008
    • 14

    C#-FORM: Problem using the function FromArgb(Int32)

    Hi,
    I am trying to use the function Color.FromArgb( Int32). The Int32 is on the format AARRGGBB, where AA is the alpha component.

    My problem is that when I want to specify a solid color I need to give the alpha value FF, but that will not fit in an Int32 value (e.g. 0xFFFF0000 for solid red), since the largest Int32 value is 0x7FFFFFFF. How can I define a (near) solid color using this function?
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    It should?
    .FromArgb() takes a 32 bit int.
    32bits = 4 bytes
    1 byte for AA
    1 byte for RR
    1 byte for GG
    1 byte for BB

    Although in your case I would use one of the overloads which is in this format:
    Code:
    Color.FromArgb(red,green,blue)

    Comment

    • oberon
      New Member
      • Mar 2008
      • 14

      #3
      Thanks for your reply.

      The overload Color.FromArgb( r, g, b) does not quite cover my needs as I want to be able to use alpha components also below 0xFF.

      I found out that by placing the variable assignment inside an unchecked scope, it will compile:
      Code:
      unchecked
      {
      	Int32 argbValue = (Int32)0x9FFF0000;
      	Color c = Color.FromArgb(argbValue);
      }
      If I don't include the "unchecked" it will not compile, complaining that the value "cannot be converted to a 'int' ".

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        You're killing me here guy.
        LOOK at the function, shesh.

        Here's another overload:
        Color.FromArgb( alpha, red, green, blue);

        If you have your heart set on using a single value, you can do it without having to use unchecked:
        Code:
        int argb=BitConverter.ToInt32(new byte[] { 0x9f, 0xff, 0x00, 0x00 }, 0); 
        Color c = Color.FromArgb(argb);

        Comment

        • vladb
          New Member
          • Sep 2009
          • 1

          #5
          Method #1:
          Code:
              private static Color IntToColorWithAplphaFF(int i)
              {
                  return Color.FromArgb(0xFF, Color.FromArgb(i));
              }
          Method #2:
          Code:
              private static Color IntToColorWithAplphaFF2(int i)
              {
                  return Color.FromArgb(i | ((0xFFFFFF - i + 1)*-1));
              }
          Last edited by tlhintoq; Sep 6 '09, 11:16 AM. Reason: [CODE] ... your code here ... [/CODE] tags added

          Comment

          Working...