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);
}