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)

4 GB RAM を搭載した AMD A6 3500 で 59*66 PNG 画像のテキスト ボックスにすべての値を表示するのに、最初のコードは10秒かかり、2 番目のコードは約34秒かかります。

ファイルからの読み取りとテキストボックスへの書き込みが同時に行われると、問題が発生します。

4

1 に答える 1

1

問題は、多くのピクセルにアクセスする必要がある場合、使用している機能であるGetPixelが非常に遅いことです。LockBitsを使用してみてください。これを使用して、ほぼ瞬時に画像データを収集できます。

LockBitsメソッドを使用して画像データにアクセスします

于 2012-07-27T15:59:27.090 に答える