Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
try
{
    SqlConnection conn = new SqlConnection("Data Source=UDAY;Initial Catalog=Sample;Integrated Security=True");
    conn.Open();
    SqlCommand cmd = new SqlCommand("select id from testimage", conn);
    cmd.ExecuteNonQuery();
    if (image1.DataContext != null)
    {
                      return;
    }
    //using filestream object write the column as bytes and store it as an image
    foreach (DataRow dataRow in dataTable.Rows)
    {
        if (dataRow.ItemArray[0].ToString() == comboBox1.SelectedItem.ToString())
        {
            FileStream FS1 = new FileStream("c:\\image.jpg", FileMode.Create);
            byte[] blob = (byte[])dataRow.ItemArray[1];
            FS1.Write(blob, 0, blob.Length);
            FS1.Close();

            ImageSourceConverter imgs = new ImageSourceConverter();
                        
            image2.Stretch = Stretch.Fill;
            image2.DataContext = null;

        }
    }
}
catch (Exception ex)
{
    System.Windows.Forms.MessageBox.Show(ex.StackTrace);
}


I didn't get any errors, but the image is not loading. Can you help me?
Posted
Updated 21-Jan-11 4:05am
v3
Comments
#realJSOP 21-Jan-11 10:05am    
I added WPF to your question tags

You're also not setting the image source property to what you're hoping is the returned image.

I don't know if you need it or not, but I use this method to convert a byte[] array to an BitmapImage object:

C#
//--------------------------------------------------------------------------------
public static BitmapImage BitmapImageFromBytes(byte[] bytes)
{
   BitmapImage  image  = null;
   MemoryStream stream = null;
   try
   {
       stream = new MemoryStream(bytes);
       stream.Seek(0, SeekOrigin.Begin);
       System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
       image = new BitmapImage();
       image.BeginInit();
       MemoryStream ms = new MemoryStream();
       img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
       ms.Seek(0, SeekOrigin.Begin);
       image.StreamSource = ms;
       image.StreamSource.Seek(0, SeekOrigin.Begin);
       image.EndInit();
    }
    catch (Exception)
    {
       throw;
    }
    finally
    {
       stream.Close();
       stream.Dispose();
    }
    return image;
}
 
Share this answer
 
Comments
Member 10271446 5-Feb-14 1:30am    
As you said, iam using the same code to display the image from data base which is in byte[] format in "windows8 store" .. but it is showing an error message that "system.drawing is not found , are u missing an assembly reference?" .. pls let me know i fthere are nay assembly references to be added or any other alternative to display image
You are using ExecuteNonQuery. This does not return any rows, but any output parameters specified will be populated with data. You are not specifying any paramaters and your query is Select an ID.

Read the spec on ExecuteNonQuery to see this; http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx[^]

You should be executing a ExecuteReader and the query should include the necessary column containing the blob.

At present the blob won't have anything to write to the file.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900