I still don't understand why this code in c# works but similar doesn't in c++:
original c#:
Code:
int ISampleGrabberCB.BufferCB( double SampleTime, IntPtr pBuffer, int BufferLen )
{
Graphics g;
String s;
float sLeft;
float sTop;
SizeF d;
g = Graphics.FromImage(bitmapOverlay);
g.Clear(System.Drawing.Color.Transparent);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// Prepare to put the specified string on the image
g.DrawRectangle(System.Drawing.Pens.Blue, 0, 0, m_videoWidth - 1, m_videoHeight - 1);
g.DrawRectangle(System.Drawing.Pens.Blue, 1, 1, m_videoWidth - 3, m_videoHeight - 3);
d = g.MeasureString(m_String, fontOverlay);
sLeft = (m_videoWidth - d.Width) / 2;
sTop = (m_videoHeight - d.Height ) / 2;
g.DrawString(m_String, fontOverlay, System.Drawing.Brushes.Red,
sLeft, sTop, System.Drawing.StringFormat.GenericTypographic);
// Add a frame number in the bottom right
s = "Frame " + m_Count.ToString();
d = g.MeasureString(s, transparentFont);
sLeft = (m_videoWidth - d.Width) - 10;
sTop = (m_videoHeight - d.Height ) - 10;
g.DrawString(s, transparentFont, transparentBrush, sLeft, sTop,
System.Drawing.StringFormat.GenericTypographic);
g.Dispose();
// need to flip the bitmap so it's the same orientation as the
// video buffer
bitmapOverlay.RotateFlip(RotateFlipType.RotateNoneFlipY);
// create and copy the video's buffer image to a bitmap
Bitmap v;
v = new Bitmap(m_videoWidth, m_videoHeight, m_stride,
PixelFormat.Format32bppArgb, pBuffer);
g = Graphics.FromImage(v);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// draw the overlay bitmap over the video's bitmap
g.DrawImage(bitmapOverlay, 0, 0, bitmapOverlay.Width, bitmapOverlay.Height);
// dispose of the various objects
g.Dispose();
v.Dispose();
// Increment frame number. Done this way, frame are zero indexed.
m_Count++;
return 0;
}
my C++:
Code:
HRESULT STDMETHODCALLTYPE Camera::BufferCB( double SampleTime, BYTE *pBuffer, long BufferLen )
{
Graphics^ g;
Bitmap^ bitmapOverlay = gcnew Bitmap(640, 480,
PixelFormat::Format24bppRgb);
g = g->FromImage(bitmapOverlay);
g->Clear(System::Drawing::Color::Transparent);
g->SmoothingMode = System::Drawing::Drawing2D::SmoothingMode::AntiAlias;
g->DrawRectangle(System::Drawing::Pens::Blue, 20, 20, 100, 100);
delete g;
bitmapOverlay->RotateFlip(RotateFlipType::RotateNoneFlipY);
Bitmap^ v;
BensBitmaps bensbmp;
// this is buffer form camera should not be different that whatever is returned by BufferCB I guess
test.pSampleGrabber->GetCurrentBuffer(&(test.cbBuffer), (long*)test.pBuffer);
bensbmp.BHmakeBitmap3(test.MediaType,test.pBuffer,NULL);
v = bensbmp.bmpN;
g = g->FromImage(v);
g->SmoothingMode = System::Drawing::Drawing2D::SmoothingMode::AntiAlias;
g->DrawImage(bitmapOverlay, 0 , 0, bitmapOverlay->Width, bitmapOverlay->Height); //<----- access violation
delete g;
delete v;
return 0;
}
Anytime I try to do something with bitmap from buffer I get access violation. Not mentioning that BufferCB in c++ returns BYTE *pBuffer which I found impossible to translate to IntPtr (needed for creation of bitmap) so instead Im using :
Code:
BensBitmaps bensbmp;
test.pSampleGrabber->GetCurrentBuffer(&(test.cbBuffer), (long*)test.pBuffer);
bensbmp.BHmakeBitmap3(test.MediaType,test.pBuffer,NULL);
while using BufferCB only as indicator that buffer is ready.
Am I right that reason why I can't access/change buffer is in my class not being managed
original c#:
internal class Capture : ISampleGrabberCB, IDisposable
my c++:
public class Camera : ISampleGrabberCB
or am I just doing something wrong?