string sqlConnectionString = "Your connection string.";
SqlConnection sqlConnection = new SqlConnection(sqlConnectionString);
string sqlCommandString = "Select [FileName], [ContentType], [BinaryStream] From [UploadFile] Where [ID]=@ID";
SqlCommand sqlCommand = new SqlCommand(sqlCommandString, sqlConnectionString);
sqlCommand.Parameters.AddWithValue("@ID", 1);
DataTable myDataTable = new DataTable();
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
sqlDataAdapter.Fill(myDataTable);
DataRow myImageFile = myDataTable.Rows[0];
BitmapImage myBitmapImage = new BitmapImage();
myBitmapImage.BeginInit();
myBitmapImage.StreamSource = new MemoryStream((byte[])myImageFile["BinaryStream"]);
myBitmapImage.EndInit();
Image myImage = new Image();
myImage.Source = myBitmapImage;
Code (VB.NET)
Dim sqlConnectionString As String = "Your connection string."
Dim sqlConnection As New SqlConnection(sqlConnectionString)
Dim sqlCommandString As String = "Select [FileName], [ContentType], [BinaryStream] From [UploadFile] Where [ID]=@ID"
Dim sqlCommand As New SqlCommand(sqlCommandString, sqlConnectionString)
sqlCommand.Parameters.AddWithValue("@ID", 1)
Dim myDataTable As New DataTable()
Dim sqlDataAdapter As New SqlDataAdapter(sqlCommand)
sqlDataAdapter.Fill(myDataTable)
Dim myImageFile As DataRow = myDataTable.Rows(0)
Dim myBitmapImage As New BitmapImage()
myBitmapImage.BeginInit()
myBitmapImage.StreamSource = New MemoryStream(CType(myImageFile("BinaryStream"), Byte()))
myBitmapImage.EndInit()
Dim myImage As New Image()
myImage.Source = myBitmapImage