printing MSchart in VB2005

I am having lots of trouble trying to print a chart in VB2005

I get this error now

The runtime has encountered a fatal error. The address of the error was at 0x7a005c3d, on thread 0x11c4. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

this is my code :

AxMSChart1.EditCopy()

If My.Computer.Clipboard.ContainsImage() Then #### error here ######

Dim grabpicture As System.Drawing.Image

grabpicture = My.Computer.Clipboard.GetImage()

' PictureBox1.Image = grabpicture 'temp just to check its here

End If

This should work acording to the msdn help

I know the chart is on the clipboard as i can past it into exel it is just reading it back into graabpicture that will not work then print the picture as a bmp which i have done many times before.



Answer this question

printing MSchart in VB2005

  • yousef.net

    I get the same error.

    It used to work with VB2003 standard, but after upgrading to VB2005 pro. the error was there.

    I tried
    Clipboard.GetData("Bitmap")
    Clipboard.GetDataObject
    and Clipboard.GetImage

    Nothing works when I use mschart.editcopy, but I know printing from the clipboard works with other images.

    please help


  • frusciante

    Here is what I had to do to fix it....

    'Replace the following

    'm_oMSChart1.SelectPart(0, 0, 0, 0, 0)

    'm_oMSChart1.EditCopy()

    'Pcte = Clipboard.GetDataObject.GetData(DataFormats.Bitmap, True)

    'm_oGraphPic1.FormattedPicture = VB6.ImageToIPictureDisp(Pcte)

    'with

    m_oMSChart1.Backdrop.Fill.Brush.Style = MSChart20Lib.VtBrushStyle.VtBrushStyleSolid

    m_oMSChart1.Backdrop.Fill.Style = MSChart20Lib.VtFillStyle.VtFillStyleBrush

    System.Windows.Forms.Application.DoEvents()

    Pcte = Hardcopy.CreateBitmap(m_oMSChart1)

    m_oGraphPic1.FormattedPicture = VB6.ImageToIPictureDisp(Pcte)

    m_oMSChart1.Backdrop.Fill.Style = MSChart20Lib.VtFillStyle.VtFillStyleNull

    m_oMSChart1.Backdrop.Fill.Brush.Style = MSChart20Lib.VtBrushStyle.VtBrushStyleSolid

    and add these classes

    Public Class Hardcopy

    Public Shared Function CreateBitmap(ByVal Control As Control) As Bitmap

    Dim gDest As Graphics

    Dim hdcDest As IntPtr

    Dim hdcSrc As Integer

    Dim hWnd As Integer = Control.Handle.ToInt32

    CreateBitmap = New Bitmap(Control.Width, Control.Height)

    gDest = Graphics.FromImage(CreateBitmap)

    hdcSrc = Win32.GetWindowDC(hWnd)

    hdcDest = gDest.GetHdc

    Win32.BitBlt(hdcDest.ToInt32, 0, 0, Control.Width, Control.Height, hdcSrc, 0, 0, Win32.SRCCOPY)

    gDest.ReleaseHdc(hdcDest)

    Win32.ReleaseDC(hWnd, hdcSrc)

    End Function

    End Class

    Public Class Win32

    Public Declare Function BitBlt Lib "gdi32" Alias "BitBlt" (ByVal hDestDC As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hSrcDC As Integer, ByVal xSrc As Integer, ByVal ySrc As Integer, ByVal dwRop As Integer) As Integer

    Public Declare Function GetWindowDC Lib "user32" Alias "GetWindowDC" (ByVal hwnd As Integer) As Integer

    Public Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hwnd As Integer, ByVal hdc As Integer) As Integer

    Public Const SRCCOPY As Integer = &HCC0020

    End Class


  • SusanHX

    That didn't work for me. I had to use Win32 API calls to solve the problem:


    using System.Runtime.InteropServices;
    ...

    //copy the chart bitmap to the clipboard
    MSChart1.EditCopy();

    Application.DoEvents();

    //open the clipboard
    if (Win32ClipboardAPI.OpenClipboard(IntPtr.Zero))
    {
    //check that it has recognized the bitmap
    if (Win32ClipboardAPI.IsClipboardFormatAvailable(Win32ClipboardAPI.CF_BITMAP))
    {
    //Get the pointer for the current Clipboard Data
    IntPtr bmptr = Win32ClipboardAPI.GetClipboardData(Win32ClipboardAPI.CF_BITMAP);

    if (bmptr != IntPtr.Zero)
    {
    //retrieve the bitmap
    Bitmap chartCapture = Bitmap.FromHbitmap(bmptr);
    .....

    //tidy up
    chartCapture.Dispose();
    }
    }

    //Close the clipboard and release unused resources
    Win32ClipboardAPI.CloseClipboard();
    }


    private class Win32ClipboardAPI
    {
    public const uint CF_BITMAP = 2;

    [DllImport("user32.dll")]
    public static extern bool OpenClipboard(IntPtr hWndNewOwner);

    [DllImport("user32.dll")]
    public static extern IntPtr GetClipboardData(uint uFormat);

    [DllImport("user32.dll")]
    public static extern bool CloseClipboard();

    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern bool IsClipboardFormatAvailable(uint format);
    }


  •  JD 

    i am using panel control and make it container of Mschart control and to take copy of mschart i use panel.darwtobitmap().

    It is not working properly, copies only boundary of mschart control.



  • gfs

    You also can place the mschart control in a panel and use the panel's drawtobitmap method to get an image of the chart.


  • printing MSchart in VB2005