查看原图与缩略图(C#)
思路
上传图片:
每当上传一个图片的时候,就根据图片在数据库的ID创建一个文件夹存放当前图片的的文件夹,把原来的图片按照一定的比例进行缩小,缩小的图片叫4_+图片名称,没有缩小的图片,也就是原图,不做任何修饰。
当查询的时候就根据ID,拼凑出一个路径,然后根据路径进行绑定图片。如果是缩略图,则查询4-+图片名称,原图就直接是原来图片的名称。
实现效果,当点击小图的时候,可以查看大图。
这是在页面显示的图片:
当点击小图时,查看到的大图片。
下面是具体的实现:页面的代码
1 <asp:DataList ID="dsgetall" runat="server" OnSelectedIndexChanged="dsgetall_SelectedIndexChanged"
2 RepeatColumns="3" Width="100%">
3 <ItemTemplate>
4 <table cellpadding="0" cellspacing="0" border="0">
5 <tr>
6 <td>
7 <asp:ImageButton ID="ImageButton1" runat="server" CommandArgument='<%# getArgument(Eval("url"),Eval("Name")) %>'
8 Height="112px" ImageUrl='<%# this.LogImg2(Eval("url"),Eval("Name"))%>' OnCommand="ImageButton1_Command"
9 Width="150px" /></td>
10
11 </tr>
12 <tr>
13 <td align="left" rowspan="2">
14 <%# DataBinder.Eval(Container.DataItem, "DES")%></td>
15 </tr>
16 <tr>
17 </tr>
18 </table>
19 </ItemTemplate>
20 </asp:DataList>
2 RepeatColumns="3" Width="100%">
3 <ItemTemplate>
4 <table cellpadding="0" cellspacing="0" border="0">
5 <tr>
6 <td>
7 <asp:ImageButton ID="ImageButton1" runat="server" CommandArgument='<%# getArgument(Eval("url"),Eval("Name")) %>'
8 Height="112px" ImageUrl='<%# this.LogImg2(Eval("url"),Eval("Name"))%>' OnCommand="ImageButton1_Command"
9 Width="150px" /></td>
10
11 </tr>
12 <tr>
13 <td align="left" rowspan="2">
14 <%# DataBinder.Eval(Container.DataItem, "DES")%></td>
15 </tr>
16 <tr>
17 </tr>
18 </table>
19 </ItemTemplate>
20 </asp:DataList>
.CS
代码
protected void Page_Load(object sender, EventArgs e)
{
bind();
}
public void bind()
{
//得到当前企业的ID--从另个页面传过来的ID
//qiyeid
string qiyeid = Request.QueryString["qiyeid"].ToString();
ccwu.DAL.Img_en en = new ccwu.DAL.Img_en();
this.dsgetall.DataSource = en.GetList(" ENID=" + qiyeid.ToString());
this.dsgetall.DataBind();
}
//从模板里进入的,当点击图片按钮的时候查看详细
protected void ImageButton1_Command(object sender, CommandEventArgs e)
{
string argument = e.CommandArgument.ToString();
string picturespath = Convert.ToString(argument.Split('#')[0].ToString());
string picturename = Convert.ToString(argument.Split('#')[1].ToString());
string str = ".." + picturespath + "/" + picturename;
//调用方法,查看原图
JavaScriptLocationHref(str, this);
}
/// <summary>
/// 绑定数据,当点击图片的时候获取这个值
/// </summary>
/// <returns></returns>
protected string getArgument(object id, object isAuditing)
{
return id.ToString() + "#" + isAuditing.ToString();
}
/// <summary>
/// //在页面绑定产生一个路径 //ImageUrl要绑定的路径
/// </summary>
/// <param name="picturespath">图片存在的路径</param>
/// <param name="picturename">图片名称</param>
/// <returns>静态字段,返回一个静态字段在页面进行显示</returns>
public string LogImg2(object picturespath, object picturename)
{
string urlString = "";
if (picturespath != null && picturename != null)
{
if (picturespath.ToString().Length > 0 && picturename.ToString().Length > 0)
{
urlString = "~" + picturespath.ToString() + "/" + "4_" + picturename.ToString();
}
}
//如果没有图片,则显示自己定义的图片
if (picturespath != null && picturename == null)
{
if (picturespath.ToString().Length > 0 && picturename.ToString().Length == 0)
{
urlString = "~/ccwu/images/BackImage/nopic.bmp";
}
}
return urlString;
}
public static void JavaScriptLocationHref(string url, Page page)
{
#region
string js = @"<Script language='JavaScript'>
window.location.replace('{0}');
</Script>";
js = string.Format(js, url);
if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "JavaScriptLocationHref"))
{
page.ClientScript.RegisterStartupScript(page.GetType(), "JavaScriptLocationHref", js);
}
#endregion
}
{
bind();
}
public void bind()
{
//得到当前企业的ID--从另个页面传过来的ID
//qiyeid
string qiyeid = Request.QueryString["qiyeid"].ToString();
ccwu.DAL.Img_en en = new ccwu.DAL.Img_en();
this.dsgetall.DataSource = en.GetList(" ENID=" + qiyeid.ToString());
this.dsgetall.DataBind();
}
//从模板里进入的,当点击图片按钮的时候查看详细
protected void ImageButton1_Command(object sender, CommandEventArgs e)
{
string argument = e.CommandArgument.ToString();
string picturespath = Convert.ToString(argument.Split('#')[0].ToString());
string picturename = Convert.ToString(argument.Split('#')[1].ToString());
string str = ".." + picturespath + "/" + picturename;
//调用方法,查看原图
JavaScriptLocationHref(str, this);
}
/// <summary>
/// 绑定数据,当点击图片的时候获取这个值
/// </summary>
/// <returns></returns>
protected string getArgument(object id, object isAuditing)
{
return id.ToString() + "#" + isAuditing.ToString();
}
/// <summary>
/// //在页面绑定产生一个路径 //ImageUrl要绑定的路径
/// </summary>
/// <param name="picturespath">图片存在的路径</param>
/// <param name="picturename">图片名称</param>
/// <returns>静态字段,返回一个静态字段在页面进行显示</returns>
public string LogImg2(object picturespath, object picturename)
{
string urlString = "";
if (picturespath != null && picturename != null)
{
if (picturespath.ToString().Length > 0 && picturename.ToString().Length > 0)
{
urlString = "~" + picturespath.ToString() + "/" + "4_" + picturename.ToString();
}
}
//如果没有图片,则显示自己定义的图片
if (picturespath != null && picturename == null)
{
if (picturespath.ToString().Length > 0 && picturename.ToString().Length == 0)
{
urlString = "~/ccwu/images/BackImage/nopic.bmp";
}
}
return urlString;
}
public static void JavaScriptLocationHref(string url, Page page)
{
#region
string js = @"<Script language='JavaScript'>
window.location.replace('{0}');
</Script>";
js = string.Format(js, url);
if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "JavaScriptLocationHref"))
{
page.ClientScript.RegisterStartupScript(page.GetType(), "JavaScriptLocationHref", js);
}
#endregion
}