定义无图片时显示默认图片,大小

<% @ Control Language = " C# "  AutoEventWireup = " true "  CodeFile = " ImageThumbnail.ascx.cs "  Inherits = " ImageThumbnail "   %>
< asp:Image ID = " Image1 "  runat = " server "  CssClass = " photo "  BorderWidth = " 1 "   />
 1 using  System;
 2
 3 public  partial  class  ImageThumbnail : System.Web.UI.UserControl
 4 {
 5    public object PhotoID
 6    {
 7        get
 8        {
 9            object o = ViewState["PhotoID"];
10            return (o != null? (int)o : 0;
11        }

12        set
13        {
14            ViewState["PhotoID"= (value != null && value != DBNull.Value) ? Convert.ToInt32(value) : 0;
15        }

16    }

17    public ImageSizes ImageSize
18    {
19        get
20        {
21            object o = ViewState["ImageSize"];
22            return (o != null? (ImageSizes)o : ImageSizes.Thumb;
23        }

24        set
25        {
26            ViewState["ImageSize"= value;
27        }

28    }

29    public enum ImageSizes
30    {
31        Large = 0,
32        Thumb = 1,
33        FullSize = 2
34    }

35    public string NoPhotoImg
36    {
37        get
38        {
39            object o = ViewState["NoPhotoImg"];
40            return (o != null? (string)o : null;
41        }

42        set
43        {
44            ViewState["NoPhotoImg"= value;
45        }

46    }

47    protected void Page_PreRender(object sender, System.EventArgs e)
48    {
49        if (Convert.ToInt32(PhotoID) == 0)
50        {
51            if (NoPhotoImg != null)
52            {
53                Image1.ImageUrl = NoPhotoImg;
54            }

55            else
56            {
57                Image1.Visible = false;
58            }

59        }

60        else
61        {
62            Image1.ImageUrl = "ImageFetch.ashx?Size=" + Convert.ToInt32(ImageSize) + "&ImageID=" + Convert.ToString(PhotoID);
63        }

64    }

65    protected void Page_Load(object sender, EventArgs e)
66    {
67
68    }

69}

70
ImageFetch.ashx
 1 <% @ WebHandler Language = " C# "  Class = " ImageFetch "   %>
 2
 3 using  System;
 4 using  System.Web;
 5 using  System.Data.SqlClient;
 6 using  System.Data;
 7 using  System.IO;
 8
 9 public   class  ImageFetch : IHttpHandler
10 {
11    const int BUFFERSIZE = 1024;
12
13    public bool IsReusable
14    {
15        get
16        {
17            return true;
18        }

19    }

20
21    public void ProcessRequest(HttpContext context)
22    {
23        HttpResponse response = context.Response;
24        HttpRequest request = context.Request;
25        response.ContentType = "image/jpeg";
26        response.Cache.SetCacheability(HttpCacheability.Public);
27        response.BufferOutput = false;
28        writeSingleImage(Convert.ToInt32(request.QueryString["ImageID"]), Convert.ToInt32(request.QueryString["Size"]), response.OutputStream);
29        response.End();
30    }

31
32    public void writeSingleImage(int ImageID, int size, Stream output)
33    {
34        string cxnstr = System.Configuration.ConfigurationManager.ConnectionStrings["ClubSiteDB"].ConnectionString;
35        SqlConnection connection = new SqlConnection(cxnstr);
36        string query;
37        if (size == 0)
38        {
39            query = "select largeimage from images where id=@item_id";
40        }

41        else if (size == 1)
42        {
43            query = "select thumbimage from images where id=@item_id";
44        }

45        else if (size == 2)
46        {
47            query = "select origimage from images where id=@item_id";
48        }

49        else
50        {
51            query = "select largeimage from images where id=@item_id";
52        }

53        SqlCommand command = new SqlCommand(query, connection);
54        SqlParameter param0 = new SqlParameter("@item_id", SqlDbType.Int);
55        param0.Value = ImageID;
56        command.Parameters.Add(param0);
57        connection.Open();
58
59        byte[] d = ((byte[])(command.ExecuteScalar()));
60        output.Write(d, 0, d.Length);
61        connection.Close();
62    }

63}

转载于:https://www.cnblogs.com/zwl12549/archive/2007/12/14/995042.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值