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
StackTrace: d:\c-sharp\c++ compiling\consoleapplication7\ffmpeg_wrapper\ffmpegwrapper.h: の MyVideo.FFMPEGWrapper.Push(Bitmap b) で 93 行目 d:\ の ScreenVideoRecorder.Form1.button1_Click(オブジェクト送信者、EventArgs e) でC-Sharp\ScreenVideoRecorder\ScreenVideoRecorder\ScreenVideoRecorder\Form1.cs:System.Windows.Forms.Button.OnClick(EventArgs e) の System.Windows.Forms.Control.OnClick(EventArgs e) の 64 行目 System.Windows.Forms .Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase .WndProc(Message& m) System.Windows.Forms.Button.WndProc(Message& m) で System.Windows.Forms.System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) の Control.ControlNativeWindow.OnMessage(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 理由、ApplicationContext コンテキスト) で System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 理由、ApplicationContext コンテキスト) System.Windows.Forms.Application で。Run(Form mainForm) at ScreenVideoRecorder.Program.Main() in d:\C-Sharp\ScreenVideoRecorder\ScreenVideoRecorder\ScreenVideoRecorder\Program.cs:line 19 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System .AppDomain.ExecuteAssembly (String assemblyFile, Evidence assemblySecurity, String[] args) で Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() System.Threading.ThreadHelper.ThreadStart_Context(オブジェクト状態) で System.Threading.ExecutionContext.RunInternal(ExecutionContext) System.Threading.Run での executionContext、ContextCallback コールバック、オブジェクトの状態、ブール値の preserveSyncCtx) (ExecutionContext executionContext、ContextCallback コールバック、オブジェクトの状態、ブール値の preserveSyncCtx) を System.Threading で実行します。System.Threading.ThreadHelper.ThreadStart() InnerException での ExecutionContext.Run (ExecutionContext executionContext、ContextCallback コールバック、オブジェクト状態):
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 );