Hi developers,
I downloaded the Canon SDK Version 7.1 and tried to migrate the
"RELCTRL" sample to C# (with PInvoke). Almost everything is working
except the following callback function that should receive the image
data from the camera. I´m always getting the same error:
An unhandled exception of type 'System.NullRef erenceException '
occurred in Unknown Module.
Additional information: Object reference not set to an instance of an
object.
This error shows up after the callback functions has been called 6
times. Unfortunatley I don´t know why this happens and how I can solve
the problem.
=============== =============== =============== =============== =============== ====
/* The function which receives the picture from a camera */
UInt32 ViewFinderCallB ackFun(UInt32 pBuf, UInt32 Size, UInt32 Format,
UInt32 Context)
{
UInt32 err = cdError.cdOK;
if ( Format == cdType.FILEFORM AT_BMP )
{
IntPtr pBuffer = new IntPtr(pBuf);
// Get the BITMAPHEADER from pBuf
BITMAPHEADER frameHeader = new BITMAPHEADER();
frameHeader = (BITMAPHEADER)M arshal.PtrToStr ucture(pBuffer,
frameHeader.Get Type());
// Create byte array (BITMAPHEADER + image data) and copy
byte[] VideoData = new byte[frameHeader.bmf Header.bfSize];
Marshal.Copy(pB uffer, VideoData, 0,
(int)frameHeade r.bmfHeader.bfS ize);
// Get image size
int width = frameHeader.bmi Header.biWidth;
int height = frameHeader.bmi Header.biHeight ;
// Create new Bitmap
Bitmap frame = new Bitmap(width, height);
BitmapData bmpData = frame.LockBits(
new Rectangle(0, 0, width, height),
ImageLockMode.R eadWrite,
PixelFormat.For mat24bppRgb);
// Copy image data from memory to Scan0 of our Bitmap
// Start at index 54 because of BITMAPHEADER
for (int i = 54; i < VideoData.Lengt h; i++)
{
Marshal.WriteBy te(bmpData.Scan 0, i-54, VideoData);
}
frame.UnlockBit s(bmpData);
// Create delegate
if (this.RecievedF rame != null)
{
this.RecievedFr ame(this, new StreamEventArgs (frame));
}
}
return err;
}
[StructLayout(La youtKind.Sequen tial, Pack=1)]
public struct BITMAPINFOHEADE R
{
public UInt32 biSize;
public Int32 biWidth;
public Int32 biHeight;
public Int16 biPlanes;
public Int16 biBitCount;
public UInt32 biCompression;
public UInt32 biSizeImage;
public Int32 biXPelsPerMeter ;
public Int32 biYPelsPerMeter ;
public UInt32 biClrUsed;
public UInt32 biClrImportant;
}
[StructLayout(La youtKind.Sequen tial, Pack=1)]
public struct BITMAPFILEHEADE R // 14 bytes
{
public Int16 bfType; //"magic cookie" - must be "BM"
public Int32 bfSize;
public Int16 bfReserved1;
public Int16 bfReserved2;
public Int32 bfOffBits;
}
[StructLayout(La youtKind.Sequen tial)]
public struct BITMAPHEADER
{
[MarshalAs(Unman agedType.Struct , SizeConst=14)]
public BITMAPFILEHEADE R bmfHeader;
[MarshalAs(Unman agedType.Struct , SizeConst=40)]
public BITMAPINFOHEADE R bmiHeader;
}
=============== =============== =============== =============== =============== ====
If anybody can help me ... please.
Regards
Michael
I downloaded the Canon SDK Version 7.1 and tried to migrate the
"RELCTRL" sample to C# (with PInvoke). Almost everything is working
except the following callback function that should receive the image
data from the camera. I´m always getting the same error:
An unhandled exception of type 'System.NullRef erenceException '
occurred in Unknown Module.
Additional information: Object reference not set to an instance of an
object.
This error shows up after the callback functions has been called 6
times. Unfortunatley I don´t know why this happens and how I can solve
the problem.
=============== =============== =============== =============== =============== ====
/* The function which receives the picture from a camera */
UInt32 ViewFinderCallB ackFun(UInt32 pBuf, UInt32 Size, UInt32 Format,
UInt32 Context)
{
UInt32 err = cdError.cdOK;
if ( Format == cdType.FILEFORM AT_BMP )
{
IntPtr pBuffer = new IntPtr(pBuf);
// Get the BITMAPHEADER from pBuf
BITMAPHEADER frameHeader = new BITMAPHEADER();
frameHeader = (BITMAPHEADER)M arshal.PtrToStr ucture(pBuffer,
frameHeader.Get Type());
// Create byte array (BITMAPHEADER + image data) and copy
byte[] VideoData = new byte[frameHeader.bmf Header.bfSize];
Marshal.Copy(pB uffer, VideoData, 0,
(int)frameHeade r.bmfHeader.bfS ize);
// Get image size
int width = frameHeader.bmi Header.biWidth;
int height = frameHeader.bmi Header.biHeight ;
// Create new Bitmap
Bitmap frame = new Bitmap(width, height);
BitmapData bmpData = frame.LockBits(
new Rectangle(0, 0, width, height),
ImageLockMode.R eadWrite,
PixelFormat.For mat24bppRgb);
// Copy image data from memory to Scan0 of our Bitmap
// Start at index 54 because of BITMAPHEADER
for (int i = 54; i < VideoData.Lengt h; i++)
{
Marshal.WriteBy te(bmpData.Scan 0, i-54, VideoData);
}
frame.UnlockBit s(bmpData);
// Create delegate
if (this.RecievedF rame != null)
{
this.RecievedFr ame(this, new StreamEventArgs (frame));
}
}
return err;
}
[StructLayout(La youtKind.Sequen tial, Pack=1)]
public struct BITMAPINFOHEADE R
{
public UInt32 biSize;
public Int32 biWidth;
public Int32 biHeight;
public Int16 biPlanes;
public Int16 biBitCount;
public UInt32 biCompression;
public UInt32 biSizeImage;
public Int32 biXPelsPerMeter ;
public Int32 biYPelsPerMeter ;
public UInt32 biClrUsed;
public UInt32 biClrImportant;
}
[StructLayout(La youtKind.Sequen tial, Pack=1)]
public struct BITMAPFILEHEADE R // 14 bytes
{
public Int16 bfType; //"magic cookie" - must be "BM"
public Int32 bfSize;
public Int16 bfReserved1;
public Int16 bfReserved2;
public Int32 bfOffBits;
}
[StructLayout(La youtKind.Sequen tial)]
public struct BITMAPHEADER
{
[MarshalAs(Unman agedType.Struct , SizeConst=14)]
public BITMAPFILEHEADE R bmfHeader;
[MarshalAs(Unman agedType.Struct , SizeConst=40)]
public BITMAPINFOHEADE R bmiHeader;
}
=============== =============== =============== =============== =============== ====
If anybody can help me ... please.
Regards
Michael
Comment