2012-07-27 76 views
1

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) 

第一代码采用秒:第二个周围秒表示用于对AMD A6一个59 * 66 PNG图像中的文本框的所有值3500和4 GB RAM!

从文件读取并写入文本框发生在相同的时间存在的问题存在!

+0

图像有多大? – 2012-07-27 15:43:14

+0

对于600 * 600的图像,AMD A6需要大约一分钟或更多的时间! – Sourav 2012-07-27 15:43:49

回答

1

问题是,如果您需要访问很多像素,则使用的功能GetPixel的速度会非常慢。尝试使用LockBits。您可以使用它来几乎立即收集图像数据。

Using the LockBits method to access image data

+0

@sourav做了这个帮助吗? – 2012-07-27 17:57:49

+0

我添加了一些代码,但它比原来的代码更慢,请查看编辑! – Sourav 2012-07-28 07:41:34