【问题标题】:Show large image when click on small image单击小图像时显示大图像
【发布时间】:2014-04-25 20:55:48
【问题描述】:

我试图在单击小图像时显示大图像。我有一个存储图像的 SQL 表,然后在 img 控件中显示该图像。现在我希望这张图片在用户点击它时以更大尺寸的弹出窗口打开。

我从数据库中检索图像并将其显示在 img 控件中的代码是:

<script type="text/javascript">
    $('#image1Large').hide().click(function() {
        $(this).hide();
    });

    $('#image1').click(function() {
        $('#image1Large').attr('src', this.src)
            .show()
            .offset({ top: 0, left: 0 });
    });
</script>

<img  runat="server" id="image1" alt="" src="" height="100" width="100"/>
<img  runat="server" id="image1Large" alt=""/>

protected void LoadImage1()
{
    SqlCommand cmd = new SqlCommand("sps_getimage", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@flag", 1);
    cmd.Parameters.AddWithValue("@ad_id", ad_id);
    con.Open();
    SqlDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess);
    if (reader.HasRows)
    {
        reader.Read();
        MemoryStream memory = new MemoryStream();
        long startIndex = 0;
        const int ChunkSize = 256;
        while (true)
        {
            byte[] buffer = new byte[ChunkSize];
            long retrievedBytes = reader.GetBytes(0, startIndex, buffer, 0, ChunkSize);
            memory.Write(buffer, 0, (int)retrievedBytes);
            startIndex += retrievedBytes;
            if (retrievedBytes != ChunkSize)
                break;
        }

        byte[] data = memory.ToArray();
        img1 = data;
        memory.Dispose();
        image1.Src = "data:image/png;base64," + Convert.ToBase64String(data);
    }
    con.Close();
}

【问题讨论】:

标签: javascript jquery asp.net


【解决方案1】:

这实际上不是 C# 或 SQL 问题,而是客户端问题。由于您已经检索到图像并且仅通过height="100" width="100" 属性调整它的大小,您所要做的就是以原始大小显示相同的图像。有很多方法可以做到这一点,这里是一个基本的:

添加另一个图像元素以容纳更大的图像:

<img  runat="server" id="image1" alt="" src="" height="100" width="100"/>
<img  runat="server" id="image1Large" />

并添加此代码以最初隐藏较大的图像并在单击时显示它:

$('#image1Large').hide().click(function(){
   $(this).hide();    
})


$('#image1').click(function(){
    $('#image1Large').attr('src', this.src)
                     .show()
                     .offset({top:0,left:0});    
})

这是一个演示:http://jsfiddle.net/QH5a8/

在现实生活中,您需要对此进行调整(可能需要对较大的图像进行额外的格式化和定位,可能会在 DIV 容器中显示它等),但这就是我们的想法。

【讨论】:

  • 在您的演示中它运行良好,但是当我应用它时它不起作用。我编辑问题请检查它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-08
  • 2013-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多