在PictureBox中压印图像时减少延迟

时间:2015-12-15 19:49:11

标签: vb.net latency emboss

我如何减少从Picturebox抓取图像的延迟然后压印图像并返回图片框,因为这是一个相机手柄,需要捕获/浮雕至少16 fps但我变得像每个1 2.5秒

   SwitchImageSave = 1
    Button1.Enabled = False
    StCam.StopTransfer(m_hCamera)
    Dim nReval As Integer
    Dim nLastErrorNo As Integer
    Dim nBufferSize As Integer
    Dim dwWidth As Integer
    Dim dwHeight As Integer
    Dim dwLinePitch As Integer
    nReval = StCam.GetPreviewDataSize(m_hCamera, nBufferSize, dwWidth, dwHeight, dwLinePitch)
    Dim dwPreviewPixelFormat As Integer
    nReval = StCam.GetPreviewPixelFormat(m_hCamera, dwPreviewPixelFormat)
    Dim pixelFormat As Imaging.PixelFormat = Imaging.PixelFormat.Format8bppIndexed
    Select Case dwPreviewPixelFormat
        Case StCam.STCAM_PIXEL_FORMAT_24_BGR
            pixelFormat = Imaging.PixelFormat.Format24bppRgb
        Case StCam.STCAM_PIXEL_FORMAT_32_BGR
            pixelFormat = Imaging.PixelFormat.Format32bppRgb
    End Select
    Dim pbyteImageBuffer(nBufferSize) As Byte
    Dim dwNumberOfByteTrans As Integer = 0
    Dim pdwFrameNo(1) As Integer
    Dim dwMilliseconds As Integer = 100
    Dim gch As System.Runtime.InteropServices.GCHandle = System.Runtime.InteropServices.GCHandle.Alloc(pbyteImageBuffer, System.Runtime.InteropServices.GCHandleType.Pinned)
    Dim ptr As IntPtr = gch.AddrOfPinnedObject()
    nReval = StCam.TakePreviewSnapShot(m_hCamera, ptr, nBufferSize, dwNumberOfByteTrans, pdwFrameNo, dwMilliseconds)
    gch.Free()
    Dim bitmap As Bitmap = New Bitmap(dwWidth, dwHeight, pixelFormat)
    Select Case pixelFormat
        Case Imaging.PixelFormat.Format8bppIndexed
            Dim colorPalette As System.Drawing.Imaging.ColorPalette = bitmap.Palette
            For pixelValue As Integer = 0 To 255
                colorPalette.Entries(pixelValue) = Color.FromArgb(pixelValue, pixelValue, pixelValue)
            Next
            bitmap.Palette = colorPalette
    End Select
    Dim bitmapData As Imaging.BitmapData = bitmap.LockBits(New Rectangle(0, 0, dwWidth, dwHeight), Imaging.ImageLockMode.WriteOnly, pixelFormat)
    Runtime.InteropServices.Marshal.Copy(pbyteImageBuffer, 0, bitmapData.Scan0(), nBufferSize)
    bitmap.UnlockBits(bitmapData)
    Dim bmap As Bitmap

    bmap = New Bitmap(bitmap)
    PictureBox2.Image = bmap
    '  PictureBox2.Image = bmap
    Dim tempbmp As New Bitmap(bmap)
    Dim i, j As Integer
    Dim DispX As Integer = 1, DispY As Integer = 1
    Dim red, green, blue As Integer
    With tempbmp
        For i = 0 To .Height - 2
            For j = 0 To .Width - 2
                Dim pixel1, pixel2 As System.Drawing.Color
                pixel1 = .GetPixel(j, i)
                pixel2 = .GetPixel(j + DispX, i + DispY)
                red = Math.Min(Math.Abs(CInt(pixel1.R) - CInt(pixel2.R)) + 128, 255)
                green = Math.Min(Math.Abs(CInt(pixel1.G) - CInt(pixel2.G)) + 128, 255)
                blue = Math.Min(Math.Abs(CInt(pixel1.B) - CInt(pixel2.B)) + 128, 255)
                bmap.SetPixel(j, i, Color.FromArgb(red, green, blue))
            Next


        Next
    End With

    PictureBox2.Image = bmap
    PictureBox2.Refresh()
    PictureBox2.BringToFront()

1 个答案:

答案 0 :(得分:0)

Dim bitmapData As Imaging.BitmapData = bitmap.LockBits(New Rectangle(0, 0, dwWidth, dwHeight), Imaging.ImageLockMode.WriteOnly, pixelFormat)
    Runtime.InteropServices.Marshal.Copy(pbyteImageBuffer, 0, bitmapData.Scan0(), nBufferSize)
    bitmap.UnlockBits(bitmapData)

    PictureBox2.Image = bitmap

    Dim temp As Bitmap = PictureBox2.Image
     Dim raz As Integer = temp.Height / 4
    Dim height As Integer = temp.Height
    Dim width As Integer = temp.Width
    Dim rect As New Rectangle(Point.Empty, temp.Size)
    Dim bmpData As BitmapData = temp.LockBits(rect, ImageLockMode.[ReadOnly], temp.PixelFormat)
    Dim bpp As Integer = If((temp.PixelFormat = PixelFormat.Format32bppArgb), 4, 3)
    Dim size As Integer = bmpData.Stride * bmpData.Height
    Dim data As Byte() = New Byte(size - 1) {}
    'byte[] newdata = new byte[size];
    System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, data, 0, size)
    'System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, newdata, 0, size);
    Dim options = New ParallelOptions()
    Dim maxCore As Integer = Environment.ProcessorCount - 1
    options.MaxDegreeOfParallelism = If(maxCore > 0, maxCore, 1)
    For y As Integer = 0 To height - 4
        For x As Integer = 0 To width - 4
            If True Then
                Dim index As Integer = y * bmpData.Stride + x * bpp
                'Blue
                data(index) = CByte(Math.Min(Math.Abs(CInt(data(index)) - CInt(data(index + bpp + bmpData.Stride))) + 128, 255))
                'Green
                data(index + 1) = CByte(Math.Min(Math.Abs(CInt(data(index + 1)) - CInt(data(index + bpp + 1 + bmpData.Stride))) + 128, 255))
                'Red
                data(index + 2) = CByte(Math.Min(Math.Abs(CInt(data(index + 2)) - CInt(data(index + bpp + 2 + bmpData.Stride))) + 128, 255))
            End If


        Next


    Next