无法在此 while 循环中设置正确的值 [英] Can't set the correct value in this while loop
问题描述
我已将此代码转换为 VB.NET,这是一个检查图像是否为灰度的函数.
private static unsafe bool IsGrayScale(Image image){使用 (var bmp = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb)){使用 (var g = Graphics.FromImage(bmp)){g.DrawImage(image, 0, 0);}var data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);var pt = (int*)data.Scan0;var res = true;for (var i = 0; i < data.Height * data.Width; i++){var color = Color.FromArgb(pt[i]);if (color.A != 0 && (color.R != color.G || color.G != color.B)){资源=假;休息;}}bmp.UnlockBits(数据);返回资源;}}
C# 代码正在运行,但没有做我需要的,所以我做了一些修改,试图创建一个函数来检查图像是否具有特定的 RGB 颜色,但最重要的部分不起作用:
暗色__1 = Color.FromArgb(pt(i))
异常:
错误 1 表达式不是数组或方法,并且不能有参数列表.C:\Visual Studio Projects\WindowsApplication9\WindowsApplication9\Form1.vb 21 47 WindowsApplication9
这是翻译后的代码:
导入 System.Drawing.Imaging公开课表1私有共享函数 ImageHasColor(ByVal image As Image, ByVal R As Int32, ByVal G As Int32, ByVal B As Int32) As Boolean使用 bmp = New Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb)使用图 = Graphics.FromImage(bmp)graph.DrawImage(image, 0, 0)结束使用Dim data = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.[ReadOnly], bmp.PixelFormat)Dim pt = CType(data.Scan0, Integer)消息框(pt)将 res 设为布尔值昏暗我 = 0当我 <数据.高度 * 数据.宽度暗色__1 = Color.FromArgb(pt(i))如果颜色__1.A <>0 AndAlso color__1.R = R AndAlso color__1.G = G AndAlso color__1.B = B 然后资源 = 真退出时万一System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)结束时间bmp.UnlockBits(数据)返回资源结束使用结束函数Private Sub Form1_Load(sender As Object, e As EventArgs) 处理 MyBase.LoadDim bmp = New Bitmap("C:\Users\Administrador\Desktop\PrtScr capture_3.jpg")MsgBox(ImageHasColor(bmp, 240, 240, 240))结束子结束类
<块引用>
更新:
这些代码只在第一次有效,如果我多次使用它,我会得到这个错误:
尝试读取或写入受保护的内存
1.
私有共享函数 ImageHasColor(ByVal image As Image, ByVal R As Int32, ByVal G As Int32, ByVal B As Int32) As Boolean使用 bmp = New Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb)使用图 = Graphics.FromImage(bmp)graph.DrawImage(image, 0, 0)结束使用暗色__1作为颜色昏暗的字节Dim data = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.[ReadOnly], bmp.PixelFormat)Dim pt = CType(data.Scan0, Integer)将 res 设为布尔值对于 i = 0 到 data.Height * data.Width - 1color__1 = Color.FromArgb(Marshal.ReadInt32(pt, i * 4))如果颜色__1.A <>0 AndAlso color__1.R = R AndAlso color__1.G = G AndAlso color__1.B = B 然后资源 = 真退出万一'System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)下一个bmp.UnlockBits(数据)返回资源结束使用结束函数
2.
私有共享函数 ImageHasColor(ByVal image As Image, ByVal R As Int32, ByVal G As Int32, ByVal B As Int32) As Boolean使用 bmp = New Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb)使用图 = Graphics.FromImage(bmp)graph.DrawImage(image, 0, 0)结束使用暗色__1作为颜色昏暗的字节Dim data = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.[ReadOnly], bmp.PixelFormat)Dim pt = CType(data.Scan0, Integer)将 res 设为布尔值昏暗我 = 0当我 <数据.高度 * 数据.宽度color__1 = Color.FromArgb(Marshal.ReadInt32(pt, i * 4))如果颜色__1.A <>0 AndAlso color__1.R = R AndAlso color__1.G = G AndAlso color__1.B = B 然后资源 = 真退出时万一System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)结束时间bmp.UnlockBits(数据)返回资源结束使用结束函数
<块引用>
更新:
此代码似乎有效
私有共享函数 ImageHasColor(ByVal image As Image, ByVal R As Int32, ByVal G As Int32, ByVal B As Int32) As Boolean尝试使用 bmp = New Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb)使用 graph = Graphics.FromImage(bmp) : graph.DrawImage(image, 0, 0) : End UsingDim Pixel_Color 作为颜色Dim Ditmap_Data = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.[ReadOnly], bmp.PixelFormat)Dim pt As IntPtr = CType(Ditmap_Data.Scan0, IntPtr)对于 i = 0 到 Ditmap_Data.Height * Ditmap_Data.Width - 1Pixel_Color = Color.FromArgb(Marshal.ReadInt32(pt, i * 4))如果 Pixel_Color.A <>0 AndAlso Pixel_Color.R = R AndAlso Pixel_Color.G = G AndAlso Pixel_Color.B = B 然后bmp.UnlockBits(Ditmap_Data)返回真万一下一个bmp.UnlockBits(Ditmap_Data)返回错误结束使用Catch ex 作为例外MsgBox(例如Message)什么都不返回结束尝试结束函数
现在你只是在逆势而上.为什么这些 UnlockBits 在循环中?你在原始代码中看到它们了吗?立即删除它们.
私有共享函数 HasColor(ByVal image As Image, ByVal toFind As Color) As Boolean使用 bmp = New Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb)使用图 = Graphics.FromImage(bmp)graph.DrawImage(image, 0, 0)结束使用Dim data = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.[ReadOnly], bmp.PixelFormat)Dim pt = data.Scan0 '由于我们不能在 VB.Net 中使用指针,我们现在将坚持使用 IntPtrDim res As Boolean = False对于 i=0 到 data.Height * data.Width - 1Dim foundColor as Int32 = Marshal.ReadInt32(pt, i*4)If (foundColor and &HFF000000) <>0 AndAlso ( (foundColor and &H00FFFFFF) = (toFind.ToArgb() and &H00FFFFFF) ) 然后资源 = 真退出万一下一个bmp.UnlockBits(数据)返回资源结束使用结束函数Private Sub Form1_Load(sender As Object, e As EventArgs) 处理 MyBase.LoadDim bmp = New Bitmap("C:\Users\Administrador\Desktop\PrtScr capture_3.jpg")MsgBox(ImageHasColor(bmp, Color.FromArgb(240, 240, 240)))结束子
结束课程
I have translated this code to VB.NET which is a function to check if a image is grayscale.
private static unsafe bool IsGrayScale(Image image)
{
using (var bmp = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb))
{
using (var g = Graphics.FromImage(bmp))
{
g.DrawImage(image, 0, 0);
}
var data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);
var pt = (int*)data.Scan0;
var res = true;
for (var i = 0; i < data.Height * data.Width; i++)
{
var color = Color.FromArgb(pt[i]);
if (color.A != 0 && (color.R != color.G || color.G != color.B))
{
res = false;
break;
}
}
bmp.UnlockBits(data);
return res;
}
}
The C# code is working but is not doing what I need so I did a little modifications trying to make a function which checks if a image has certain RGB color but the most important part is not working:
Dim color__1 = Color.FromArgb(pt(i))
Exception:
Error 1 Expression is not an array or a method, and cannot have an argument list. C:\Visual Studio Projects\WindowsApplication9\WindowsApplication9\Form1.vb 21 47 WindowsApplication9
Here is the translated code:
Imports System.Drawing.Imaging
Public Class Form1
Private Shared Function ImageHasColor(ByVal image As Image, ByVal R As Int32, ByVal G As Int32, ByVal B As Int32) As Boolean
Using bmp = New Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb)
Using graph = Graphics.FromImage(bmp)
graph.DrawImage(image, 0, 0)
End Using
Dim data = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.[ReadOnly], bmp.PixelFormat)
Dim pt = CType(data.Scan0, Integer)
MsgBox(pt)
Dim res As Boolean
Dim i = 0
While i < data.Height * data.Width
Dim color__1 = Color.FromArgb(pt(i))
If color__1.A <> 0 AndAlso color__1.R = R AndAlso color__1.G = G AndAlso color__1.B = B Then
res = True
Exit While
End If
System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
End While
bmp.UnlockBits(data)
Return res
End Using
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim bmp = New Bitmap("C:\Users\Administrador\Desktop\PrtScr capture_3.jpg")
MsgBox(ImageHasColor(bmp, 240, 240, 240))
End Sub
End Class
UPDATE:
Those codes works only for the first time, if I use it more than one time I get this error:
Attempted to read or write protected memory
1.
Private Shared Function ImageHasColor(ByVal image As Image, ByVal R As Int32, ByVal G As Int32, ByVal B As Int32) As Boolean
Using bmp = New Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb)
Using graph = Graphics.FromImage(bmp)
graph.DrawImage(image, 0, 0)
End Using
Dim color__1 As Color
Dim byt
Dim data = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.[ReadOnly], bmp.PixelFormat)
Dim pt = CType(data.Scan0, Integer)
Dim res As Boolean
For i = 0 To data.Height * data.Width - 1
color__1 = Color.FromArgb(Marshal.ReadInt32(pt, i * 4))
If color__1.A <> 0 AndAlso color__1.R = R AndAlso color__1.G = G AndAlso color__1.B = B Then
res = True
Exit For
End If
'System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
Next
bmp.UnlockBits(data)
Return res
End Using
End Function
2.
Private Shared Function ImageHasColor(ByVal image As Image, ByVal R As Int32, ByVal G As Int32, ByVal B As Int32) As Boolean
Using bmp = New Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb)
Using graph = Graphics.FromImage(bmp)
graph.DrawImage(image, 0, 0)
End Using
Dim color__1 As Color
Dim byt
Dim data = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.[ReadOnly], bmp.PixelFormat)
Dim pt = CType(data.Scan0, Integer)
Dim res As Boolean
Dim i = 0
While i < data.Height * data.Width
color__1 = Color.FromArgb(Marshal.ReadInt32(pt, i * 4))
If color__1.A <> 0 AndAlso color__1.R = R AndAlso color__1.G = G AndAlso color__1.B = B Then
res = True
Exit While
End If
System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
End While
bmp.UnlockBits(data)
Return res
End Using
End Function
UPDATE:
This code seems to be working
Private Shared Function ImageHasColor(ByVal image As Image, ByVal R As Int32, ByVal G As Int32, ByVal B As Int32) As Boolean
Try
Using bmp = New Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb)
Using graph = Graphics.FromImage(bmp) : graph.DrawImage(image, 0, 0) : End Using
Dim Pixel_Color As Color
Dim Ditmap_Data = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.[ReadOnly], bmp.PixelFormat)
Dim pt As IntPtr = CType(Ditmap_Data.Scan0, IntPtr)
For i = 0 To Ditmap_Data.Height * Ditmap_Data.Width - 1
Pixel_Color = Color.FromArgb(Marshal.ReadInt32(pt, i * 4))
If Pixel_Color.A <> 0 AndAlso Pixel_Color.R = R AndAlso Pixel_Color.G = G AndAlso Pixel_Color.B = B Then
bmp.UnlockBits(Ditmap_Data)
Return True
End If
Next
bmp.UnlockBits(Ditmap_Data)
Return False
End Using
Catch ex As Exception
MsgBox(ex.Message)
Return Nothing
End Try
End Function
Now you're just being contrarian. Why these UnlockBits in the loop? Do you see them in the original code? Remove them at once.
Private Shared Function HasColor(ByVal image As Image, ByVal toFind As Color) As Boolean
Using bmp = New Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb)
Using graph = Graphics.FromImage(bmp)
graph.DrawImage(image, 0, 0)
End Using
Dim data = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.[ReadOnly], bmp.PixelFormat)
Dim pt = data.Scan0 'Since we can't use a pointer in VB.Net, we'll stick to IntPtr for now
Dim res As Boolean = False
For i=0 To data.Height * data.Width - 1
Dim foundColor as Int32 = Marshal.ReadInt32(pt, i*4)
If (foundColor and &HFF000000) <> 0 AndAlso ( (foundColor and &H00FFFFFF) = (toFind.ToArgb() and &H00FFFFFF) ) Then
res = True
Exit For
End If
Next
bmp.UnlockBits(data)
Return res
End Using
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim bmp = New Bitmap("C:\Users\Administrador\Desktop\PrtScr capture_3.jpg")
MsgBox(ImageHasColor(bmp, Color.FromArgb(240, 240, 240)))
End Sub
End Class
这篇关于无法在此 while 循环中设置正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!