算术运算导致溢出..以下高亮代码中的错误

帕特尔
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Compare(PictureBox1.Image, PictureBox2.Image)
End Sub

Public Function Compare(ByVal img1_ As Image, ByVal img2_ As Image) As Double
    Dim pixelNb As Integer = img1_.Width * img1_.Height
    Dim percent As Double = 100
    Dim resized_img2_ As Bitmap = ResizeBitmap(CType(img2_, Bitmap), img1_.Width, img1_.Height)
    For i As Integer = 0 To img1_.Width - 1
        For j As Integer = 0 To img1_.Height - 1
            percent -= ColorCompare((CType(img1_, Bitmap)).GetPixel(i, j), (CType(resized_img2_, Bitmap)).GetPixel(i, j)) / pixelNb
        Next
    Next

    Return percent
End Function

Public Function ResizeBitmap(ByVal b As Bitmap, ByVal nWidth As Integer, ByVal nHeight As Integer) As Bitmap
    Dim result As Bitmap = New Bitmap(nWidth, nHeight)
    Using g As Graphics = Graphics.FromImage(CType(result, Image))
        g.DrawImage(b, 0, 0, nWidth, nHeight)
    End Using

    Return result
End Function

Public Function ColorCompare(ByVal c1 As Color, ByVal c2 As Color) As Double
    Return Double.Parse((Math.Abs(c1.B - c2.B) + Math.Abs(c1.R - c2.R) + Math.Abs(c1.G - c2.G)).ToString()) * 100 / (3 * 255)
End Function

在这一行中给出错误

Return Double.Parse((Math.Abs(c1.B - c2.B) + Math.Abs(c1.R - c2.R) +
           Math.Abs(c1.G - c2.G)).ToString()) * 100 / (3 * 255)
玛丽

c1.B 返回一个字节。字节的值为 0-255 并且是无符号的,因此它不能保存负值。

Dim a As Byte = 200
Dim b As Byte = 201
Dim x  = a - b

导致相同的溢出错误。更改为整数可避免该错误。演员似乎没有必要,因为它正在扩大(无数据丢失)。

    Dim a As Byte = 200
        Dim b As Byte = 201
        Dim c As Integer = a
        Dim d As Integer = b
        Dim x = c - d 

将颜色字节更改为整数,然后重试。

Dim g, h, i, j, k, l As Integer
g = c1.B
h = c2.B
i = c1.R
j = c1.R
k = c1.G
l = c1.G
Dim result As Double = CDbl(Math.Abs(g - h) + Math.Abs(i - j) + Math.Abs(k - l)) * 100 / (3 * 255)
    Return result

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用常量进行算术运算时出现溢出错误

来自分类Dev

Thymeleaf中的算术运算

来自分类Dev

算术溢出错误转换SQL中的浮点错误

来自分类Dev

编码测试中的算术溢出

来自分类Dev

在YAML中执行算术运算?

来自分类Dev

算术溢出错误-802

来自分类Dev

除了覆盖算术运算符外,还有其他方法可以在Java中实现“溢出安全”算术运算吗?

来自分类Dev

模板中的算术运算

来自分类Dev

余烬车把中的算术运算

来自分类Dev

System.IO.File.Delete引发算术运算导致溢出

来自分类Dev

Lisp中的暂停算术运算

来自分类Dev

'<Module>'的类型初始值设定项引发了异常,算术运算导致溢出

来自分类Dev

算术运算即使在未检查的情况下也导致溢出

来自分类Dev

算术运算导致int溢出

来自分类Dev

使用常量进行算术运算时出现溢出错误

来自分类Dev

合金中的算术运算

来自分类Dev

编码测试中的算术溢出

来自分类Dev

一元+/-运算符如何可能导致“ -a”或“ + a”中的整数提升,而“ a”是算术数据类型的常量/变量?

来自分类Dev

Lisp中的暂停算术运算

来自分类Dev

Makefile规则中的算术运算

来自分类Dev

算术运算导致C#溢出

来自分类Dev

C ++中的算术运算

来自分类Dev

c中复数的算术运算

来自分类Dev

算术运算导致 Color.FromArgb 溢出

来自分类Dev

primaryjoin 和_ 中的两个算术运算导致失败

来自分类Dev

我的代码中是什么导致了以下错误:PHP 致命错误:未捕获的错误:在 null 上调用成员函数 begin()?

来自分类Dev

如何修复错误“算术运算导致溢出”

来自分类Dev

Shell 脚本中的算术运算

来自分类Dev

如何解决“算术运算导致溢出”。对于具有长值的枚举

Related 相关文章

热门标签

归档