Question
How can I create images in a HSDatabase without using the AcquisitionDevice in VB.NET (Part 3)?
Answer
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 Try Dim i As Integer Dim bytes(bytecount) As Byte Marshal.FreeHGlobal(bytePointer) 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) Next Catch ex As Exception Results = False End Try bmpImage.UnlockBits(BMPdata) 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 Dim Himg As HSCLASSLIBRARYLib.HSImage Himg = Me.mApplicationControl.Database.Image("Acquire", "Image") If (Himg Is Nothing) Then Me.mApplicationControl.Database.AddView("Acquire") Himg = Me.mApplicationControl.Database.AddImage("Acquire", "Image", HSCLASSLIBRARYLib.hsImageType.hsImage32bppRgb) End If Himg.Width = width Himg.Height = height Results = True Try Himg.AttachImagePointer(bytePointer, width, height) Catch ex As Exception Results = False End Try Return Results End Function
Comments
0 comments
Please sign in to leave a comment.