Currently i have a aspx page called game.aspx, which contains various components and a button called 'show image' when i press this button i want the image to be retrieved from the sql database and show on game.aspx
At the moment I am using Response.BinaryWrite to display the image to a aspx page called image.aspx, code shown below:
namespace WebApplication.MemberPages
{
public partial class Image : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void GenerateImage()
{
Byte[] content = game.RetrieveImageBinary(); //Gets binary data from the database
System.Web.HttpContext.Current.Response.BinaryWrite(content);
System.Web.HttpContext.Current.Response.ContentType = "image/jpg";
}
}
In game.aspx i have a imagebox where the source is 'image.aspx'
Code:
This displays the image correctly, although when doing so all other components that where on the game.aspx page have all disappeared!!
My question is am i doing anything wrong? Is Response.BinaryWrite the best solution to the problem? (i am willing to take different approaches).
Due to the program being a web application I don’t have access to a picturebox(would have made life a lot easier) and i am not to keen on making a temporary file.
Thanks
Faesel Saeed
faeselPosted Nov 28, 2007, 10:53 AM
so if your looking for the answer you have come to the right place my friend.
the answer is http handlers
here is a copy and paste job of how i did mine:
web.config file:
(WebApplication.ImageHandler, WebApplication = class name, namespace)
Image tag:
(url is pointing to your http handler, im also passing a parameter to get a specific image)
http handlers .ashx code:
public class ImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
int _imageId;
if (context.Request["id"] != null) //Parameter being passed from the image tag
{
_imageId = int.Parse(context.Request["id"]);
}
else
{
throw new ArgumentException("No parameter specified");
}
Game game = new Game();
Byte[] image = game.RetrieveDB(_imageId);
context.Response.BinaryWrite(image);
}
public bool IsReusable
{
get
{
return false;
}
}
}
I hope this helps and is usefull because i went absolutely crazy for 2 week trying to find this out.
Faesel Saeed