简体   繁体   中英

Unhandled Exception: System.ArgumentException: Cannot widen from source type to target type

the following scripts have unhandled exceptions.Unhandled Exception: System.ArgumentException: Cannot widen from source type to target type either because the source type is a not a primitive type or the conversion cannot be accomplished. do anybody know? i set the breakpoint. it is the setvalue line give me this error. but i do not know whats wrong.

    System.Array pixelDataO;
    IPixelBlock3 pBlockO_3 = (IPixelBlock3)pBlockO;
    pixelDataO = (System.Array)pBlockO_3.get_PixelData(0);
   // Console.WriteLine(pixelDataO);
    int maxy = -1;
    Console.WriteLine("going rastermerge");
    //loop through each pixel in the pixelblock and do calculation
    for (int x = 0; x < pBlock1_3.Width; x++)
    {
        for (int y = 0; y < pBlock1_3.Height; y++)
        {
            // check to see if it's a valid data  point
            object oval1 = pBlock1_3.GetVal(0, x, y);
            object oval2 = pBlock2_3.GetVal(0, x, y);
            object oval3 =null;
            if (oval1 != null && oval2 != null)
            {
                //Console.WriteLine(oval1);
                int val1 = Convert.ToByte(oval1);
                //Console.WriteLine("finish");
                // set out of range values to 1
                if (val1 < 1 || val1 > 5) val1 = 1;
                // S5 must be coded as a 4
                if (val1 == 5) val1 = 4;
                int val2 = Convert.ToByte(oval2); ;
                //          int val3 = 10 * slval[val1] + val2;
                int val3 = 10 * val1 + val2;
                if (val3 > 10 && y > maxy) maxy = y;
                oval3 = Convert.ToSByte(val3);
                //Console.WriteLine(oval3);
            }
            Console.WriteLine(oval3);
            pixelDataO.SetValue(oval3, x, y);
        }
    }

Unhandled Exception: System.ArgumentException: Cannot widen from source type to target type either because the source type is a not a primitive type or the conversion cannot be accomplished

This is occurring because you are trying to stuff a type that can't be converted to the target type. Let's take a look at your code...

Take this line: pixelDataO = (System.Array)pBlockO_3.get_PixelData(0);

I asked in the comment's about what type this is, what does pBlockO_3.get_PixelData(0); return? According to the comment's, it's a System.SByte[,] .

Further down in your code, specifically the loop you're in, you have:

 oval3 = Convert.ToByte(val3);

When you do this, you are getting a 8-bit-unsigned integer . Next you are then trying to set a value to the element at the specified position in the two-dimensional pixelDataO array.

 pixelDataO.SetValue(oval3, x, y);

The concern is oval3 isn't a System.SByte it's a 8-bit unsigned integer ; the wrong type and the reason it's can't widen.

To fix the issue you need to use the Convert.ToSByte()

Now this line, oval3 = Convert.ToByte(val3); becomes :

  oval3 = Convert.ToSByte(val3);

On another note, you may need to adjust other code as well, as you could have the issue again depending on what you are doing with other fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM