Click here to Skip to main content
15,882,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

Here is my code, I am facing problem in converting image to byte format.
Also value is getting populated in database but when I want to show the image in gridview it shows nothing.

Please help....
I dont understand if this is the problem or anything else is the problem
here null value is getting populated from database....

 public byte[] ShowEmpImage(int wids)
{
    int flag = 0;

    SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["WATERMARKING"].ConnectionString);
    connection.Open();
    SqlCommand imid = new SqlCommand("SELECT IMAGE_ID from WATERMARKED_IMAGES", connection);
    SqlCommand wid = new SqlCommand("SELECT WID from WATERMARKED_IMAGES where FLAG='" + flag + "'", connection);
    wids = Convert.ToInt32(wid.ExecuteScalar());
    SqlCommand photoCommand = new SqlCommand("SELECT IMAGE.IMAGE_CONTENT  FROM   WATERMARKED_IMAGES INNER JOIN   IMAGE ON WATERMARKED_IMAGES.IMAGE_ID = IMAGE.IMAGE_ID where WATERMARKED_IMAGES.FLAG='" + flag + "'  ", connection);
       // Get bytes return from DB Command
    byte[] b = (byte[])photoCommand.ExecuteScalar();
    if (b.Length > 0)
    {
        return b;
    }



}
Posted
Updated 19-Mar-11 1:58am
v6
Comments
Abdul Quader Mamun 19-Mar-11 6:47am    
Too big
RKT S 19-Mar-11 7:35am    
now i have shorten it...:)
Dalek Dave 19-Mar-11 7:04am    
Edited for Grammar and Readability.
Toli Cuturicu 19-Mar-11 16:26pm    
Only an 1x8 monochrome image can be converted to a byte?!

You posted way too much code. I didn't bother to look through it. Using google, I found this code in a CP article:

C#
public byte[] ImageToByteArray( System.Drawing.Image p_ImageIn )
{
    byte[] aRet = null;
    using ( System.IO.MemoryStream oMS = new System.IO.MemoryStream() )
    {
        p_ImageIn.Save( oMS, System.Drawing.Imaging.ImageFormat.Gif );
        aRet = oMS.ToArray();
    }
    // Possibly dispose image too here, if no extra manipulation is needed
    // Remember, Image is sent by ref, so if you dispose it inside method it will 
    // not be available outside either.
    // p_ImageIn.Dispose();
    return aRet;
}
 
Share this answer
 
v2
Comments
RKT S 19-Mar-11 10:16am    
john i have shorten the code now....plz have a look...here null value is getting populated in field b....
You should study these link
 
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