none
How to convert Image files to stream...

    Question

  • Hi,

    How to conver an image files like bmp, jpg, gif, tiff, etc into Stream or MemoryStream. The image may be from local disk or database like Northwind.

    Cheer.

    Friday, March 03, 2006 1:24 PM

Answers

  • yes. Thanks a lot. Now it's working fine. Here is a code snippet that I got successful.

    string cmdString = "select Photo from Employees";

    conn = new OleDbConnection( connString );

    OleDbCommand cmd1 = conn.CreateCommand();

    cmd1.CommandText = cmdString;

    if (conn.State != ConnectionState.Open)

    conn.Open();

    byte[] imageData = (byte[])cmd1.ExecuteScalar();

    MemoryStream mem = new MemoryStream(imageData,78,imageData.Length - 78);

    pictureBox1.Image = Image.FromStream(mem);

    conn.Close();

     

    Tuesday, March 07, 2006 5:07 AM
  • How to Know whether an OLE object contains Images.

    Also can any one ans me what kind of information is stored by OLE header in database.

    Tuesday, March 07, 2006 11:05 AM

All replies


  • Image image = Image.FromFile( @"c:\image.bmp" );
    using(MemoryStream stream = new MemoryStream())
    {
     // Save image to stream.
     image.Save( stream, ImageFormat.Bmp );
    }

     

    Friday, March 03, 2006 2:49 PM
    Moderator
  • Thanks for your reply with code snippet.

    I want to create an application that reterives an image from Northwind Employees table and I Load that images to the picturebox. The picture box get the image from stream. Can any one suggest me the way of getting image from database and convert it to stream.

    Cheer.

    Saturday, March 04, 2006 11:29 AM
  • byte data[] =new byte[]; //(trying to convert from vb code)

    System.IO.MemoryStream str;

    Bitmap resim ;

    'open your datareader

    data = dr.Item["UrunResim"];

    str = new System.IO.MemoryStream(data);

    resim = new System.Drawing.Bitmap(str); 

    I think it'll help

    Saturday, March 04, 2006 12:42 PM
  • Here is some self explaing code:


    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();
        }
    }

     

    Saturday, March 04, 2006 2:29 PM
    Moderator
  • Hi,

    Thanks for the response with code snippet.

    I have tried with this code snippet. But, It throws "An unhandled exception of type 'System.ArgumentException' occurred in system.drawing.dll" exception while drawing am image to the picture box. This cause is occur only getting am image from database. But am image is getting from file means it works fine.

    I thing the images are stored to the database with some encripted or different image format compare with original image format. 

    Can any one say what's happen wrong with this.

    Cheer.

     

    Monday, March 06, 2006 4:39 AM
  • Please see my most recent response to your other thread ("Why I can not get image from Northwind Database ?"). The images in the Northwind database have a 48-byte OLE header which must be removed. I've posted code demonstrating how to do this in the other thread. -- Brendan Reynolds wrote in message news:d5924133-4e4e-40d8-8859-5078f135446c@discussions.microsoft.com... > Hi, > > Thanks for the response with code snippet. > > I have tried with this code snippet. But, It throws "An unhandled > exception of type 'System.ArgumentException' occurred in > system.drawing.dll" exception while drawing am image to the picture box. > This cause is occur only getting am image from database. But am image is > getting from file means it works fine. > > I thing the images are stored to the database with some encripted or > different image format compare with original image format. > > Can any one say what's happen wrong with this. > > Cheer. > > > >
    Monday, March 06, 2006 4:53 PM
  • yes. Thanks a lot. Now it's working fine. Here is a code snippet that I got successful.

    string cmdString = "select Photo from Employees";

    conn = new OleDbConnection( connString );

    OleDbCommand cmd1 = conn.CreateCommand();

    cmd1.CommandText = cmdString;

    if (conn.State != ConnectionState.Open)

    conn.Open();

    byte[] imageData = (byte[])cmd1.ExecuteScalar();

    MemoryStream mem = new MemoryStream(imageData,78,imageData.Length - 78);

    pictureBox1.Image = Image.FromStream(mem);

    conn.Close();

     

    Tuesday, March 07, 2006 5:07 AM
  • How to Know whether an OLE object contains Images.

    Also can any one ans me what kind of information is stored by OLE header in database.

    Tuesday, March 07, 2006 11:05 AM
  • Hi,

    I have kept the database with OLE Object and I have stored different data like bitmap images, other vector images, excel chart, word document, pdf document, etc to that field.

    I will have to use C# code to get those data[OLE Field] from database and categorize those data and display it into appropriate object like bitmap and vector images are to picture box, word files, pdf files, excel charts are into web browser, etc. For this pupose I need to get the OLE header information [I think this Ole header information has to store type of data]. Can any one help me.

    Cheer.

    Wednesday, March 08, 2006 4:16 AM
  • Prabu -

    It looks like your first question was answered.  As for your second question, it would help to split this into a seperate thread (it helps keep question/answer pairs more clean).

    Also, I'd try the SQL Server Data Access Forum.
    Forum for data access to SQL Server using SQL Native Client, OLEDB, ODBC, ADO, MDAC JDBC, or SOAP/HTTP. For .NET (ADO.NET) use Data Access Forum in the .NET group.

    http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=87&SiteID=1

     

    Thanks!
    Karen

     

    Saturday, March 11, 2006 12:11 AM
  • Thank you for your suggestion sir.

    I will have to create a seperate threat for my doubt about accessing OLE header information.

    Cheer.

    Saturday, March 11, 2006 6:55 AM
  • look at my problem.

     

    i have a 100 images .

    i want to send images through  web service from windows mobile .

     

    previously i have used to convert from file stream to byte array.

    but it is taking too much time and every time i am getting socket Exception .

    so

    i am trying to send fast .so i select memory stream.

    but for me , it is little bit  difficult to convert image to byte[]  through memory stream.

     

    could you please suggest which stream is better to send lot of images?

    how to avoid that socket exception?

     

    please any one suggest me regarding this problems?

    Saturday, September 17, 2011 7:02 AM