关于.net中使用axmschart控件editcopy方法的问题!

来源:互联网 发布:nginx是什么 编辑:程序博客网 时间:2024/05/16 16:59

从AxMSChart复制图象到剪贴板然后再导出图片,出现错误,代码如下: 
                Dim iBitmap As System.Drawing.Bitmap

 

                AxMSChart1.EditCopy()
                iBitmap = Clipboard.GetImage
   
错误信息如下:

检测到   FatalExecutionEngineError
Message:   运行库遇到了错误。此错误的地址为   0x7a005c3d,在线程   0xc98   上。错误代码为   0xc0000005。此错误可能是   CLR   中的   bug,或者是用户代码的不安全部分或不可验证部分中的   bug。此   bug   的常见来源包括用户对   COM-interop   或   PInvoke   的封送处理错误,这些错误可能会损坏堆栈。

我猜测可能是mschart迁移到到.net之后出现的问题,单独用editcopy没问题,用画板可以读取内存里

的图片,用Clipboard.GetImage方法也没问题,但两者在一起就有问题,我估计可能editcopy方法独

占了剪贴板(我还是不相信)?亦或者editcopy线程不安全?具体到现在我也不太清楚,反正就不能简单共存

(以前VB里就这么用的)。

 

我也试着从网友介绍的用单线程、提升权限角度解决问题,但都没成功。既然我发现使用完

editcopy方法后用windows自带的画板能读取,就联想到我用api函数来读取clipboard行不行呢?经过

测试后,发现果然可以,我用vb.net的代码写的过程,大家可以参考一下:


声明:
    Private Const CF_BITMAP As Short = 2
    Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Integer) As

Integer
    Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Integer) As

Integer
    Private Declare Function CloseClipboard Lib "user32" () As Integer

功能过程代码:
        Dim iBitmapPtr As Integer
        Dim iBitmap As System.Drawing.Bitmap

        Me.AxMSChart1.EditCopy()
        OpenClipboard(Me.Handle.ToInt32)
        iBitmapPtr = GetClipboardData(CF_BITMAP)
        iBitmap = System.Drawing.Bitmap.FromHbitmap(iBitmapPtr)
        If iBitmap Is Nothing Then
            MessageBox.Show("获取Chart图像失败!", "消息", MessageBoxButtons.OK,

MessageBoxIcon.Error)
        Else
            Me.SaveFileDialog1.FileName = ""
            Me.SaveFileDialog1.Filter = "BMP文件(*.bmp)|*.bmp"
            Me.SaveFileDialog1.OverwritePrompt = True
            If Me.SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                If System.IO.File.Exists(Me.SaveFileDialog1.FileName) = True Then
                    System.IO.File.Delete(Me.SaveFileDialog1.FileName)
                End If
                iBitmap.Save(Me.SaveFileDialog1.FileName)
            End If
        End If
        CloseClipboard()

原创粉丝点击