locked
How to get Clipboard data and convert it into Image/Bitmap object ? RRS feed

  • Question

  • Hi,

       I have some tables in the Word Document.   I am using Microsoft.Word.Interop to  read and select the table.  After selection of Table , I want to show the preview of this  Table in   the Picture Box control  of Windows Form Dialog.

    The way i am doing is:--

     table.Range.Select();

     wordDoc.ActiveWindow.Selection.Range.CopyAsPicture(); // Copy the Selected Table object of word document

     bool imgPresent = Clipboard.ContainsData(System.Windows.Forms.DataFormats.MetafilePict); // It comes true
     bool canbeCovertible = Clipboard.ContainsData(System.Windows.Forms.DataFormats.EnhancedMetafile); // It also comes true

    bool containImg = Clipboard.ContainImage() // It  comes false

    if (Clipboard.GetDataObject().GetDataPresent(System.Windows.Forms.DataFormats.EnhancedMetafile))  //true

    {

       Bitmap bmp= (Bitmap)dobj.GetData(System.Windows.Forms.DataFormats.EnhancedMetafile, true); // exception occurs here

       PictureBox picture1 = new PictureBox();

       picture.Image = bmp;

    }

    I am getting exception that parameter not valid once it enter inside the if  block to convert the Clipboard data to Bitmap object.

    The Selction of Table and copy to clipboard is  happening but I am unable to set in Picture box due to this error.  While the Clipboard data I can paste in Paint brush application.

    Please let me know how to correct this issue? Is there anyway without Clipboard copy I can get the preview of the Table ?

    Tuesday, November 6, 2012 1:44 PM

All replies

  • Copy the metafile to the clipboard and word should make it available as bitmap.

    --
    .
    --

    Hi,

       I have some tables in the Word Document.   I am using Microsoft.Word.Interop to  read and select the table.  After selection of Table , I want to show the preview of this  Table in   the Picture Box control  of Windows Form Dialog.

    The way i am doing is:--

     table.Range.Select();

     wordDoc.ActiveWindow.Selection.Range.CopyAsPicture(); // Copy the Selected Table object of word document

     bool imgPresent = Clipboard.ContainsData(System.Windows.Forms.DataFormats.MetafilePict); // It comes true
     bool canbeCovertible = Clipboard.ContainsData(System.Windows.Forms.DataFormats.EnhancedMetafile); // It also comes true

    bool containImg = Clipboard.ContainImage() // It  comes false

    if (Clipboard.GetDataObject().GetDataPresent(System.Windows.Forms.DataFormats.EnhancedMetafile))  //true

    {

       Bitmap bmp= (Bitmap)dobj.GetData(System.Windows.Forms.DataFormats.EnhancedMetafile, true); // exception occurs here

       PictureBox picture1 = new PictureBox();

       picture.Image = bmp;

    }

    I am getting exception that parameter not valid once it enter inside the if  block to convert the Clipboard data to Bitmap object.

    The Selction of Table and copy to clipboard is  happening but I am unable to set in Picture box due to this error.  While the Clipboard data I can paste in Paint brush application.

    Please let me know how to correct this issue? Is there anyway without Clipboard copy I can get the preview of the Table ?

    • Proposed as answer by DavidMCandy Tuesday, November 13, 2012 2:55 AM
    Thursday, November 8, 2012 11:15 AM