将位图保存到文件会出错 [英] Saving bitmap to file gives error

查看:206
本文介绍了将位图保存到文件会出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


这是对我的
最新主题
,已经回答了。


我想将Mat图像转换为Bitmap,然后将其发送到我的C#GUI,以在Windows Form Application上显示该图像。


现在,以下代码给出了我的错误:


System.AccessViolationException未处理

  HResult = -2147467261

 消息= Poging tot het lezen of schrijven van beveiligd geheugen。 Dit duidt er vaak op dat ander geheugen is beschadigd。

 来源= System.Drawing

  StackTrace:

       bij System.Drawing.SafeNativeMethods.Gdip.GdipDrawImageRectI(HandleRef图形,HandleRef图像,Int32 x,Int32 y,Int32宽度,Int32高度)

       bij System.Drawing.Graphics.DrawImage(图片图片,Int32 x,Int32 y,Int32宽度,Int32高度)

       bij System.Drawing.Graphics.DrawImage(图片图片,矩形矩形)

       bij System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)

       bij System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e,Int16 layer)

       bij System.Windows.Forms.Control.WmPaint(Message& m)

       bij System.Windows.Forms.Control.WndProc(Message& m)

       bij System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)

  InnerException:



它指向

应用程序.Run(新Form1()); 




所以我尝试了不同的方法。




尝试加载Mat。使用以下代码行将Mat转换为Bitmap:

 Bitmap ^ test = gcnew Bitmap(matToConvert.rows,matToConvert.cols, 4 * matToConvert.rows,System :: Drawing :: Imaging :: PixelFormat :: Format24bppRgb,IntPtr(matToConvert.data)); 

然后在此行之后立即保存一个文件为.bmp。但是当我想运行代码时,它会给我同样的错误:


System.AccessViolationException未处理的是$
  HResult = -2147467261

 消息= Poging tot het lezen of schrijven van beveiligd geheugen。 Dit duidt er vaak op dat ander geheugen is beschadigd。

 来源= System.Drawing

  StackTrace:

       bij System.Drawing.SafeNativeMethods.Gdip.GdipSaveImageToFile(HandleRef image,String filename,Guid& classId,HandleRef encoderParams)

       bij System.Drawing.Image.Save(String filename,ImageCodecInfo encoder,EncoderParameters encoderParams)

       bij OpencvDotNet.OpenCvWrapper.ApplyFilter()在c:\ usersrs \ andries \documents\visual studio 2015 \projects\opencvdotnet\opencvdotnet\opencvdotnet.cpp:regel 24

       bij WinFormProject.Form1.button1_Click(对象发送者,EventArgs e)在C:\ Users \ Annies \documents \ visual studio 2015 \Projects \ OpencvDotNet \ WinFormProject\Form1.cs:regel 49

       bij System.Windows.Forms.Control.OnClick(EventArgs e)

       bij System.Windows.Forms.Button.OnClick(EventArgs e)

       bij System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)

       bij System.Windows.Forms.Control.WmMouseUp(消息& m,MouseButtons按钮,Int32点击)

       bij System.Windows.Forms.Control.WndProc(Message& m)

       bij System.Windows.Forms.ButtonBase.WndProc(Message& m)

       bij System.Windows.Forms.Button.WndProc(Message& m)

       bij System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)

       bij System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)

       bij System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 reason,Int32 pvLoopData)

        bij System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context)

       bij System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context)

       bij WinFormProject.Program.Main()在C:\ Users \ Annries \documents\visual studio 2015 \Projects\OpencvDotNet \ WinFormProject\Program.cs:regel 18

  InnerException:




奇怪的是,当我继续它给我这个图像:< img alt =""src ="https://social.msdn.microsoft.com/Forums/getfile/1131140">



我希望有人可以帮我解决这个非常非常奇怪的错误。






解决方案

Hi Turbo,


>> System.AccessViolationException


从MSDN,您可以获得以下消息:


AccessViolationException异常只能在不安全的托管代码中发生,或者当可验证的托管代码与非托管代码交互时:


1,在不安全的托管代码中发生的访问冲突可以表示为NullReferenceException异常或AccessViolationException异常,具体取决于平台。


2,非托管代码中的访问冲突始终包含在托管代码中的AccessViolationException异常中。


在任何一种情况下,您都可以识别并更正AccessViolationException异常的原因,如下所示:如下:


1,确保您已尝试访问的内存已分配。


2, 确保您尝试访问的内存未损坏。


您还可以使用try / catch块来处理它。


更多信息详情请参阅以下文件:


AccessViolationException类


问候,


斯坦利


Hello,

This is a follow up to my latest thread, which has been answered.

I want to convert a Mat image into a Bitmap, then send it over to my C# GUI to display that image on a Windows Form Application.

Now the following code gives me this error:

System.AccessViolationException was unhandled
  HResult=-2147467261
  Message=Poging tot het lezen of schrijven van beveiligd geheugen. Dit duidt er vaak op dat ander geheugen is beschadigd.
  Source=System.Drawing
  StackTrace:
       bij System.Drawing.SafeNativeMethods.Gdip.GdipDrawImageRectI(HandleRef graphics, HandleRef image, Int32 x, Int32 y, Int32 width, Int32 height)
       bij System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height)
       bij System.Drawing.Graphics.DrawImage(Image image, Rectangle rect)
       bij System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
       bij System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
       bij System.Windows.Forms.Control.WmPaint(Message& m)
       bij System.Windows.Forms.Control.WndProc(Message& m)
       bij System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  InnerException:

And it points to

Application.Run(new Form1());


So I tried a different approach.


Try to load a Mat. Convert that Mat into a Bitmap just like above using this line of code:

Bitmap^ test = gcnew Bitmap(matToConvert.rows, matToConvert.cols, 4 * matToConvert.rows, System::Drawing::Imaging::PixelFormat::Format24bppRgb, IntPtr(matToConvert.data));

Then save it immediately after this line to a file as .bmp. But when I want to run the code it gives me the same error:

System.AccessViolationException was unhandled
  HResult=-2147467261
  Message=Poging tot het lezen of schrijven van beveiligd geheugen. Dit duidt er vaak op dat ander geheugen is beschadigd.
  Source=System.Drawing
  StackTrace:
       bij System.Drawing.SafeNativeMethods.Gdip.GdipSaveImageToFile(HandleRef image, String filename, Guid& classId, HandleRef encoderParams)
       bij System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
       bij OpencvDotNet.OpenCvWrapper.ApplyFilter() in c:\users\andries\documents\visual studio 2015\projects\opencvdotnet\opencvdotnet\opencvdotnet.cpp:regel 24
       bij WinFormProject.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\Andries\documents\visual studio 2015\Projects\OpencvDotNet\WinFormProject\Form1.cs:regel 49
       bij System.Windows.Forms.Control.OnClick(EventArgs e)
       bij System.Windows.Forms.Button.OnClick(EventArgs e)
       bij System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       bij System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       bij System.Windows.Forms.Control.WndProc(Message& m)
       bij System.Windows.Forms.ButtonBase.WndProc(Message& m)
       bij System.Windows.Forms.Button.WndProc(Message& m)
       bij System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       bij System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       bij System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       bij System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       bij System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       bij WinFormProject.Program.Main() in C:\Users\Andries\documents\visual studio 2015\Projects\OpencvDotNet\WinFormProject\Program.cs:regel 18
  InnerException:

Weird thing is, when I continue it gives me this image:

I hope someone can help me with this very, very weird error.



解决方案

Hi Turbo,

>>System.AccessViolationException

From MSDN, you can get the following messages that say:

An AccessViolationException exception can occur only in unsafe managed code or when verifiable managed code interacts with unmanaged code:

1, An access violation that occurs in unsafe managed code can be expressed as either a NullReferenceException exception or an AccessViolationException exception, depending on the platform.

2, An access violation in unmanaged code that bubbles up to managed code is always wrapped in an AccessViolationException exception.

In either case, you can identify and correct the cause of the AccessViolationException exception as follows:

1, Make sure that the memory that you are attempting to access has been allocated.

2, Make sure that the memory that you are attempting to access has not been corrupted.

Also you can use a try/catch block to handle it.

For more details, please refer to the following document:

AccessViolationException Class

Regards,

Stanly


这篇关于将位图保存到文件会出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆