Main Menu

All times are in GMT -8 (DST) :: The time is now 7:34 am.

Login or Register
Create an account and register your LMI products to gain access to useful resources.

Register Account


Sub Menu

Knowledge Base
Article Data
Article Ref
Date Created
Mon, 11th May 2015
Date Modified
Mon, 6th Mar 2017

   How to create images in a HSDatabase without using the AcquisitionDevice in VB.NET, Part 3


How can I create images in a HSDatabase without using the AcquisitionDevice in VB.NET (Part 3)?


 This method is using the AttachImagePointer() function to set the HSImage pixels from a pre-filled buffer.


Dim lBytePointer As IntPtr

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim lBitmap As New System.Drawing.Bitmap("C:/image.jpg")
    FillBuffer(lBitmap, lBytePointer)

    AttachImageBuffer(lBytePointer, lBitmap.Width, lBitmap.Height)

End Sub

Public Function FillBuffer(ByRef bmpImage As Bitmap, ByRef bytePointer As IntPtr) As Boolean

    ' Copy image data from an in memory Bitmap into a byte pointer array.
    ' This buffer can be filled from another process.

    Dim Results As Boolean
    Dim rect As Rectangle
    Dim BMPdata As Imaging.BitmapData
    Dim bytecount As Integer

    rect = New Rectangle(0, 0, bmpImage.Width, bmpImage.Height)
    BMPdata = bmpImage.LockBits(rect, Imaging.ImageLockMode.ReadOnly, Imaging.PixelFormat.Format32bppRgb)

    bytecount = bmpImage.Width * bmpImage.Height * 4
    Results = True


        Dim i As Integer
        Dim bytes(bytecount) As Byte

        bytePointer = Marshal.AllocHGlobal(bytecount)

        For i = 0 To BMPdata.Height - 1

            Marshal.Copy(BMPdata.Scan0.ToInt64() + BMPdata.Width * i * 4, bytes, BMPdata.Width * i * 4, BMPdata.Width * 4)
            Marshal.Copy(bytes, BMPdata.Width * i * 4, bytePointer.ToInt64() + bytecount - BMPdata.Width * (i + 1) * 4, BMPdata.Width * 4)


    Catch ex As Exception

        Results = False

    End Try


    Return Results

End Function

Public Function AttachImageBuffer(ByVal bytePointer As IntPtr, ByVal width As Integer, ByVal height As Integer) As Boolean

    ' Attach a byte pointer array to a hexsight Color image

    Dim Results As Boolean

    Himg = Me.mApplicationControl.Database.Image("Acquire", "Image")

    If (Himg Is Nothing) Then

        Himg = Me.mApplicationControl.Database.AddImage("Acquire", "Image", HSCLASSLIBRARYLib.hsImageType.hsImage32bppRgb)

    End If

    Himg.Width = width
    Himg.Height = height
    Results = True


        Himg.AttachImagePointer(bytePointer, width, height)

    Catch ex As Exception

        Results = False

    End Try

    Return Results

End Function 

Article Comments 

Comment posted,,,,!\,.you.MUST.own.a.copy.of.\,'activate',.the.Knowledgebase.searches.for.'activation',.'activations',.'activated'.and.'activates'',.including.singular,,,’,.appnotes,.and.other.resources,.too!.ARM’s.Privacy.Policy.has.been.updated......