473,416 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

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

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();
}
Nov 18 '05 #1
3 2205
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" <12***@hotmail.com> wrote in message
news:8o********************@comcast.com...
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();
}

Nov 18 '05 #2
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" <12***@hotmail.com> a écrit dans le message de
news:8o********************@comcast.com...
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();
}


Nov 18 '05 #3
That's what i thought. OK thanks very much for your insight. I did go the
seperate page route.
works great.

Thanks,
Robert
Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jim Mitchell | last post by:
I have the code snippet below. I fill a table of imagebuttons and would like to know which one was clicked to trigger the post back. Unfortunately, the Command event does not fire unless I load...
4
by: Ester | last post by:
I have an interior design of a living room with lights and aircond image. My task is to load the living room image without any light and aircond images as background image of a WebForm after that I...
1
by: Henke | last post by:
Hello, I have one ImageButton controls which I initialize in Page_Load and declare on class level. ImageButton save = new ImageButton(); save.ImageUrl = "save.gif" save.Click += new...
3
by: jens.buchta | last post by:
Hi! I'm using a DataGrid with a template column to display an Image inside of it. I'm hooking into its OnPrerender-Event to set the ImageURL-Property dynamically. Everything works just fine...
5
by: Varangian | last post by:
Hi there experts, Is there a way to convert a System.Drawing.Image to an ImageButton. Maybe using ChangeType I don't know! Thank you
1
by: mfr | last post by:
(appologies for re-post, but my no-spam alias was not configured correctly). Hi, I'm using VS2005 to create a web site. On one of my aspx pages I have an ImageButton that displays an image...
9
by: tshad | last post by:
Is there a way to display images (imageButtons or linkbuttons for instance) as a max size (200px by 50px) and not have it stretch the image? What I want to be able to do is limit the real estate...
0
by: Sully | last post by:
Hi Guys, I have this about 98% done and I cannot get it work properly. I have a Repeater bound to a MySQL DataSource, inside the Repeater I have an ImageButton, outside the repeater I have a...
3
by: premprakashbhati | last post by:
hi, good evening.. i am going to upload an image in a web form .....for that iam using HTML input(file) control and one web control button i.e., Upload_Button() here is the code ...its work fine...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.