如何在.aspx表单和.ashx处理程序之间传递值

时间:2016-11-21 05:21:10

标签: c# asp.net ashx

我想根据用户输入(宽度,高度等)绘制图像

我在.aspx页面中有我的表单,但是我使用ashx处理程序绘制图像。

我有一个绘制图片的代码,但只是来自预先指定的值。

现在我要做的是从我的.aspx表单中获取值

的.aspx

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

.ashx.cs

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";
context.Response.BinaryWrite(bajt);
context.Response.Flush();

已经尝试过这个

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

        int __width = Convert.ToInt32(_width);

但值似乎为空

Some1请帮助我?

谢谢!

更新

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

1 个答案:

答案 0 :(得分:1)

您不是从.aspx页面发布(或获取)到您的.ashx处理程序,因为它不是它的工作原理。这就是context.Request.QueryString.Get("input_width.Text")不起作用的原因。此外,不需要&#34; .Text&#34;,只需&#34; input_width&#34;。

您需要将参数附加到对ashx的调用中:

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

并在你的处理程序中

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