Solved

Converting/marshalling '1 dimensional array of Byte' to 'System.Intptr'

Posted on 2010-11-17
2
1,049 Views
Last Modified: 2012-05-10

Hello, I am completely novice at marshalling, managed/unmanaged concerns, etc.  I have a camera SDK that is giving me data in a format

 Public BitmapData() As Byte

And I want to plug it into my Bitmap constructor of

Public Sub New ( _
    width As Integer, _
    height As Integer, _
    stride As Integer, _
    format As PixelFormat, _
    scan0 As IntPtr _
)

I'm sure I have the correct width, height, format, etc, but can;t seem to plug my BitmapData into the IntPtr.  I get an error.

"Value of type '1-dimensional array of Byte' cannot be converted to 'System.IntPtr'."

Does anyone know how to fit this round peg into the square hole?

In C# they do it like this:

            fixed (byte* ptr = pBmp)
            {
                image = new Bitmap(frame.Width / frame.SubSampleX, frame.Height / frame.SubSampleY, 3 * frame.Width / frame.SubSampleX, PixelFormat.Format24bppRgb,  (IntPtr)ptr);
            //    image.RotateFlip(RotateFlipType.RotateNoneFlipY);
            }

Open in new window


But I don't think I can do that in VB.NET.    Any thoughts?  Thanks.
0
Comment
Question by:riceman0
[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 Comments
 
LVL 29

Assisted Solution

by:nffvrxqgrcfqvvc
nffvrxqgrcfqvvc earned 200 total points
ID: 34158846
Using the Marshal Class allocate a IntPtr buffer the same size as your byte array using Marshal.AllocHGlobal() then use Marshal.Copy() to copy the byte array into your allocated pointer. Pass the pointer then release it when your finished.

Here is an example using a blank byte array thats 64 bytes.
Dim bBitmapData(64 - 1) As Byte
Dim pBitmapData As IntPtr = Marshal.AllocHGlobal(bBitmapData.Length)
Marshal.Copy(bBitmapData, 0, pBitmapData, bBitmapData.Length)
'TODO: Pass pBitmapData
Marshal.FreeHGlobal(pBitmapData)

Open in new window

0
 
LVL 32

Accepted Solution

by:
Erick37 earned 300 total points
ID: 34159290
You can give this sample a try.  Be sure the ImageBytes array contains the bitmap bytes.
Option Explicit On
Option Strict On

Imports System.Runtime.InteropServices
Imports System.Drawing.Imaging

Public Class Form1

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

		Dim bmd As BitmapData

		'' Your picture data
		Dim ImageBytes() As Byte ''Must be populated

		'' Create a blank bitmap of the same size and format of your target
		Dim bm As Bitmap = New Bitmap(frame.Width, frame.Height, PixelFormat.Format24bppRgb)

		'' Lock the bitmap data.
		Dim rc As Rectangle = New Rectangle(0, 0, bm.Width, bm.Height)
		bmd = bm.LockBits(rc, Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format24bppRgb)

		'' Calculate the size of the data
		Dim bmsize As Integer = bmd.Stride * bmd.Height

		'' Copy the data into the bitmap array.
		Marshal.Copy(ImageBytes, 0, bmd.Scan0, bmsize)

		bm.UnlockBits(bmd)

		''The new bitmap is bm

	End Sub

End Class

Open in new window

0

Featured Post

Technology Partners: 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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

691 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