Note that there are some explanatory texts on larger screens.

plurals
  1. POIssues using OleDbConnection to export an image in an excel document in c#
    primarykey
    data
    text
    <p>I want to export an image to an excel document in c#</p> <p>The image the following formats: System.Windows.Media.Imaging.BitmapImage</p> <p>These are the connection details: </p> <pre><code>private const string OleDbConnection = "provider=Microsoft.Jet.OLEDB.4.0;Data Source='{0}';Extended Properties=Excel 8.0;"; private const string OleDbInsert = "INSERT INTO [Report] ([Time], [Elapsed], [Description], [Picture]) values( @timestamp, @elapsed, @description, @pic);"; private const string OleDbCreate = "CREATE TABLE [Report] ([Time] varchar(255), [Elapsed] varchar(255), [Description] varchar(255), [pic] blob);"; </code></pre> <p>I am using the following code to write the image in excel:</p> <pre><code>foreach (var e in Entries) { using(var command = new OleDbCommand { Connection = connection, CommandText = OleDbInsert }) { command.Parameters.Add(new OleDbParameter("@component", OleDbType.BSTR) { Value = e.val1 }); command.Parameters.Add(new OleDbParameter("@result", OleDbType.BSTR) { Value = e.val2 }); command.Parameters.Add(new OleDbParameter("@description", OleDbType.BSTR) { Value = e.val3}); if (e.EncodedScreenshot != string.Empty) { byte[] imageContent = imageToByteArray(ImageWpfToGDI(e.Picture)); OleDbParameter ph = new OleDbParameter("@pic", OleDbType.Binary) { Value = imageContent}; ph.Size = imageContent.Length; command.Parameters.Add(ph); } } </code></pre> <p>These are the methods I am using:</p> <pre><code> public byte[] imageToByteArray(Image imageIn) { MemoryStream ms = new MemoryStream(); imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); return ms.ToArray(); } private System.Drawing.Image ImageWpfToGDI(System.Windows.Media.Imaging.BitmapImage image) { MemoryStream ms = new MemoryStream(); var encoder = new System.Windows.Media.Imaging.BmpBitmapEncoder(); encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(image as System.Windows.Media.Imaging.BitmapSource)); encoder.Save(ms); ms.Flush(); return System.Drawing.Image.FromStream(ms); } </code></pre> <p>All the data exports to the Excel file except the image (and there definitely exists an image). Could you please give me some advice on how to fix this? I have been searching online alot but I still cannot find a solution. </p> <p>I made some modifications according to this link: <a href="https://stackoverflow.com/questions/7211367/insert-and-retrieve-image-in-access-2007-db-from-c-sharp-app">Insert and retrieve Image in Access 2007 DB from C# App</a>. I believe my error is because of the image datatypes. What datatype should I use for the image in the DB and OleDbParameter? </p> <p>Could someone help me out please? I would be grateful with any response. </p> <p>Many thanks! :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload