简体   繁体   中英

“Cannot implicitly convert type 'double' to 'byte'”

I am trying to increase the red value of an image by fifty percent. Here is my code:

public static Bitmap IncreaseRedFiftyPercent(Bitmap b)
    {
        Bitmap temp = (Bitmap) b;
        Bitmap bmap = (Bitmap)temp.Clone();
        Color c;
        for (int i = 0; i < bmap.Width; i++)
        {
            for (int j = 0; j < bmap.Height; j++)
            {
                c = bmap.GetPixel(i, j);
                byte increase = c.R + c.R * 0.5;  //This line gives error

                bmap.SetPixel(i, j, Color.FromArgb(increase, c.G, c.B));
            }
        }
        b = (Bitmap)bmap.Clone();
        return b;
    }

Here is what i do: I read all pixels of the picture, and increase the red value by fifty percent and keep blue and green the same. But the line

byte increase = c.R + c.R * 0.5;  //This line gives error

gives me an error saying that

Cannot implicitly convert type 'double' to 'byte'. An explicit conversion exists (are you missing     
a cast?)    

And i cannot convert double to byte? It looks like sensible what i am doing, what is wrong here?

Thanks

Arithmetic in C# is performed by first determining which operator to use by choosing from a list of possible operators:

int * int --> int
long * long --> long
double * double --> double

and so on; that list is quite long.

In your case the best operator is double * double --> double , and so the byte is converted to double. This is lossless . But the result is a double; it might have a fractional part, and its magnitude might be larger than the largest possible byte. Converting back to byte is lossy . Therefore you are required to say "I guarantee that I really want to make this lossy conversion" by inserting a cast to byte.

Now, before you do so you should make sure that you are in fact doing the right thing! If the byte is already at, say, 200, then increasing it by 50% to the double 300.00 and then converting that back to a byte that can only be between 0 and 255 is likely to produce unexpected results. Think carefully before you insert that cast.

You could use this instead, although it won't account for overflow (any result over 255 will roll over to 0):

byte increase = (byte)(c.R + c.R / 2);

Note that I use /2 instead of *0.5 to use integer math instead of floating-point math. If you're processing lots of large images the performance difference could be significant.

Based on your requirement something like this may work:

byte increase = (byte)(Math.Min(c.R + c.R / 2 , 255));

The problem is that when you take a byte (eg cR ) and multiply by a double (eg 0.5 ) then the result is a double (because a byte may not have the precision to hold the result). You are then trying to assign this to a byte and as the error message says no implicit conversion exists so you have to explicitly convert it.

This can be done with something like

byte increase = (byte)(c.R + c.R * 0.5);

And I've been reminded by another answer that this won't check for overflow so if cR is more than 170 then you will get overflow issues. To this end you will want to do something like D Stanley's Min technique.

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