Combined with ASHX to display pictures from the database in DataGrid

xiaoxiao2021-03-06  55

For files about ASHX suffixes, please see the problem related articles that eliminate images in IE caches and cannot update

Read and save pictures from the database, please see Write and Read Pictures (C # Asp.Net Sql Server) Related Articles

The following uses ASHX files to make it easy to read images from the database and display them in DataGrid.

//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ----------------------------------

<% @ Page language = "c #" codebehind = "bindimage.aspx.cs" autoeventwireup = "false" inherits = "showimage.bindimg"%> bindimg </ title> </ head> <body> <form id = "form1" method = "post" runat = "server"> <font face = "Song body"> <ask: DataGrid ID = "mydataGrid" runat = "server" autogeneratecolumns = "false" width = "632px"> <AlternatingItemStyle BackColor = "Beige"> </ AlternatingItemStyle> <HeaderStyle HorizontalAlign = "Center"> </ HeaderStyle> <Columns> <asp: TemplateColumn HeaderText = "Photo"> <ItemTemplate> <img src = '< % # "GetImage.ashx? Id =" DataBinder.eval (ContaNDataItem, "EmployeeID")%> '> </ itemtemplate> <asp: templateColumn> <ask: boundcolumn datafield = "Lastname" Headeertext = "Last name "> </ asp: BoundColumn> <asp: BoundColumn DataField =" FirstName "HeaderText =" First name "> </ asp: BoundColumn> <asp: BoundColumn DataField =" title "HeaderText =" title "> </ asp: BoundColumn> </ columns> </ asp: datagrid> </ font> </ form> </ body> </ html></p> <p><% @ Page language = "c #" codebehind = "bindimage.aspx.cs" autoeventwireup = "false" inherits = "showimage.bindimage"%> P> Using system;</p> <p>Using system.data;</p> <p>Using system.drawing;</p> <p>Using system.web; using system.data.sqlclient;</p> <p>Namespace showImage {///</p> <p>/// Bindimg's summary description. ///</p> <p>Public class bindImage: system.web.ui.page</p> <p>{</p> <p>Protected system.Web.ui.WebControls.DataGrid mydatagrid;</p> <p>Private Void Page_Load (Object Sender, System.EventArgs E)</p> <p>{</p> <p>/ / Place the user code here to initialize the page IF (! Page.ispostback)</p> <p>{</p> <p>SqlConnection conn = new sqlconnection (@ "server = shoutor / mydb; database = northwind; uid = sa; pwd = shoutor");</p> <p>Try</p> <p>{</p> <p>Cn.open ();</p> <p>Sqlcommand cmd = new sqlcommand ("Select Employeeid, Lastname, Firstname, Title from Employees", Conn;</p> <p>SqlDataReader Reader = cmd.executeReader ();</p> <p>MyDataGrid.datasource = reader;</p> <p>MyDataGrid.databind ();</p> <p>}</p> <p>Finally</p> <p>{</p> <p>CONN.CLOSE ();</p> <p>}</p> <p>}</p> <p>}</p> <p>#Region web form designer generated code</p> <p>Override protected void oninit (Eventargs E)</p> <p>{</p> <p>// // Codegen: This call is necessary for the ASP.NET Web Form Designer. //</p> <p>InitializationComponent ();</p> <p>Base.onit (E);</p> <p>}</p> <p>// // // Designer supports the required method - do not use the code editor to modify the // / this method. ///</p> <p>Private vidinitiRizeComponent ()</p> <p>{</p> <p>This.Load = New System.EventHandler (this.page_load);</p> <p>}</p> <p>#ndregion</p> <p>}</p> <p>}</p> <p>//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -----------------------</p> <p><% @ WebHandler Language = "C #" class = "showimage.getimage" codebehind = "getimage.ashx.cs"%></p> <p>//------------------------------- GetImage.ashx.cs ----------- -----------------------</p> <p>Using system.data; using system.data.sqlclient; using system.drawing; using system.drawing.imaging; using system.io; namespace showimage {/////</p> <p>/// Getimg's summary description. ///</p> <p>Public Class GetImage: IHTTPHANDLER</p> <p>{</p> <p>Public void processRequest (httpcontext context)</p> <p>{</p> <p>String id = (string) context.request ["id"];</p> <p>IF (id! = null)</p> <p>{</p> <p>MemoryStream Stream = new memorystream ();</p> <p>SqlConnection conn = new sqlconnection (@ "server =; dataBase =; uid =; pwd =");</p> <p>Bitmap BM = NULL;</p> <p>Image image = NULL;</p> <p>Try</p> <p>{</p> <p>Cn.open ();</p> <p>Sqlcommand cmd = new sqlcommand ("SELECT Photo from Employees where EmployeeID = '" ID "'", CONN);</p> <p>Byte [] blob = (byte []) cmd.executescalar ();</p> <p>Stream.write (blob, 78, blob.length-78);</p> <p>BM = New Bitmap (stream);</p> <p>Int width = 48; int Height = (int) (width * ((double) bm.height / (double) bm.width);</p> <p>// getthumbnailimage generated thumbnail image = bm.getthumbnailImage (Width, Height, Null, INTPTR.ZERO);</p> <p>Context.response.contentType = "image / jpeg";</p> <p>Image.Save (Context.Response.OutputStream, Imageformat.jpeg);} finally {if (image! = null) image.dispose (); if (bm! = null) bm.dispose (); stream.close (); CONN.CLOSE ();}}} public bool isreusable {get {return true;}}}}</p> <p>ProcessRequest uses the easy-to-use image.getthumbnailimage method of the nachelf class library to narrow the bitmap to 48 pixels, while maintaining the aspect ratio of the image. Similar techniques can be used to create a DataGrid displayed from other database images. The basic idea is to use the template column to output a <IMG> tab that references an HTTP handler and contains information that uniquely identifies the records in the query string. After that, the HTTP handle uses ADO.NET to get the image data bit and use GDI (Image Device Interface ) to build image.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-114534.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="114534" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.057</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'wTW4K1N1qJRhKvHzPIUWSdl0quAgOuYG_2FPDETTNkUfgMOFBs3qG6SEUa4GU3saxeuTqEWKqR_2B7aIVxcYvoffxA_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>