0

I want to read from database where I've stored a image in binary field and display a image.

 while (reader.Read())
    {
        byte[] imgarr = (byte[])reader["file"];

        Stream s = new MemoryStream(imgarr);
        System.Drawing.Image image = System.Drawing.Image.FromStream(s);
        Graphics g = Graphics.FromImage(image);
        g.DrawImage(image, new Point(400, 10));
        image.Save(Response.OutputStream, ImageFormat.Jpeg);
        g.Dispose();
        image.Dispose();
    }
    con.Close();

This piece of code doesn't work:

 System.Drawing.Image image = System.Drawing.Image.FromStream(s);

I tried the code from this article. And I got the same mistake " the parameter is not valid ". Maybe I'm not aware of something, some setting in sql server or webconfig or sth else. Anyone else who has experience from fetching images from database?

The parameter is not valid is the error message. db table contains data. What am I doing wrong?

3
  • 2
    "Doesn't work" isn't a very detailed diagnosis. What's actually happening? Is an exception being thrown? If so, what and where is it being thrown?
    – Jon Skeet
    Commented Mar 11, 2010 at 21:08
  • Have you checked whether imgarr holds any data? Commented Mar 11, 2010 at 21:08
  • Always wrap IDisposable objects in "using" statments. Commented Mar 11, 2010 at 21:56

4 Answers 4

3
 public Byte[] Ret_image(Int32 id)

    {

        SqlCommand cmd = new SqlCommand();

        cmd.CommandText = "select*from tbimage where imageid=@id";

        cmd.Connection = con;

        cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;

        SqlDataReader dr = cmd.ExecuteReader();

        dr.Read();

        Byte[] ar = (Byte[])(dr[1]);

        dr.Close();

        cmd.Dispose();

        return ar;

    }

    protected void btnRetImage_Click(object sender, EventArgs e)

    {

        try

        {

            Byte[] ar = Ret_image(Convert.ToInt32(TextBox2.Text));

            String st = Server.MapPath("abc.jpg");

            FileStream fs = new FileStream(st, FileMode.Create, FileAccess.Write);

            fs.Write(ar, 0, ar.Length);

            fs.Close();

            Image1.Visible = true;

            Image1.ImageUrl = "abc.jpg";

            Label1.Visible = false;
}
catch(Expration exp)
{
 Label1.Text = "Wrong Image id";

            Label1.Visible = true;

            Image1.Visible = false;

        }

    }

}
0
0

The line that you say doesn't work looks correct to me. Since you didn't say what sort of image, perhaps it is an unsupported format.

Otherwise, I'll hazard a guess that the actual problem is at

byte[] imgarr = (byte[])reader["file"];

and the byte array doesn't contain the data you think it should.

0

Usually setting the Stream position to 0 does the trick:

Stream s = new MemoryStream(imgarr);
s.Position = 0;
0

You might want to check out dbfile the StaticSql.cs file has some of what you are looking for.

conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.Read())
{
    ... other code 

    byte[] filebytes = (byte[])rdr[FILE_STREAM];
    result.FileBytes = new byte[filebytes.Length];
    Array.Copy(filebytes, result.FileBytes, filebytes.Length);
}

The DBFile.cs has the file writing code

response.ContentType = file.ContentType;

byte[] buffer = new byte[file.ByteCount];
file.Read(buffer, 0, file.ByteCount);
response.BinaryWrite(buffer);

As long as you set the content type on the response and you are writing the file out in the response you shouldn't need to create the image and then save it (unless you are doing some editting of it that is).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.