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 :) )?
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
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.
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 = newMemoryStream())
{ img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
Ret = ms.ToArray();
strm = newMemoryStream(Ret); }
} catch
(Exception) { throw; }
}
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?
Member
5 Points
53 Posts
Write alternative image if no blob image in database
May 10, 2012 11:02 AM|BoxheadMonkey|LINK
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
Participant
1181 Points
405 Posts
Re: Write alternative image if no blob image in database
May 10, 2012 01:33 PM|mishra.bhupesh|LINK
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?
Participant
1181 Points
405 Posts
Re: Write alternative image if no blob image in database
May 10, 2012 09:26 PM|mishra.bhupesh|LINK
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.
Member
5 Points
53 Posts
Re: Write alternative image if no blob image in database
May 11, 2012 03:58 AM|BoxheadMonkey|LINK
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);
while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 4096);
}
Participant
1181 Points
405 Posts
Re: Write alternative image if no blob image in database
May 11, 2012 05:17 AM|mishra.bhupesh|LINK
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?
Member
5 Points
53 Posts
Re: Write alternative image if no blob image in database
May 11, 2012 05:40 AM|BoxheadMonkey|LINK
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
Participant
1181 Points
405 Posts
Re: Write alternative image if no blob image in database
May 11, 2012 05:55 AM|mishra.bhupesh|LINK
I thik it's best. Go with it. Because ashx file is working itself as url for Image control.