1

Graphics.DrawLine 创建访问冲突时遇到困难。尝试重新启动 VS2010/计算机,修复框架,Windows 更新,并寻找任何类型的补丁或要删除的补丁。似乎没有什么可以摆脱它。甚至 MS 站点上的示例也因错误而崩溃。不知道要寻找什么来修复它。它曾经可以工作,所以我打赌它在某个地方是一个糟糕的补丁,只是不确定哪个。目前在windows XP下运行。

更新 1:更新了代码以与 API 交叉引用。DrawLineInt 使用Framework,DrawLineInt2 使用API​​ 命令。它们都违反了 DrawLine 函数。将此示例放入 VB6 中不会产生此问题。

更新 2:显然,如果 VS Studio 环境设置设置为 C++,则不会发生此错误。重置设置并将其放回“Visual Basic”会导致此错误再次出现。卸载/安装 VS2010 和 .NET 框架并没有解决这个问题。运行构建的可执行文件不会导致可执行文件崩溃,但会导致 VS...hmmm.. 崩溃?

更新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 字节
GdiPlus.dll!EpAlphaBlender::Blend() + 0x58 字节
GdiPlus.dll!EpScanGdiDci::DrawScanRecords_Dci() + 0x1de 字节
GdiPlus.dll!EpScanGdiDci::ProcessBatch_Dci() + 0x181 字节 GdiPlus.dll!EpScanGdiDci::EmptyBatch() + 0x6b5ab 字节 GdiPlus.dll!EpScanGdiDci::End() + 0x1e 字节
GdiPlus.dll!EpScanBufferNative::~EpScanBufferNative() + 0x18 字节
GdiPlus.dll!DpDriver::StrokePath () + 0x20a 字节
GdiPlus.dll!DriverMulti::StrokePath() + 0x6c 字节 GdiPlus.dll!GpGraphics::DrvStrokePath() + 0x2a 字节
GdiPlus.dll!GpGraphics::RenderDrawPath() + 0xa3 字节
GdiPlus.dll!GpGraphics: :DrawLines() + 0xe8 字节
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=尝试读取或写入受保护的内存。这通常表明其他内存已损坏。Source=System.Drawing StackTrace:在 System.Drawing.SafeNativeMethods.Gdip.GdipDrawLineI(HandleRef graphics, HandleRef pen, Int32 x1, Int32 y1, Int32 x2, Int32 y2) at System.Drawing.Graphics.DrawLine(Pen pen, Int32 x1 , Int32 y1, Int32 x2, Int32 y2) 在 C:\Documents and Settings\FF\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb 中的 WindowsApplication1.Form1.DrawLineInt(PaintEventArgs e):WindowsApplication1 的第 15 行。 C:\Documents and Settings\FF\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb 中的 Form1.Form1_Paint(Object sender, PaintEventArgs e):System.Windows.Forms.Control 的第 19 行。
在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ScrollableControl.WndProc(Message& m) 在 System.Windows.Forms.Form.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(味精和味精)
在 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 reason, ApplicationContext context) 在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() 在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() 在 Microsoft.VisualBasic.ApplicationServices .WindowsFormsApplicationBase.Run(String[] commandLine) at WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81 at System.AppDomain。_nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object状态)在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 在 System.Threading.ThreadHelper.ThreadStart( )RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback , 对象状态) 在 System.Threading.ThreadHelper.ThreadStart()RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback , 对象状态) 在 System.Threading.ThreadHelper.ThreadStart()

4

1 回答 1

0

找到了对我有用的解决方案。升级或回滚您的视频驱动程序。

我有一个驱动程序版本为 9.00.100.10 的 Radeon HD6950。当我将驱动程序回滚到版本 8.980.0.0 时。“gdipdrawline”错误停止。这意味着使用该功能的所有控件现在也可以使用。我可以确认 GroupBox 和 DataGridView 工作正常,因为它们之前因相同或相似的调用堆栈而崩溃。

于 2013-08-24T00:40:29.323 回答