• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 85
  • Last Modified:

How to save a image using imagedata (C++ sample to vb.net)

I have a sample application written in C++ for a slap scanner. I am able to get the image scanned but do not know what to do with the data received. Can anybody guide me in the right direction?

I have attached the SaveImage Code from the sample app(Written in C++) and would like to know how convert it to vb.net. I am novice when it comes to C++.

I have tried to read the bytes from memory starting at the pointer returned to the tagImageData structure during the scanning process.

here is the structure that contains the image data:
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> Public Structure tagImageData
        Public Buffer As IntPtr '< Pointer to image buffer
        Public Width As UInteger       '< Image horizontal size
        Public Height As UInteger      '< Image vertical size
        Public ResolutionX As Double  '< Horizontal image resolution (in PPI)
        Public ResolutionY As Double  '< Vertical image resolution (in PPI)
        Public FrameTime As Double    '< Image acquisition time (in seconds)
        '< This value contains the time taken for acquisition
        '< from device (excluding processing time).
        Public Pitch As Integer       '< Image line pitch (in Bytes).
        '  Positive values indicate top down line order,
        '  Negative values mean bottom up line order
        Public BitsPerPixel As Byte   '< Number of Bits per pixel
        Public Format As Integer      '< Image color format
        Public IsFinal As Boolean     '< Marks image as finally processed
        '  A value of "FALSE" disqualifies image for further processing.
        '  (e.g. interim or preprocessed result images)
    End Structure
Werner Pretorius
Werner Pretorius
  • 2
1 Solution
Werner PretoriusAuthor Commented:
I managed to solve the problem myself using the following code:

    Public Sub OnResultImageAvailable(ByVal deviceHandle As IntPtr, ByVal pContext As IntPtr, ByVal imageStatus As Integer, ByVal image As tagImageData, ByVal imageType As Integer, ByVal detectedObjects As Integer)
            '**************read the image buffer from memory
            Dim index As Integer = image.Buffer 'set startIndex from where memory should be read
            Dim b As Byte
            Dim iBytesToRead As Integer = image.Width * image.Height - 1 'calculate the ammount of bytes to be read
            Dim buffer(iBytesToRead) As Byte 'create byte array based on iBytesToRead
            Dim i As Integer = 0
            Do While i < iBytesToRead
                b = Marshal.ReadByte(index)
                buffer.SetValue(b, i)
                index = index + 1
                i = i + 1
            '**************read the image buffer from memory

            '**************convert the byte array to bitmap image
            Dim bmp As New Bitmap(image.Width, image.Height, PixelFormat.Format8bppIndexed)

            'define a grayscale palette
            Dim palette As ColorPalette = bmp.Palette
            Dim j As Integer
            For j = 0 To 255
                palette.Entries(j) = Color.FromArgb(255, j, j, j)
            bmp.Palette = palette

            'copy bytes to bitmap data region
            Dim RECT As New Rectangle(0, 0, image.Width, image.Height)
            Dim bmpData As BitmapData = bmp.LockBits(RECT, ImageLockMode.WriteOnly, bmp.PixelFormat)

            'set the pointer
            Dim ptr As IntPtr = bmpData.Scan0

            Dim numberOfBytes As Integer = bmpData.Stride * bmp.Height

            Marshal.Copy(buffer, 0, ptr, numberOfBytes)
            '**************convert the byte array to bitmap image

            'flip the upside down image

            'display the image
            PictureBox1.Image = bmp

        Catch ex As Exception

        End Try
        MsgBox("Result Image Available")
    End Sub
Werner PretoriusAuthor Commented:
I managed to solve the problem myself. I was reading the memory until i reach a null value at first. I decided to read the memory for (width * height ) number of bytes and that seemed to do the trick
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now