Click here to Skip to main content
15,943,406 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am loading images to my app using this piece of code and it doesn't show the image correctly
C#
        Image [] img = new Image[100];
int i=0 ;
while (reader.Read())
        {

                string filePath = (string)reader["Path"];
                byte[] transactionContext = (byte[])reader["TransactionContext"];
                SqlFileStream sqlFileStream = new SqlFileStream(filePath, transactionContext, FileAccess.Read);
                byte[] data = new byte[(int)sqlFileStream.Length];
                sqlFileStream.Read(data, 0, Convert.ToInt32(sqlFileStream.Length));
                sqlFileStream.Close();
                MemoryStream ms = new MemoryStream();
                ms.Write(data, 0, data.Length);
                img[i] = byteArrayToImage(ms);
                ms.Seek(0, SeekOrigin.Begin);
                ms.Dispose();
                ms.Close();
                ms = null;
                data = null;
                i++;

        }

and byteArrayToImage method :
C#
public Image byteArrayToImage(MemoryStream ms)
{

    Image returnImage = Image.FromStream(ms);
    return returnImage;
}


but when i save them on the disk using this code it works perfectly :
C#
        Image [] img = new Image[100];
int i=0 ;
    while (reader.Read())
        {


            string filePath = (string)reader["Path"];
            byte[] transactionContext = (byte[])reader["TransactionContext"];
            SqlFileStream sqlFileStream = new SqlFileStream(filePath, transactionContext, FileAccess.Read);
            byte[] data = new byte[(int)sqlFileStream.Length];
            sqlFileStream.Read(data, 0, Convert.ToInt32(sqlFileStream.Length));
            sqlFileStream.Close();
            string filename = @"D:\Temp\pic" + i.ToString()+".jpeg";
            i++;
            System.IO.FileStream fs = new System.IO.FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Write);
            fs.Write(data, 0, data.Length);
            fs.Flush();
            fs.Close();
   }
Posted
Updated 12-Sep-13 21:10pm
v3

You forgot to rewind the stream to position 0 after the date is written, so you are trying to read past the current stream position, where there is no data. Please look at the code sample:
http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx[^].

Now, pay attention of the line memStream.Seek(0, SeekOrigin.Begin); you should do the same.

[EDIT]

However, your code looks redundant. Do this:
C#
System.Bitmap.Image myImage = new System.Bitmap.Image(sqlFileStream);

Your second sample (with file) confirms that your sqlFileStream contains correct data.

Please see: http://msdn.microsoft.com/en-us/library/z7ha67kw.aspx[^].

—SA
 
Share this answer
 
v3
Comments
Reza Oruji 13-Sep-13 2:28am    
thanks sergey. but it gives same result
Sergey Alexandrovich Kryukov 13-Sep-13 11:59am    
Well, please modify your code using "Improve question", comment the changed/added line(s). I don't say it's the only problem, but it was certainly one of the bugs.
Also, look properly at the code sample on the page I references, try to find out what else is missing...
—SA
Sergey Alexandrovich Kryukov 13-Sep-13 12:04pm    
Also, please see my answer update, after [EDIT].
—SA
[no name] 13-Sep-13 21:18pm    
Good suggestion.
Sergey Alexandrovich Kryukov 13-Sep-13 22:07pm    
Thank you.
—SA
Your example about file writing confuses the question.

Try this example


C# Image to Byte Array and Byte Array to Image Converter Class[^]
 
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