我有一张宽 888px 高 592px 的图片,宽高比为 3:2。
以下产生错误值 1,因为 BitmapDecoder.PixelWidth 和 BitmapDecoder.PixelHeight 都是 uint
(无符号整数)和 decoder
的整数计算/截断下面是一个 BitmapDecoder 对象。
double aspectRatio = decoder.PixelWidth/decoder.PixelHeight;
下面给出了预期的正确值 1.5,但 Visual Studio 说“Cast is redundant”,但这是为什么呢?
double aspectRatio = (double)decoder.PixelWidth/(double)decoder.PixelHeight;
最佳答案
您只需将 一个 uint 转换为 double 即可强制执行浮点运算,因此:
double aspectRatio = decoder.PixelWidth / (double)decoder.PixelHeight;
或:
double aspectRatio = (double)decoder.PixelWidth / decoder.PixelHeight;
就个人而言,我会选择后者,但这是一个见仁见智的问题。
关于c# - Visual Studio 2015 表示 'cast is redundant' 。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34040683/