使用Graphics.DrawLine创建访问冲突有困难。尝试重新启动VS2010 /计算机,修复框架,Windows更新,并查找要删除的任何类型的修补程序或修补程序。似乎什么都没有摆脱它。甚至MS站点上的示例也会因错误而崩溃。不知道要找什么来解决它。它曾经工作过,所以我在某个地方打赌它是一个糟糕的补丁,只是不确定哪个。目前在Windows XP下运行。
更新1:更新了与API交叉引用的代码。 DrawLineInt使用Framework,DrawLineInt2使用API命令。它们都违反了DrawLine功能。将此示例投入VB6不会产生此问题。
更新2:显然,如果VS工作室环境设置设置为C ++,则此错误不会发生。重置设置并将其放回" Visual Basic"导致此错误再次出现。卸载/安装VS2010和.NET框架没有解决这个问题。运行构建的可执行文件不会导致可执行文件崩溃,但可能会崩溃VS ...嗯......?
更新3:尝试了其他绘图方法,并在下面添加了一个调用跟踪。显然,任何使用PEN的方法都会导致异常,但是使用BRUSH的方法则不会。在copy_32方法内部进行跟踪时,指向绘图空间的指针无效(0x0或0xBAADF00D)。这也使得GroupBox无用,因为它使用相同的绘制例程。
示例代码......
Public Class Form1
Public Sub DrawLineInt2(ByVal e As PaintEventArgs)
'Misc
Dim Ret As GpStatus
' Create pen.
Dim blackPen As IntPtr = IntPtr.Zero
Dim blackbrush As IntPtr = IntPtr.Zero
Dim c As Int32 = &HFF000000
Ret = GdipCreateSolidFill(c, blackbrush)
'Ret = GdipCreatePen1(c, 10, GpUnit.UnitPixel, blackPen)
Ret = GdipCreatePen2(blackbrush, 10, GpUnit.UnitPixel, blackPen)
' Create coordinates of points that define line.
Dim x1 As Integer = 100
Dim y1 As Integer = 100
Dim x2 As Integer = 500
Dim y2 As Integer = 125
' Draw line to screen.
Dim graphics As IntPtr = IntPtr.Zero
'Ret = GdipCreateFromHDC(e.Graphics.GetHdc, graphics)
Ret = GdipCreateFromHWND(Me.Handle, graphics)
'System.AccessViolationException here
Ret = GdipDrawLineI(graphics, blackPen, x1, y1, x2, y2)
'Here Also
'Ret = GdipDrawRectangleI(graphics, blackPen, x1, y1, x2, y2)
'Here as well
'Ret = GdipDrawArcI(graphics, blackPen, x1, y1, x2, y2, 15, 20)
'No problems with this line
'Ret = GdipFillRectangleI(graphics, blackbrush, x1, y1, x2, y2)
Ret = GdipDeletePen(blackPen)
Ret = GdipDeleteBrush(blackbrush)
Ret = GdipDeleteGraphics(graphics)
'e.Graphics.ReleaseHdc()
End Sub
Public Sub DrawLineInt(ByVal e As PaintEventArgs)
' Create pen.
Dim blackPen As New Pen(Color.Black, 3)
' Create coordinates of points that define line.
Dim x1 As Integer = 100
Dim y1 As Integer = 100
Dim x2 As Integer = 500
Dim y2 As Integer = 100
' Draw line to screen.
Try
e.Graphics.DrawLine(blackPen, x1, y1, x2, y2)
Catch
End Try
blackPen.Dispose()
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
'DrawLineInt(e)
DrawLineInt2(e)
End Sub
Private Enum GpStatus As Int32
Ok = 0
GenericError = 1
InvalidParameter = 2
OutOfMemory = 3
ObjectBusy = 4
InsufficientBuffer = 5
NotImplemented = 6
Win32Error = 7
WrongState = 8
Aborted = 9
FileNotFound = 10
ValueOverflow = 11
AccessDenied = 12
UnknownImageFormat = 13
FontFamilyNotFound = 14
FontStyleNotFound = 15
NotTrueTypeFont = 16
UnsupportedGdiplusVersion = 17
GdiplusNotInitialized = 18
PropertyNotFound = 19
PropertyNotSupported = 20
End Enum
Private Enum GpUnit As Int32 ' aka Unit
UnitWorld ' 0 -- World coordinate (non-physical unit)
UnitDisplay ' 1 -- Variable -- for PageTransform only
UnitPixel ' 2 -- Each unit is one device pixel.
UnitPoint ' 3 -- Each unit is a printer's point, or 1/72 inch.
UnitInch ' 4 -- Each unit is 1 inch.
UnitDocument ' 5 -- Each unit is 1/300 inch.
UnitMillimeter ' 6 -- Each unit is 1 millimeter.
End Enum
Private Declare Function GdipDrawLine Lib "gdiplus.dll" (ByVal graphics As IntPtr, ByVal pen As IntPtr, ByVal x1 As Single, ByVal y1 As Single, ByVal x2 As Single, ByVal y2 As Single) As GpStatus
Private Declare Function GdipDrawLineI Lib "gdiplus.dll" (ByVal graphics As IntPtr, ByVal pen As IntPtr, ByVal x1 As Int32, ByVal y1 As Int32, ByVal x2 As Int32, ByVal y2 As Int32) As GpStatus
Private Declare Function GdipCreatePen1 Lib "gdiplus.dll" (ByVal color As Int32, ByVal Width As Single, ByVal unit As GpUnit, ByRef pen As IntPtr) As GpStatus
Private Declare Function GdipCreatePen2 Lib "gdiplus.dll" (ByVal brush As IntPtr, ByVal Width As Single, ByVal unit As GpUnit, ByRef pen As IntPtr) As GpStatus
Private Declare Function GdipDeletePen Lib "gdiplus.dll" (ByVal pen As Int32) As GpStatus
Private Declare Function GdipCreateSolidFill Lib "gdiplus" (ByVal argb As Int32, ByRef brush As IntPtr) As GpStatus
Private Declare Function GdipDeleteBrush Lib "gdiplus" (ByVal brush As IntPtr) As GpStatus
Private Declare Function GdipFillRectangleI Lib "gdiplus" (ByVal graphics As IntPtr, ByVal brush As IntPtr, ByVal x As Int32, ByVal y As Int32, ByVal Width As Int32, ByVal Height As Int32) As GpStatus
Private Declare Function GdipDrawRectangleI Lib "gdiplus" (ByVal graphics As IntPtr, ByVal pen As IntPtr, ByVal x As Int32, ByVal y As Int32, ByVal Width As Int32, ByVal Height As Int32) As GpStatus
Private Declare Function GdipDrawArcI Lib "gdiplus" (ByVal graphics As IntPtr, ByVal pen As IntPtr, ByVal x As Int32, ByVal y As Int32, ByVal Width As Int32, ByVal Height As Int32, ByVal startAngle As Single, ByVal sweepAngle As Single) As GpStatus
Private Declare Function GdipCreateFromHDC Lib "gdiplus.dll" (ByVal hdc As IntPtr, ByRef graphics As IntPtr) As GpStatus
Private Declare Function GdipCreateFromHWND Lib "gdiplus.dll" (ByVal hwnd As IntPtr, ByRef graphics As IntPtr) As GpStatus
Private Declare Function GdipDeleteGraphics Lib "gdiplus" (ByVal graphics As IntPtr) As GpStatus
结束班
异常调用堆栈
GdiPlus.dll!ScanOperation :: Copy_32()+ 0x14 bytes
GdiPlus.dll!EpAlphaBlender :: Blend()+ 0x58 bytes
GdiPlus.dll!EpScanGdiDci :: DrawScanRecords_Dci()+ 0x1de bytes
GdiPlus.dll!EpScanGdiDci :: ProcessBatch_Dci()+ 0x181字节 GdiPlus.dll!EpScanGdiDci :: EmptyBatch()+ 0x6b5ab字节 GdiPlus.dll!EpScanGdiDci :: End()+ 0x1e bytes
GdiPlus.dll!EpScanBufferNative :: ~EpScanBufferNative()+ 0x18 bytes
GdiPlus.dll!DpDriver :: StrokePath()+ 0x20a字节
GdiPlus.dll!DriverMulti :: StrokePath()+ 0x6c字节 GdiPlus.dll!GpGraphics :: DrvStrokePath()+ 0x2a字节
GdiPlus.dll!GpGraphics :: RenderDrawPath()+ 0xa3 bytes
GdiPlus.dll!GpGraphics :: DrawLines()+ 0xe8 bytes
GdiPlus.dll!GpGraphics :: DrawLine()+ 0x45字节
GdiPlus.dll!_GdipDrawLine @ 24()+ 0x8e字节
GdiPlus.dll!_GdipDrawLineI @ 24()+ 0x37字节
[外部代码] WindowsApplication2.exe!WindowsApplication2.Form1.DrawLineInt2(System.Windows.Forms.PaintEventArgs e)第30行+ 0x17字节基本
它放弃的例外......
System.AccessViolationException未处理Message = Attempted to 读或写受保护的内存。这通常表明其他 记忆已腐败。 Source = System.Drawing StackTrace:at System.Drawing.SafeNativeMethods.Gdip.GdipDrawLineI(HandleRef graphics,HandleRef pen,Int32 x1,Int32 y1,Int32 x2,Int32 y2)at System.Drawing.Graphics.DrawLine(钢笔,Int32 x1,Int32 y1,Int32 x2,Int32 y2)at WindowsApplication1.Form1.DrawLineInt(PaintEventArgs e)中 C:\ Documents and Settings \ FF \ Local Settings \ Application Data \ Temporary Projects \ WindowsApplication1 \ Form1.vb:第15行at WindowsApplication1.Form1.Form1_Paint(Object sender,PaintEventArgs e) 在C:\ Documents and Settings \ FF \ Local Settings \ Application中 Data \ Temporary Projects \ WindowsApplication1 \ Form1.vb:第19行at System.Windows.Forms.Control.OnPaint(PaintEventArgs e)at System.Windows.Forms.Form.OnPaint(PaintEventArgs e)at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, 在System.Windows.Forms.Control.WmPaint(消息& m)的Int16层) 在System.Windows.Forms.Control.WndProc(Message& m)at System.Windows.Forms.ScrollableControl.WndProc(Message& m)at System.Windows.Forms.Form.WndProc(Message& m)at 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)at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr的 dwComponentID,Int32 reason,Int32 pvLoopData)at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(的Int32 reason,ApplicationContext context)at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(的Int32 reason,ApplicationContext context)at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() 在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() 在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(字符串[] WindowsApplication1.My.MyApplication.Main(String []上的commandLine) Args)in 17d14f5c-a337-4978-8281-53493378c1071.vb:第81行 System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String [] args)在System.AppDomain.ExecuteAssembly(String assemblyFile, 证据assemblySecurity,String [] args)at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()at System.Threading.ThreadHelper.ThreadStart_Context(Object state)at System.Threading.ExecutionContext.Run(执行上下文 executionContext,ContextCallback回调,对象状态,布尔值 ignoreSyncCtx)at System.Threading.ExecutionContext.Run(执行上下文 executionContext,ContextCallback回调,对象状态)at System.Threading.ThreadHelper.ThreadStart()
答案 0 :(得分:0)
找到适合我的解决方案。升级或回滚视频驱动程序。
我有一个驱动程序版本为9.00.100.10的Radeon HD6950。当我将驱动程序回滚到版本8.980.0.0时。 “gdipdrawline”错误停止了。这意味着现在使用该功能的所有控件也可以工作。我可以确认使用GroupBox和DataGridView,因为它们之前使用相同或类似的调用堆栈崩溃。