开发者

How to read binary column in database into image on asp.net page?

开发者 https://www.devze.com 2022-12-23 03:16 出处:网络
I want to read from database where I\'ve stored a image in binary field and display a image. while (reader.Read())

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?


 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;

        }

    }

}


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.


Usually setting the Stream position to 0 does the trick:

Stream s = new MemoryStream(imgarr);
s.Position = 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).

0

精彩评论

暂无评论...
验证码 换一张
取 消