我在 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
消息=尝试读取或写入受保护的内存。这通常表明其他内存已损坏。源=FFMPEG_WRAPPER
StackTrace:在 MyVideo.FFMPEGWrapper.Push(Bitmap b) in d:\c-sharp\c++ compile\consoleapplication7\ffmpeg_wrapper\ffmpegwrapper.h:第 93 行 ScreenVideoRecorder.Form1.button1_Click(Object sender, EventArgs e) in d:\ C-Sharp\ScreenVideoRecorder\ScreenVideoRecorder\ScreenVideoRecorder\Form1.cs:System.Windows.Forms.Control.OnClick(EventArgs e) 处 System.Windows.Forms.Button.OnClick(EventArgs e) 处 System.Windows.Forms 处的第 64 行.Button.OnMouseUp(MouseEventArgs 事件) 在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ButtonBase .WndProc(Message& m) 在 System.Windows.Forms.Button.WndProc(Message& m) 在 System.Windows.Forms。Control.ControlNativeWindow.OnMessage(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 在系统.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 在 System.Windows.Forms .Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 在 System.Windows.Forms.Application。在 d:\C-Sharp\ScreenVideoRecorder\ScreenVideoRecorder\ScreenVideoRecorder\Program.cs:System.AppDomain._nExecuteAssembly(RuntimeAssembly 程序集,String[] args) 的第 19 行中的 ScreenVideoRecorder.Program.Main() 处运行(窗体 mainForm) .AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(Object state) 在 System.Threading.ExecutionContext.RunInternal(ExecutionContext在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 在 System.Threading 的 executionContext、ContextCallback 回调、对象状态、布尔值 preserveSyncCtx)。ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at 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 );