Click here to Skip to main content
16,012,759 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i write this code for retriving image from sql server bt its nt working its shows error parameter is not valid in image.fromstream.(ms,true).

adap = new SqlDataAdapter("Select  Picture from visitor1 where Pass_Code=2", con);

DataSet DS = new DataSet();
adap.Fill(DS, "visitor1");

byte[] imageData = (byte[])DS.Tables["visitor1"].Rows[0]["Picture"];

Image newImage;

using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
{
    ms.Write(imageData, 0, imageData.Length);


    newImage = Image.FromStream(ms,true);
}

please help

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 13-Jul-11 0:45am
v2

Try taking the boolean parameter off: you may not have color information embedded in your image in the db.
newImage = Image.FromStream(ms);


DOH! I missed that the first time round: Add a line:

using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
{
    ms.Write(imageData, 0, imageData.Length);

    ms.Seek(0,0);   //<<<<-----  Add this line.

    newImage = Image.FromStream(ms,true);
}
Otherwise the stream is past the data when you try to read it into the image...
Or just do this:
using (MemoryStream ms = new MemoryStream(imageData))
   {
   newImage = Image.FromStream(ms);
   }




"K THANKS I WANT TO KNOW HOW BYTE VALUE IS PASS TO QUERY"


Try:
using (SqlCommand com = new SqlCommand("INSERT INTO myTable (picture) VALUES (@PIC)", con))
    {
    com.Parameters.AddWithValue("@PIC", MyData);
    com.ExecuteNonQuery();
    }
Obviously you need to expand that to cover all your other fields as well, but that typing I will leave to you! :laugh:
 
Share this answer
 
v3
Comments
advancedansh 13-Jul-11 6:51am    
i m also use it without boolean parameter but its nt working
OriginalGriff 13-Jul-11 7:02am    
Answer updated
OriginalGriff 13-Jul-11 7:21am    
Well, there is your problem.
You have stored rubbish to your database.
...txtcity.Text + "','Image.value.toString()','" + txttomeet.Text...
Stores the text "Image.value.ToString()" as the Picture parameter.
You need to change all of that stuff, for lots of reasons, to use parametrized queries.
Partly so it can stand a chance of working, partly so your first user doesn't accidentally or deliberately destroy your database.

And no, I'm not joking: Google "SQL Injection Attack".
advancedansh 13-Jul-11 7:41am    
K THANKS I WANT TO KNOW HOW BYTE VALUE IS PASS TO QUERY
OriginalGriff 13-Jul-11 8:43am    
Answer updated - and don't SHOUT! Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.
hey! advancedansh as I get your reply to OriginalGriff to know how to pass Bye to database. if you are curious to know how to pass Image as a Byte value in a database then you can try given code

Make a function Name ReadFile which is responsible to convert Image data to raw byte[] format
byte[] ReadFile(string sPath)
        {
            //Initialize byte array with a null value initially.
            byte[] data = null;
            //Use FileInfo object to get file size.
            FileInfo fInfo = new FileInfo(sPath);
            long numBytes = fInfo.Length;
            //Open FileStream to read file
            FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
            //Use BinaryReader to read file stream into byte array.
            BinaryReader br = new BinaryReader(fStream);
            //When you use BinaryReader, you need to supply number of bytes to read from file.
            //In this case we want to read entire file. So supplying total number of bytes.
            data = br.ReadBytes((int)numBytes);
            return data;
        }

or you can use this method as

byte[] imageData = ReadFile("YourImagepath");
          string query = "Insert into TableName(Images) values(@Images)";
           SqlCommand cmd = new SqlCommand(query, ClassLibrary.myConnection.GetConnection());
           //SqlParameter sqlp = new SqlParameter();
           cmd.Parameters.Add(new SqlParameter("@Images", (object)imageData));
           cmd.ExecuteNonQuery();
 
Share this answer
 
1.first to create Handler.aspx in C# and write that following codes.

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Data.SqlClient;

public class Handler : IHttpHandler {

    SqlConnection con = new SqlConnection("Data Source=PRAKASAM-PC\\SQLEXPRESS;Initial Catalog=test1;Integrated Security=True");
    
    public void ProcessRequest (HttpContext context) 
    {
        con.Open();

        SqlCommand cmd = new SqlCommand();

        cmd.CommandText = "select pphoto from prode where pk = @pk";
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Connection = con;

        SqlParameter ImageId = new SqlParameter("@pk", System.Data.SqlDbType.VarChar);
        ImageId.Value = context.Request.QueryString["imageid"];
        cmd.Parameters.Add(ImageId);
        SqlDataReader dreader = cmd.ExecuteReader();
        dreader.Read();
        context.Response.BinaryWrite((byte[])dreader["pphoto"]);
        dreader.Close();
        con.Close();

    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}


2.To call that Handler in your filename.aspx

C#
int pk = int.Parse(ds.Tables[0].Rows[0][0].ToString());

foreach (GridViewRow rr in GridView1.Rows)
{
    Image ss = (Image)rr.FindControl("Image1");
    ss.ImageUrl = "Handler.ashx?imageid=" + pk;
    pk++;
}
 
Share this answer
 
v2
Comments
RaviRanjanKr 13-Jul-11 8:57am    
Always wrap your code in "pre" tag. :)

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