我试图使用Lockbit来确定图像中每个像素的颜色。尤其是黑人和白人对其他人。我最初是用GetPixel编写这篇文章的,但它太慢了;然而,使用Lockbit也同样慢。它在短短的几秒钟内穿过前200个像素,而不是慢得厉害(大约是一个像素/秒)。是否存在实现错误、监督或更好的解决方案?
Dim bmp As Bitmap = New Bitmap("path")
Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
Dim bmpData As System.Drawing.Imaging.BitmapData = bmp.LockBits _
(rect, Drawing.Imaging.ImageLockMode.ReadWrite, _
Imaging.PixelFormat.Format24bppRgb)
Dim ptr As IntPtr = bmpData.Scan0
Dim bytes As Integer = Math.Abs(bmpData.Stride) * bmp.Height
Dim rgbValues(bytes - 1) As Byte
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes)
pbStatus.Maximum = bmp.Width * bmp.Height
' Retrieve RGB values
Dim RedValue As Int32
Dim GreenValue As Int32
Dim BlueValue As Int32
Dim l As Integer = 0
Dim progress As Integer = 0
For x = 0 To bmp.Width - 1
For y = 0 To bmp.Height - 1
Application.DoEvents()
l = ((bmp.Width * 3 * y) + (x * 3))
RedValue = rgbValues(l)
GreenValue = rgbValues(l + 1)
BlueValue = rgbValues(l + 2)
If RedValue & GreenValue & BlueValue = "000" Then
txtTX.Text = txtTX.Text + "Black, "
ElseIf RedValue & GreenValue & BlueValue = "255255255" Then
txtTX.Text = txtTX.Text + "White, "
Else
txtTX.Text = txtTX.Text + "Neither, "
End If
progress = progress + 1
lblProgress.text = progress
Next
Next
bmp.UnlockBits(bmpData)发布于 2016-06-26 01:14:25
调用Application.DoEvents太频繁了,如果需要更新UI线程上的控件,则执行以下操作:
Dim bmp As Bitmap = New Bitmap("path")
Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
Dim bmpData As System.Drawing.Imaging.BitmapData = bmp.LockBits _
(rect, Drawing.Imaging.ImageLockMode.ReadWrite, _
Imaging.PixelFormat.Format24bppRgb)
Dim ptr As IntPtr = bmpData.Scan0
Dim bytes As Integer = Math.Abs(bmpData.Stride) * bmp.Height
Dim rgbValues(bytes - 1) As Byte
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes)
pbStatus.Maximum = bmp.Width * bmp.Height
' Retrieve RGB values
Dim RedValue As Int32
Dim GreenValue As Int32
Dim BlueValue As Int32
Dim l As Integer = 0
Dim progress As Integer = 0
DIM BreatheUI as integer = 0
For x = 0 To bmp.Width - 1
For y = 0 To bmp.Height - 1
' Update the UI every 500 cycles
BreatheUI += 1
IF BreatheUI = 500 Then Application.DoEvents(): BreatheUI = 0
l = ((bmp.Width * 3 * y) + (x * 3))
RedValue = rgbValues(l)
GreenValue = rgbValues(l + 1)
BlueValue = rgbValues(l + 2)
If RedValue & GreenValue & BlueValue = "000" Then
txtTX.Text = txtTX.Text + "Black, "
ElseIf RedValue & GreenValue & BlueValue = "255255255" Then
txtTX.Text = txtTX.Text + "White, "
Else
txtTX.Text = txtTX.Text + "Neither, "
End If
progress = progress + 1
lblProgress.text = progress
Next
Next
bmp.UnlockBits(bmpData)https://stackoverflow.com/questions/28257244
复制相似问题