?
Solved

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

Posted on 2017-09-05
2
Low Priority
?
40 Views
Last Modified: 2017-09-05
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
SaveImage.cpp
SaveImage.h
0
Comment
Question by:Werner Pretorius
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 

Accepted Solution

by:
Werner Pretorius earned 0 total points
ID: 42282220
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)
        Try
            '**************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
            Loop
            '**************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)
            Next
            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)
            bmp.UnlockBits(bmpData)
            '**************convert the byte array to bitmap image


            'flip the upside down image
            bmp.RotateFlip(RotateFlipType.RotateNoneFlipX)

            'display the image
            PictureBox1.Image = bmp

        Catch ex As Exception

        End Try
        MsgBox("Result Image Available")
    End Sub
0
 

Author Closing Comment

by:Werner Pretorius
ID: 42282227
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question