How to Convert Copied Clipboard Image Data From Excel To Byte Array. Basically I am trying to save Excel CopyPicture image to the database. For which first I need to convert clipboard image to byte array and update in OLEDB Field of access database.
I tried this code below

oRng = oSheet.get_Range("A3", "A3");
                Clipboard.Clear();
                oRng.CopyPicture(XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlBitmap);
     IDataObject idata = (IDataObject)Clipboard.GetDataObject();

                

  /*              string[] sdata = idata.GetFormats();
                for (int counter = 0; counter < sdata.Length; counter++)
                {
                    MessageBox.Show(sdata[counter].ToString());
                }
     */               
// picbox.Image = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap); 
                    //bmp = (Bitmap)picbox.Image;
                    // IDataObject idata = (IDataObject)Clipboard.GetDataObject();
                    //idata.
if (idata.GetDataPresent(System.Windows.Forms.DataFormats.MetafilePict))
                    {
                        
                        //System.Drawing.Imaging.Metafile 
                            picbox.Image =   (Image) idata.GetData(DataFormats.MetafilePict, true); // ERROR here. I mean the return value is always null when I copy from Excel the picture through CopyPicture code.
                        //bmp = (Bitmap)idata.GetData(DataFormats.EnhancedMetafile, true);
                        bmp = (Bitmap)picbox.Image;
                        
                    }
  bmp.Save(memStream,ImageFormat.Bmp); // ERROR I get error here when I manually copy and try to save as memory stream. error Message is "Value cannot be null" , " Parameter Name: stream Line: System.Drawing".
               

                
                using (BinaryReader reader = new BinaryReader(memStream))
                {
                    //byte[] imageData = null;
                    byteArray = reader.ReadBytes((int)memStream.Length);
                   
                }

Have a look at code-snippet.

Image img = Clipboard.GetImage();
             
            if (img != null)
            {
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                byte[] bytes = new byte[ms.Length];
                ms.Read(bytes, 0, bytes.Length);
            }

Have a look at code-snippet.

Image img = Clipboard.GetImage();
             
            if (img != null)
            {
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                byte[] bytes = new byte[ms.Length];
                ms.Read(bytes, 0, bytes.Length);
            }

I tried the above code earlier but it did not work as img variable is always null. This means code earlier than this code is not returning image. The code is below please have a look and tell me is anything wrong in this.

oRng = oSheet.get_Range("A3", "A3");
                Clipboard.Clear();
                oRng.Select();
                oRng.CopyPicture(XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlBitmap);

I tried this also (with out selecting the range) but the result is the same

oRng = oSheet.get_Range("A3", "A3");
                Clipboard.Clear();
                oRng.CopyPicture(XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlBitmap);

Hi check this article in http://netprogramminghelp.com,

how to export a image into excel using asp.net

Thank you for your help. My question is reverse I need to "Extract Pictures From Excel to Byte Array" and not the vice verse. I can insert image into excel but not extract them. So please let me know specifically about extracting images from excel to byte array in C#. Thank You Once Again.
Best Regards
Suhail AR Chougule

hello
this is the same problem i'm getting in my project..plz help me out regarding this if you get the solution. I'm getting null reference exception. plz help me out . It's really important
thanx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.