读取图像的像素值

时间:2012-07-27 15:40:08

标签: vb.net image-processing vb.net-2010

Q> 如果显示60 * 66 PNG图像的所有RGB像素值需要10-34秒,那么Image Viewer如何立即显示图像?

        Dim clr As Integer ' or string
        Dim xmax As Integer
        Dim ymax As Integer
        Dim x As Integer
        Dim y As Integer
        Dim bm As New Bitmap(dlgOpen.FileName)

        xmax = bm.Width - 1
        ymax = bm.Height - 1

        For y = 0 To ymax
            For x = 0 To xmax
                With bm.GetPixel(x, y)
                    clr = .R & .G & .B
                    txtValue.AppendText(clr)
                End With
            Next x
        Next y

修改

Dim bmp As New Bitmap(dlgOpen.FileName)
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, bmp.PixelFormat)
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)

For counter As Integer = 0 To rgbValues.Length - 1
       txtValue.AppendText(rgbValues(counter))
Next

System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes)
bmp.UnlockBits(bmpData)

第一个代码需要 10 秒,第二个代码需要 34 秒才能显示AMD A6 3500上59 * 66 PNG图像的文本框中的所有值4 GB RAM!

从文件读取并同时写入文本框时存在问题!

1 个答案:

答案 0 :(得分:1)

问题是,如果您需要访问大量像素,那么您正在使用的功能GetPixel非常慢。尝试使用LockBits。您可以使用它几乎立即收集图像数据。

Using the LockBits method to access image data