fokz8 fokz8 - 7 months ago 45
ASP.NET (C#) Question

How to pass values between .aspx form and .ashx handler

I want to draw image, based on user input (width, height, etc...)

I have my form in .aspx page, but i'm drawing image using ashx handler.

I've got a code that draws image, but only from pre-assigned values.

Now what i want to do is get values from my .aspx form


<asp:TextBox ID="input_width" Width="125" Text="600" runat="server" ClientIDMode="Static"></asp:TextBox><br/>
<asp:TextBox ID="input_height" Width="125" Text="400" runat="server"></asp:TextBox>


int width = 600;
int height = 400;

Bitmap bmp = new Bitmap(width, height);

Graphics g = Graphics.FromImage((Image)bmp);
g.FillRectangle(Brushes.Red, 0f, 0f, bmp.Width, bmp.Height);

MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Png);

byte[] bajt = ms.ToArray();

context.Response.ContentType = "image/png";

Already tried this

string _width = context.Request.QueryString.Get("input_width.Text");

int __width = Convert.ToInt32(_width);

But the value seems to be null

Some1 please help me?

Thank you!


<a href="ImageGen.ashx">Press here</a><br />
<img src="ImageGen.ashx" width="600" height="400"/>


You are not posting (or getting) from your .aspx page to your .ashx handler, as that is not how it works. This is why context.Request.QueryString.Get("input_width.Text") does not work. Also there is no need for ".Text", just "input_width".

You need to append the parameters to your call to the ashx:

<img src="ImageGen.ashx?w=<%= input_width.Text %>&h=<%= input_height.Text %>" width="600" height="400"/>

and in your handler

string _width = context.Request.QueryString["w"];
int __width = Convert.ToInt32(_width);