一、逻辑实现:将数据库中的二进制TIFF图片读出并分页显示在页面上。
1.显示界面
class="code_img_closed" src="/Upload/Images/2014120316/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('72a5abc4-fae0-44a2-8f3e-221f4b9bee30',event)" src="/Upload/Images/2014120316/2B1B950FA3DF188F.gif" alt="" />public FrameDimension MyGuid; public static int MyPos = 0; public static int totalPage = 0; public static MemoryStream stream; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string username = Request.QueryString["username"]; string sql = string.Format("select top(1) image from Image_Table where image_code=( select new_sjbh from new_ksy_dzpz where new_ksy_dzpzid='{0}' ) ", username); DataTable dt = SqlHelper.GetTable(sql); if (dt.Rows.Count > 0) { Image1.ImageUrl = "ShowImage.aspx?username=" + username + "&pagecount=0"; stream = new MemoryStream((byte[])dt.Rows[0]["image"]); // Bitmap bt = new Bitmap(Server.MapPath(Image1.ImageUrl)); Bitmap bt = new Bitmap(stream); Guid gud = (Guid)bt.FrameDimensionsList.GetValue(0); FrameDimension fds = new FrameDimension(gud); totalPage = bt.GetFrameCount(fds);//获取页数 this.LabelPageCount.Text = totalPage.ToString(); this.LabelCurrentPage.Text = "1"; if (totalPage == 1) { this.LinkButtonPreviousPage.Enabled = false;//上一页只读 this.LinkButtonFirstPage.Enabled = false;//首页只读 this.LinkButtonNextPage.Enabled = false;//下一页只读 this.LinkButtonLastPage.Enabled = false;//尾页只读 this.btnGo.Enabled = false; } } this.LinkButtonPreviousPage.Enabled = false;//上一页只读 this.LinkButtonFirstPage.Enabled = false;//首页只读 } } //下载 protected void Button1_Click(object sender, EventArgs e) { string username = Request.QueryString["username"]; string sql = string.Format("select top(1) image,name from Image_Table where image_code=( select new_sjbh from new_ksy_dzpz where new_ksy_dzpzid='{0}' ) ", username); DataTable dt = SqlHelper.GetTable(sql); if (dt.Rows.Count > 0) { byte[] bytes = (byte[])dt.Rows[0]["image"]; //下载单个凭证 Response.Buffer = true; Page.Response.Clear();//清除缓冲区所有内容 Page.Response.ContentType = "application/octet-stream"; Page.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(dt.Rows[0]["name"].ToString()) + ".tiff"); Response.BinaryWrite(bytes); Response.Flush(); Response.End(); } } //下一页 protected void LinkButtonNextPage_Click(object sender, EventArgs e) { int page = Convert.ToInt32(this.LabelCurrentPage.Text); this.LabelCurrentPage.Text = Convert.ToString(page + 1); fenyeshow(page); } //上一页 protected void LinkButtonPreviousPage_Click(object sender, EventArgs e) { int page = Convert.ToInt32(this.LabelCurrentPage.Text) - 2; this.LabelCurrentPage.Text = Convert.ToString(page + 1); fenyeshow(page); } //首页 protected void LinkButtonFirstPage_Click(object sender, EventArgs e) { int page = 0; this.LabelCurrentPage.Text = "1"; fenyeshow(page); } //尾页 protected void LinkButtonLastPage_Click(object sender, EventArgs e) { int page = Convert.ToInt32(this.LabelPageCount.Text) - 1; this.LabelCurrentPage.Text = this.LabelPageCount.Text; fenyeshow(page); } //Go protected void btnGo_Click(object sender, EventArgs e) { int page = 0; if (this.txtNewPageIndex.Text != "") { int pagecount = Convert.ToInt32(this.LabelPageCount.Text); int txt = Convert.ToInt32(this.txtNewPageIndex.Text); if (txt <= pagecount && txt > 0) { page = Convert.ToInt32(this.txtNewPageIndex.Text) - 1; this.LabelCurrentPage.Text = this.txtNewPageIndex.Text; } } if (page == 0) { this.LabelCurrentPage.Text = "1"; } fenyeshow(page); } //显示页面 public void fenyeshow(int pagecount) { string username = Request.QueryString["username"]; Image1.ImageUrl = "ShowImage.aspx?username=" + username + "&pagecount=" + pagecount.ToString(); if (this.LabelCurrentPage.Text == "1") { this.LinkButtonPreviousPage.Enabled = false;//上一页只读 this.LinkButtonFirstPage.Enabled = false;//首页只读 this.LinkButtonNextPage.Enabled = true; this.LinkButtonLastPage.Enabled = true; } if (this.LabelCurrentPage.Text == this.LabelPageCount.Text) { this.LinkButtonNextPage.Enabled = false;//下一页只读 this.LinkButtonLastPage.Enabled = false;//尾页只读 this.LinkButtonPreviousPage.Enabled = true; this.LinkButtonFirstPage.Enabled = true; } }Default.aspx
2.输出界面
public static MemoryStream stream; protected void Page_Load(object sender, EventArgs e) { string username = Request.QueryString["username"]; int pagecount =int.Parse(Request.QueryString["pagecount"]); string sql = string.Format("select top(1) image from Image_Table where image_code=( select new_sjbh from new_ksy_dzpz where new_ksy_dzpzid='{0}' ) ", username); DataTable dt = SqlHelper.GetTable(sql); if (dt.Rows.Count>0) { stream = new MemoryStream((byte[])dt.Rows[0]["image"]); } if (pagecount >= 0) { getImage(pagecount.ToString()); } } void getImage(string pp)//根据路径输出Jpeg格式图片流 { Bitmap bt = new Bitmap(stream); Guid gud = (Guid)bt.FrameDimensionsList.GetValue(0); FrameDimension fds = new FrameDimension(gud); if (pp != "") { int qq = Convert.ToInt16(pp); bt.SelectActiveFrame(fds, qq); } MemoryStream ss = new MemoryStream(); bt.Save(Server.MapPath("1.jpg")); bt.Save(ss, ImageFormat.Jpeg); byte[] bb = ss.GetBuffer(); ss.Read(bb, 0, (int)ss.Length); //this.ID_img.ImageUrl =Convert.ToString(bb); Response.BinaryWrite(bb); ss.Close(); bt.Dispose(); Response.End(); }ShowImage.aspx
Tips:会在根目录下生成1.jpg文件,若迁移代码 需删掉1.jpg文件!