我在csharp中有这段代码:
f.Push(Properties.Resources.magnifierGlass);
然后我在C ++中使用Push函数:
void Push( Bitmap ^b )
{
auto bmpData = b->LockBits(
System::Drawing::Rectangle(0,0,b->Width,b->Height),
ImageLockMode::ReadWrite,
PixelFormat::Format24bppRgb);
char* top = (char*)bmpData->Scan0.ToPointer();
if (bmpData->Stride < 0) {
top += bmpData->Stride * (1 - b->Height);
}
for( int y = 0; y < b->Height; ++y ) {
RGBTRIPLE* row = (RGBTRIPLE*)(top + y * bmpData->Stride);
for( int x = 0; x < b->Width; ++x ) {
row[x].rgbtRed = 255;
}
b->UnlockBits( bmpData );
例外是在线:
row[x].rgbtRed = 255;
尝试读取或写入受保护的内存。这通常表明其他内存已损坏。
System.AccessViolationException未处理HResult = -2147467261
Message =尝试读取或写入受保护的内存。这通常是一个 指示其他内存已损坏。来源= FFMPEG_WRAPPER
堆栈跟踪: 在MyVideo.FFMPEGWrapper.Push(位图b)的d:\ c-sharp \ c ++编译\ consoleapplication7 \ ffmpeg_wrapper \ ffmpegwrapper.h:第93行 在ScreenVideoRecorder.Form1.button1_Click(Object sender,EventArgs e)中 d:\ C-夏普\ ScreenVideoRecorder \ ScreenVideoRecorder \ ScreenVideoRecorder \ Form1.cs中:线 64 在System.Windows.Forms.Control.OnClick(EventArgs e) 在System.Windows.Forms.Button.OnClick(EventArgs e) 在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 在System.Windows.Forms.Control.WmMouseUp(消息&amp; m,MouseButtons按钮,Int32点击) 在System.Windows.Forms.Control.WndProc(消息&amp; m) 在System.Windows.Forms.ButtonBase.WndProc(消息&amp; m) 在System.Windows.Forms.Button.WndProc(消息&amp; m) 在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m) 在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m) 在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) 在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg) 在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr) dwComponentID,Int32原因,Int32 pvLoopData) 在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32) 原因,ApplicationContext上下文) 在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32) 原因,ApplicationContext上下文) 在System.Windows.Forms.Application.Run(Form mainForm) 在ScreenVideoRecorder.Program.Main()中的d:\ C-Sharp \ ScreenVideoRecorder \ ScreenVideoRecorder \ ScreenVideoRecorder \ Program.cs:line 19 在System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String [] args) 在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args) 在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback回调,对象状态,布尔值 preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx) 在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态) 在System.Threading.ThreadHelper.ThreadStart()InnerException:
Properties.Resources.magnifierGlass magnifierGlass是Bitmap类型。
这种例外可能是什么原因?
修改
此代码正在运行它不会抛出任何异常。 我希望这是一个很好的例子。
// Lock the bitmap's bits.
System::Drawing::Rectangle rect = System::Drawing::Rectangle(0,0,bmp->Width,bmp->Height);
System::Drawing::Imaging::BitmapData^ bmpData = bmp->LockBits( rect, System::Drawing::Imaging::ImageLockMode::ReadWrite, bmp->PixelFormat );
// Get the address of the first line.
IntPtr ptr = bmpData->Scan0;
// Declare an array to hold the bytes of the bitmap.
// This code is specific to a bitmap with 24 bits per pixels.
int bytes = Math::Abs(bmpData->Stride) * bmp->Height;
array<Byte>^rgbValues = gcnew array<Byte>(bytes);
// Copy the RGB values into the array.
System::Runtime::InteropServices::Marshal::Copy( ptr, rgbValues, 0, bytes );
// Set every third value to 255.
for ( int counter = 2; counter < rgbValues->Length; counter += 3 )
rgbValues[ counter ] = 255;
// Copy the RGB values back to the bitmap
System::Runtime::InteropServices::Marshal::Copy( rgbValues, 0, ptr, bytes );
// Unlock the bits.
bmp->UnlockBits( bmpData );