locked
Write alternative image if no blob image in database RRS feed

  • Question

  • User-1344339330 posted

    I have an .ashx page which gets a blob image from a db and writes it to an asp:image control.  The problem I have is that sometimes the db has no blob, so i need to display a 'no image' image which is stored on the hard drive instead.  How do I do this (i'm new to streaming/writing etc so go easy :) )?

    cheers

    Thursday, May 10, 2012 11:02 AM

Answers

  • User143067745 posted

    if (strm == null)
    {
    Image img = Image.FromFile("C:\\inetpub\\wwwroot\\ASPNET\\images\\noimage.gif");
    byte[] Ret;
    try
    {
    using (MemoryStream ms = new MemoryStream())
    {
    img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
    Ret = ms.ToArray();
    strm = new MemoryStream(Ret);
    }
    }
    catch (Exception)
    {
    throw;
    }
    }

    Above part of the code is working fine, when strm is NULL.Whole code is working fine. What problem you are facing???

    I think it's not from coding side?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 11, 2012 5:17 AM

All replies

  • User143067745 posted

    I have an .ashx page which gets a blob image from a db and writes it to an asp:image control.  The problem I have is that sometimes the db has no blob, so i need to display a 'no image'


    If you want to show only message you can do it by setting  Image's 'AlternateText' property to 'No Image'. If  no image will be retrived it will display that message.

    If   exception(due to no blob) occuring then you can put your code in 

    Try

    {

    //image retrive coding goes here.

    }

    catch(NullReferenceException)

    {   }

    Were looking for above? or any thing else?

    Thursday, May 10, 2012 1:33 PM
  • User143067745 posted

    I am sorry I can't under stand you question so please Ignore my previous post.

    As you are retriving image from database, where you will retrive image by datareader["imageColum"] or by stroed procedure's output parameters, it will thorw NullReferenceException if no blob. So you can do:

    Try

    {

    //database retrival code

    }

    catch(NullReferenceException)

    {

    Image1.ImageUrl="Url of HDD";

    }

    Otherwise please put you coding effort here ,it may help to solve the issue.

    Thursday, May 10, 2012 9:26 PM
  • User-1344339330 posted

    Thanks for the reply.  It is not quite what I needed but you may have a better alternative anyway.

    I set the image on my page using:

    img.ImageUrl = "~/ShowImage.ashx?id=" + id;

    the ashx page retrieves the image blob from the db and then displays it.  I have writen a piece of code to display an alternative image if required - here is what I have (which works):

    Stream strm ShowMyImage(ImgSearch.GetImage(id)); //this just calls a method to get the byte array MemoryStream

    if (strm == null)
    {
    Image img = Image.FromFile("C:\\inetpub\\wwwroot\\ASPNET\\images\\noimage.gif");
    byte[] Ret;
    try
    {
    using (MemoryStream ms = new MemoryStream())
    {
    img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
    Ret = ms.ToArray();
    strm = new MemoryStream(Ret);
    }
    }
    catch (Exception)
    {
    throw;
    }
    }

    byte[] buffer = new byte[4096];
    int byteSeq = strm.Read(buffer, 0, 4096);
    w
    hile (byteSeq > 0)
    {
    context.Response.OutputStream.Write(buffer, 0, byteSeq);
    byteSeq = strm.Read(buffer, 0, 4096);
    }

    Friday, May 11, 2012 3:58 AM
  • User143067745 posted

    if (strm == null)
    {
    Image img = Image.FromFile("C:\\inetpub\\wwwroot\\ASPNET\\images\\noimage.gif");
    byte[] Ret;
    try
    {
    using (MemoryStream ms = new MemoryStream())
    {
    img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
    Ret = ms.ToArray();
    strm = new MemoryStream(Ret);
    }
    }
    catch (Exception)
    {
    throw;
    }
    }

    Above part of the code is working fine, when strm is NULL.Whole code is working fine. What problem you are facing???

    I think it's not from coding side?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 11, 2012 5:17 AM
  • User-1344339330 posted

    No problem, it works :)!  This was the code I was looking for - I found it after posting.  I was just wondered if there was a better way without having to convert the image to a byte and then send back to page?

    cheers

    Friday, May 11, 2012 5:40 AM
  • User143067745 posted

    I thik it's best. Go with it. Because ashx file is working itself as url for Image control.

    Friday, May 11, 2012 5:55 AM