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

Discussion in 'ASP .Net' started by RobertH, Feb 14, 2004.

  1. RobertH

    RobertH Guest

    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();
    }
    RobertH, Feb 14, 2004
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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();
    > }
    Kevin Spencer, Feb 16, 2004
    #2
    1. Advertising

  3. 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" <> a écrit dans le message de
    news:...
    > 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();
    > }
    Patrice Scribe, Feb 16, 2004
    #3
  4. RobertH

    RobertH Guest

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

    Thanks,
    Robert
    RobertH, Feb 17, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. frank
    Replies:
    0
    Views:
    324
    frank
    Jul 11, 2003
  2. Steve - DND

    Click Event for TableRow?

    Steve - DND, Jul 31, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    497
    Steve - DND
    Jul 31, 2003
  3. lovaspillando
    Replies:
    0
    Views:
    1,006
    lovaspillando
    Aug 26, 2007
  4. Ivan Alameda Carballo
    Replies:
    0
    Views:
    474
    Ivan Alameda Carballo
    Aug 26, 2007
  5. Kevin Walzer

    Re: PIL (etc etc etc) on OS X

    Kevin Walzer, Aug 1, 2008, in forum: Python
    Replies:
    4
    Views:
    356
    Fredrik Lundh
    Aug 13, 2008
Loading...

Share This Page