首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用LockBits确定像素颜色(慢)

使用LockBits确定像素颜色(慢)
EN

Stack Overflow用户
提问于 2015-01-31 22:26:53
回答 1查看 1K关注 0票数 1

我试图使用Lockbit来确定图像中每个像素的颜色。尤其是黑人和白人对其他人。我最初是用GetPixel编写这篇文章的,但它太慢了;然而,使用Lockbit也同样慢。它在短短的几秒钟内穿过前200个像素,而不是慢得厉害(大约是一个像素/秒)。是否存在实现错误、监督或更好的解决方案?

代码语言:javascript
复制
    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)
EN

回答 1

Stack Overflow用户

发布于 2016-06-26 01:14:25

调用Application.DoEvents太频繁了,如果需要更新UI线程上的控件,则执行以下操作:

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28257244

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档