我正在从asp.net处理程序加载图像,如下所示>处理程序使用Web服务获取图像。处理程序返回bytes []数据。
<asp:Image ID="image1" ImageUrl="~/Handler1.ashx?id=1" runat="server"></asp:Image>
现在,我在网页上有图像。我在网页中保存了图像按钮。单击按钮时,我想将此图像保存为本地目录(c:\ images)作为jpeg图像。怎么做到这一点?
答案 0 :(得分:0)
我认为你必须得到图像控制的路径
点击事件内的
作为
string path = image1.ImageUrl;
然后将其保存到文件
希望这可以帮助你
答案 1 :(得分:0)
public class ImageHandler : IHttpHandler
{
Users objimage = new Users();
public void ProcessRequest(HttpContext context)
{
Int32 theID;
if (context.Request.QueryString["Id"] != null)
theID = Convert.ToInt32(context.Request.QueryString["Id"]);
else
throw new ArgumentException("No parameter specified");
HttpResponse r = context.Response;
r.ContentType = "image/jpeg";
context.Response.ContentType = "image/jpeg";
objimage.imageId = theID; //select image using id
Stream strm = objimage.SelectImageByID(theID);
byte[] buffer = new byte[2048];
if (strm != null)
{
int byteSeq = strm.Read(buffer, 0, 2048);
while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 2048);
}
}
else
{
r.WriteFile("~/img/img-profile.jpg");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
答案 2 :(得分:0)
尝试使用以下代码处理程序,它返回图像数据:
public class ImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
bool isSave = !String.IsNullOrEmpty(context.Request["save"]);
int imgId;
if (String.IsNullOrEmpty(context.Request["id"]) || Int32.TryParse(context.Request["id"], out imgId))
throw new ArgumentException("No parameter specified");
byte[] imageData = LoadImageFromWebService(imgId); //load image from web service
context.Response.ContentType = "image/jpeg";
context.Response.AddHeader("Content-Length", imageData.Length.ToString());
context.Response.AddHeader("Content-Disposition", String.Format("{0} filename=image.jpeg", isSave ? "attachment;" : String.Empty));
context.Response.BinaryWrite(imageData);
context.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
更改asp.net页面如下:
<asp:Image ID="Image1" ImageUrl="~/Handler1.ashx?id=1" runat="server"></asp:Image>
<asp:ImageButton ID="ImageButton1" OnClick="OnImageClick" runat="server" />
在这种情况下,页面中的OnImageClick
处理程序将为:
protected void OnImageClick(object sender, ImageClickEventArgs e)
{
Response.Redirect("~/Handler1.ashx?id=1&save=1");
}