Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to convert an SQL Server BLOB string to System.Drawing.Image?
    text
    copied!<p>How can I convert a BLOB represented as a string to a System.Drawing.Image type?</p> <h3>Background</h3> <p>I am going to import information about users together with their photo from a csv file using c#. The proprietary SDK that I use requires the photo to be a System.Drawing.Image</p> <p>Below is an example of the csv file. </p> <pre><code>surname firstname photo Blogs Joe 0xFFD8FFE000104A46494600010101005F005F0000FFDB0043000D090A </code></pre> <p>The photo field is actually 5k chars long and is a direct export of the BLOB field value in the sql server db. We have just taken the raw value from the database field and exported it to the csv file.</p> <p>Below is the code that demonstrates how far I have got. The <code>cvsuser</code> variable represents one row of the csv file.</p> <pre><code>// We need to convert the photo from a string to a byte array string strPhoto = null; strPhoto = csvuser.photo; // The string that represents the BLOB byte[] bytes = new byte[strPhoto.Length * sizeof(char)]; System.Buffer.BlockCopy(strPhoto.ToCharArray(), 0, bytes, 0, bytes.Length); // Then we create a memory stream that holds the image MemoryStream photostream = new MemoryStream( bytes ); // Then we can create a System.Drawing.Image by using the Memory stream var photo = Image.FromStream(photostream); </code></pre> <p>However, the <code>Image.FromStream()</code> line throws a System.ArgumentException with the message "Parameter is not valid."</p> <h3>Question</h3> <p>How can I convert the BLOB represented as a string to a System.Drawing.Image type? </p> <p>The examples I have seen previously would, for example, fetch the blob straight from the database or reading the image from a file.</p>
 

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