Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Csharp.net
I want to convert image to stream and pass it as parameter to other function please help me with this...
thanks in advance
Posted 14-Feb-13 1:00am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

public Image GetImageById( int id )
{
// Initialize connection.
SqlConnection dbconn = new SqlConnection( "connectionstring" );
 
try
{
// Create command and fetch image.
using(SqlCommand command = dbconn.CreateCommand())
{
// Set select query and add Id parameter with value.
command.CommandText = "SELECT [ImageField] FROM [Table1] WHERE [Id] = @Id";
command.Parameters.Add("@Id", SqlDbType.Int, 4).Value = id;

// Open the connection.
dbconn.Open();

// Excecute the select query.
byte[] imageData = (byte[])command.ExecuteScalar();

// If we recieved an image, return it; otherwise
// just return a null reference value.
if( imageData != null && imageData.Lenght != 0 )
{
using(MemoryStream stream = new MemoryStream( imageData ))
{
return Image.FromStream( stream );
}
}
else
{
// Image not found.
return null;
}
}
}
finally
{
dbconn.Close();
}
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

	
public Image GetImageById( int id )
{
    // Initialize connection.
    SqlConnection dbconn = new SqlConnection( "connectionstring" );
 
    try
    {
        // Create command and fetch image.
        using(SqlCommand command = dbconn.CreateCommand())
        {
            // Set select query and add Id parameter with value.
            command.CommandText = "SELECT [ImageField] FROM [Table1] WHERE [Id] = @Id";
            command.Parameters.Add("@Id", SqlDbType.Int, 4).Value = id;
            
            // Open the connection.
            dbconn.Open();
            
            // Excecute the select query.
            byte[] imageData = (byte[])command.ExecuteScalar();
            
            // If we recieved an image, return it; otherwise
            // just return a null reference value.
            if( imageData != null && imageData.Lenght != 0 )
            {            
                using(MemoryStream stream = new MemoryStream( imageData ))
                {
                    return Image.FromStream( stream );
                }
            }
            else
            {
                // Image not found.
                return null;
            }
        }
    }
    finally
    {
        dbconn.Close();
    }
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi,
 
Have a look here:
http://stackoverflow.com/a/1668493[^]
  Permalink  

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



Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 14 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100