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.NullReferenceException'
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 ViewFinderCallBackFun(UInt32 pBuf, UInt32 Size, UInt32 Format,
UInt32 Context)
{
UInt32 err = cdError.cdOK;
if ( Format == cdType.FILEFORMAT_BMP )
{
IntPtr pBuffer = new IntPtr(pBuf);
// Get the BITMAPHEADER from pBuf
BITMAPHEADER frameHeader = new BITMAPHEADER();
frameHeader = (BITMAPHEADER)Marshal.PtrToStructure(pBuffer,
frameHeader.GetType());
// Create byte array (BITMAPHEADER + image data) and copy
byte[] VideoData = new byte[frameHeader.bmfHeader.bfSize];
Marshal.Copy(pBuffer, VideoData, 0,
(int)frameHeader.bmfHeader.bfSize);
// Get image size
int width = frameHeader.bmiHeader.biWidth;
int height = frameHeader.bmiHeader.biHeight;
// Create new Bitmap
Bitmap frame = new Bitmap(width, height);
BitmapData bmpData = frame.LockBits(
new Rectangle(0, 0, width, height),
ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);
// Copy image data from memory to Scan0 of our Bitmap
// Start at index 54 because of BITMAPHEADER
for (int i = 54; i < VideoData.Length; i++)
{
Marshal.WriteByte(bmpData.Scan0, i-54, VideoData);
}
frame.UnlockBits(bmpData);
// Create delegate
if (this.RecievedFrame != null)
{
this.RecievedFrame(this, new StreamEventArgs(frame));
}
}
return err;
}
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct BITMAPINFOHEADER
{
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(LayoutKind.Sequential, Pack=1)]
public struct BITMAPFILEHEADER // 14 bytes
{
public Int16 bfType; //"magic cookie" - must be "BM"
public Int32 bfSize;
public Int16 bfReserved1;
public Int16 bfReserved2;
public Int32 bfOffBits;
}
[StructLayout(LayoutKind.Sequential)]
public struct BITMAPHEADER
{
[MarshalAs(UnmanagedType.Struct, SizeConst=14)]
public BITMAPFILEHEADER bmfHeader;
[MarshalAs(UnmanagedType.Struct, SizeConst=40)]
public BITMAPINFOHEADER bmiHeader;
}
================================================== =============================
If anybody can help me ... please.
Regards
Michael