Reputation:
i want to make asp.net page return image to the browser here is the code
protected void Page_PreRender(object sender, EventArgs e)
{
Response.Clear();
string iName = Request.QueryString.Get("ImageName") ;
Bitmap bmp = new Bitmap(Server.MapPath("a.png"));
Response.ContentType = "image/png";
bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
bmp.Dispose();
Response.End();
}
i works fine off line i mean when running it in visual studio but when running in godady hosting environment it gives this error
A generic error occurred in GDI+.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[ExternalException (0x80004005): A generic error occurred in GDI+.]
System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams) +471002
System.Drawing.Image.Save(Stream stream, ImageFormat format) +36
PageReturnImageDeals.MyImage.Page_PreRender(Object sender, EventArgs e) +191
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
System.Web.UI.Control.OnPreRender(EventArgs e) +92
System.Web.UI.Control.PreRenderRecursiveInternal() +83
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +974
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18055
Upvotes: 0
Views: 149
Reputation: 22074
1) Why you retrieve iName when you don't use it ?
2) HttpHandler is better for this kind of action
3) there is no need to take any graphic action over file - its just plain transfer
Handler.ashx.cs:
public class Handler : IHttpHandler {
public bool IsReusable {
get { return false; }
}
public void ProcessRequest(HttpContext context) {
var realPath = Server.MapPath("a.png");
context.Response.ContentType = "image/png";
context.Response.WriteFile(realPath);
}
}
EDIT:
If you insist on having it as Page:
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
var realPath = Server.MapPath("a.png");
Response.ContentType = "image/png";
Response.WriteFile(realPath);
}
Upvotes: 1