asp.net预览图片

Aspx code

<table>

        <tr>

            <td class="style3">

                <asp:Label ID="Label1" runat="server" Text="Photo upload" />

            </td>

            <td class="style4">

                <asp:FileUpload runat="server" ID="PhotoUpload" />

            </td>

            <td class="style4">

                <asp:Button runat="server" ID="btnPhotoPreview" Text="Preview" />

            </td>

            <td class="style1">

                <asp:Image runat="server" ID="ImagePreview" Height="164px" Width="125px" />

            </td>

        </tr>

    </table>

 

2.添加一个ID为btnPhotopreview的Button按钮

<asp:Button runat="server" OnClick="btnPreview_Click" ID="btnPhotoPreview" Text="Preview" />

3

.添加一个 .ashx文件,ashx是什么文件?

.ashx 文件用于写web handler的。.ashx文件与.aspx文件类似,可以通过它来调用HttpHandler类,它免去了普通.aspx页面的控件解析以及页面处理的过程。其实就是带HTML和C#的混合文件。
.ashx文件适合产生供 浏览器处理的、不需要 回发处理的数据格式,例如用于生成 动态图片动态文本等内容。
<%@ WebHandler Language="C#" Class="ImageHandler" %>
 
using System;
using System.Web;
 
public class ImageHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
   
    public void ProcessRequest (HttpContext context) {
       //Checking whether the imagebytes session variable have anything else not doing anything
 
        if ((context.Session["ImageBytes"]) != null)
        {
            byte[] image = (byte[])(context.Session["ImageBytes"]);
            context.Response.ContentType = "image/JPEG";
            context.Response.BinaryWrite(image);
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
 
}

4.双击button按钮注册事件。

protected void btnPreview_Click(object sender, EventArgs e)

    {

        Session["ImageBytes"] = PhotoUpload.FileBytes;

        ImagePreview.ImageUrl = "~/ImageHandler.ashx";

    }

 参考:

http://www.c-sharpcorner.com/uploadfile/1a81c5/previewing-image-in-asp-net-image-control/

 

 

转载于:https://www.cnblogs.com/code-charmer/p/4046317.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值