SQL image (jpeg) to Image or ImageButton or TableRow etc..

  • Thread starter Thread starter RobertH
  • Start date Start date
R

RobertH

Hello all. I have been hacking away trying to get a SQL image (jpeg) to
render in a control or table row Without using the Response.BinaryWrite....

I think i might be on the verge but need a little help.. below are 2
functions that i wrote to try and accomplish this.
the first one is what i would LIKE to do
the 2nd one works but it's not what i want to do..

here's the code :

private void renderPic(object sender, System.EventArgs e)
{
SqlConnection oConnect = new SqlConnection("server=192.x.x.x; uid=UID;
pwd=PWD; Database=DB");

//SqlCommand query = new SqlCommand("Select * from Pictures", oConnect);
string sql = "server=192.x.x.x; uid=uid; pwd=pwd; Database=database";

DataSet ds = new DataSet();

SqlConnection connection = new SqlConnection(sql);
SqlDataAdapter query = new SqlDataAdapter("Select * from Pictures",
oConnect);

connection.Open();
query.Fill(ds, "Pictures");
connection.Close();

if(ds.Tables["Pictures"].Rows.Count > 0)
{
for(int x = 0; x < ds.Tables["Pictures"].Rows.Count ; x++)
{
//Response.Clear();

byte[] y = (byte[])ds.Tables["Pictures"].Rows[x]["FileData"];

//y = (byte[])(ds.Tables["Pictures"].Rows[x]["FileData"]);
//Response.Write(y.ToString());
MemoryStream ms = new
MemoryStream((byte[])ds.Tables["Pictures"].Rows[x]["FileData"]);

//Response.AddHeader("Content-Type",
ds.Tables["Pictures"].Rows[x]["FileType"].ToString());
//Response.BinaryWrite(y);

//<img src="mydynamicimage.aspx">
TableRow tr1 = new TableRow();
TableCell tc1 = new TableCell();
Bitmap bmp = new Bitmap(ms);
//System.Drawing.Image tt = new System.Drawing.Image();

System.Web.UI.WebControls.Image fv = new
System.Web.UI.WebControls.Image();
fv.Attributes.Add("ContentType","image/jpeg");
fv.Equals(bmp);

//tc1.Text = @"<img src=""test.aspx"">";
tc1.Controls.Add(fv);

tr1.Cells.Add(tc1);

//placeholder "plIMG"
plIMG.Controls.Add(tr1);
}

}

}

private void testing(int id)
{
string connstr = "server=192.x.x.x; uid=uid; pwd=pwd; Database=database";
SqlConnection cnn = new SqlConnection(connstr);
SqlCommand cmd = new SqlCommand("select * from pictures where id=" + id,
cnn);
cnn.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
byte[] bindata = (byte[])dr.GetValue(3);
Response.AddHeader("Content-Type", "image/pjpeg");
Response.BinaryWrite(bindata);
cnn.Close();
}
 
Hard to say for sure from the snippets you posted, but it sure looks to me
like you're trying to embed your images in an HTML document, which just
isn't possible. An image tag is a reference to a separate file, as an HTML
file is plain text, and never will be anything but plain text. You need to
create a separate page or HttpHandler to render your images as files. Then
your image tags in the page can reference that file, and even pass
QueryString parameters to it, to generate the images.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

RobertH said:
Hello all. I have been hacking away trying to get a SQL image (jpeg) to
render in a control or table row Without using the Response.BinaryWrite....

I think i might be on the verge but need a little help.. below are 2
functions that i wrote to try and accomplish this.
the first one is what i would LIKE to do
the 2nd one works but it's not what i want to do..

here's the code :

private void renderPic(object sender, System.EventArgs e)
{
SqlConnection oConnect = new SqlConnection("server=192.x.x.x; uid=UID;
pwd=PWD; Database=DB");

//SqlCommand query = new SqlCommand("Select * from Pictures", oConnect);
string sql = "server=192.x.x.x; uid=uid; pwd=pwd; Database=database";

DataSet ds = new DataSet();

SqlConnection connection = new SqlConnection(sql);
SqlDataAdapter query = new SqlDataAdapter("Select * from Pictures",
oConnect);

connection.Open();
query.Fill(ds, "Pictures");
connection.Close();

if(ds.Tables["Pictures"].Rows.Count > 0)
{
for(int x = 0; x < ds.Tables["Pictures"].Rows.Count ; x++)
{
//Response.Clear();

byte[] y = (byte[])ds.Tables["Pictures"].Rows[x]["FileData"];

//y = (byte[])(ds.Tables["Pictures"].Rows[x]["FileData"]);
//Response.Write(y.ToString());
MemoryStream ms = new
MemoryStream((byte[])ds.Tables["Pictures"].Rows[x]["FileData"]);

//Response.AddHeader("Content-Type",
ds.Tables["Pictures"].Rows[x]["FileType"].ToString());
//Response.BinaryWrite(y);

//<img src="mydynamicimage.aspx">
TableRow tr1 = new TableRow();
TableCell tc1 = new TableCell();
Bitmap bmp = new Bitmap(ms);
//System.Drawing.Image tt = new System.Drawing.Image();

System.Web.UI.WebControls.Image fv = new
System.Web.UI.WebControls.Image();
fv.Attributes.Add("ContentType","image/jpeg");
fv.Equals(bmp);

//tc1.Text = @"<img src=""test.aspx"">";
tc1.Controls.Add(fv);

tr1.Cells.Add(tc1);

//placeholder "plIMG"
plIMG.Controls.Add(tr1);
}

}

}

private void testing(int id)
{
string connstr = "server=192.x.x.x; uid=uid; pwd=pwd; Database=database";
SqlConnection cnn = new SqlConnection(connstr);
SqlCommand cmd = new SqlCommand("select * from pictures where id=" + id,
cnn);
cnn.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
byte[] bindata = (byte[])dr.GetValue(3);
Response.AddHeader("Content-Type", "image/pjpeg");
Response.BinaryWrite(bindata);
cnn.Close();
}
 
IMO you could just have a page that uses Response.BinaryWrite (why not using
it when you need it ?) based on the id.

then wherever you need a particular image :

<img src="image.aspx?id=welcome">

<img src="image.aspx?id=logout"> etc will display the appropriate image....

As image.aspx returns a row image you preserve the ability to use this
wherever an image could be used (input, img, background or other HTML Tags
and properties).

Patrice

--

RobertH said:
Hello all. I have been hacking away trying to get a SQL image (jpeg) to
render in a control or table row Without using the Response.BinaryWrite....

I think i might be on the verge but need a little help.. below are 2
functions that i wrote to try and accomplish this.
the first one is what i would LIKE to do
the 2nd one works but it's not what i want to do..

here's the code :

private void renderPic(object sender, System.EventArgs e)
{
SqlConnection oConnect = new SqlConnection("server=192.x.x.x; uid=UID;
pwd=PWD; Database=DB");

//SqlCommand query = new SqlCommand("Select * from Pictures", oConnect);
string sql = "server=192.x.x.x; uid=uid; pwd=pwd; Database=database";

DataSet ds = new DataSet();

SqlConnection connection = new SqlConnection(sql);
SqlDataAdapter query = new SqlDataAdapter("Select * from Pictures",
oConnect);

connection.Open();
query.Fill(ds, "Pictures");
connection.Close();

if(ds.Tables["Pictures"].Rows.Count > 0)
{
for(int x = 0; x < ds.Tables["Pictures"].Rows.Count ; x++)
{
//Response.Clear();

byte[] y = (byte[])ds.Tables["Pictures"].Rows[x]["FileData"];

//y = (byte[])(ds.Tables["Pictures"].Rows[x]["FileData"]);
//Response.Write(y.ToString());
MemoryStream ms = new
MemoryStream((byte[])ds.Tables["Pictures"].Rows[x]["FileData"]);

//Response.AddHeader("Content-Type",
ds.Tables["Pictures"].Rows[x]["FileType"].ToString());
//Response.BinaryWrite(y);

//<img src="mydynamicimage.aspx">
TableRow tr1 = new TableRow();
TableCell tc1 = new TableCell();
Bitmap bmp = new Bitmap(ms);
//System.Drawing.Image tt = new System.Drawing.Image();

System.Web.UI.WebControls.Image fv = new
System.Web.UI.WebControls.Image();
fv.Attributes.Add("ContentType","image/jpeg");
fv.Equals(bmp);

//tc1.Text = @"<img src=""test.aspx"">";
tc1.Controls.Add(fv);

tr1.Cells.Add(tc1);

//placeholder "plIMG"
plIMG.Controls.Add(tr1);
}

}

}

private void testing(int id)
{
string connstr = "server=192.x.x.x; uid=uid; pwd=pwd; Database=database";
SqlConnection cnn = new SqlConnection(connstr);
SqlCommand cmd = new SqlCommand("select * from pictures where id=" + id,
cnn);
cnn.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
byte[] bindata = (byte[])dr.GetValue(3);
Response.AddHeader("Content-Type", "image/pjpeg");
Response.BinaryWrite(bindata);
cnn.Close();
}
 
That's what i thought. OK thanks very much for your insight. I did go the
seperate page route.
works great.

Thanks,
Robert
 
Back
Top