Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用c#在asp.net中显示Sql Server 2008中的图像?_C#_Asp.net_Image_Sql Server 2008_Byte - Fatal编程技术网

如何使用c#在asp.net中显示Sql Server 2008中的图像?

如何使用c#在asp.net中显示Sql Server 2008中的图像?,c#,asp.net,image,sql-server-2008,byte,C#,Asp.net,Image,Sql Server 2008,Byte,可能重复: 如何使用c#在asp.net中显示Sql Server 2008中的图像 这就是我到目前为止在我的代码背后所得到的。然后我想获取图像并将其显示在.net页面的表中。(图像为.png类型,存储为VarBinary) 处理程序 using System; using System.Web; using System.Drawing; using System.Drawing.Imaging; using System.Data; using System.Data.SqlClient

可能重复:

如何使用c#在asp.net中显示Sql Server 2008中的图像

这就是我到目前为止在我的代码背后所得到的。然后我想获取图像并将其显示在.net页面的表中。(图像为
.png
类型,存储为
VarBinary

处理程序

using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Data;
using System.Data.SqlClient;
using System.Web.Caching;
using System.Configuration;
using System.Web.Configuration;
using System.IO;


namespace RocoSportsWA
{
    public class DisplayImage : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {
        private SqlConnection _connection;
        private SqlCommand _command;
        private HttpContext _context;

        public void ProcessRequest(HttpContext context)
        {

        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }

        public IAsyncResult BeginProcessRequest(HttpContext context,  AsyncCallback cb, object state)
        {
            // Get the employee ID from the query string
            string _logo = context.Request["Logo"];
            if (String.IsNullOrEmpty(_logo))
                return null;

            int logo = 0;
            bool ok = int.TryParse(_logo, out logo);
            if (!ok) return null;

            _context = context;
            string conn = WebConfigurationManager.ConnectionStrings["Data Source=ROBEL-HP;Initial Catalog=RocoSportsDB;Integrated Security=True"].ConnectionString;
            // Select the image from the database
            _connection = new SqlConnection(conn);
            _connection.Open();
            _command = new SqlCommand("SELECT Logo from TEAM where Team = @HomeTeam, _connection");
            _command.Parameters.AddWithValue("@HomeTeam", logo);
            return _command.BeginExecuteReader(cb, state);
        }

        public void EndProcessRequest(IAsyncResult ar)
        {
            try
            {
                SqlDataReader reader = _command.EndExecuteReader(ar);
                if (reader != null && reader.HasRows)
                {
                    // Get the image returned in the query
                    reader.Read();
                    try
                    {
                        byte[] image = (byte[])reader[0];
                        // WRite the image into the HTTP response output stream
                        _context.Response.ContentType = "image/png";
                        // strip off the 78 byte Ole header (a relic from old MS Access databases)
                        _context.Response.OutputStream.Write(image, 78, image.Length - 78);
                    }
                    catch
                    {

                    }
                }
            }
            finally
            {
                if (_connection != null)
                    _connection.Close();
            }
        }
    }
}
WEBPAGE.NET

<asp:Image ID="HomeTeamImage" runat="server" ImageUrl='<%# "DisplayImage.cs?Logo=" + Eval("HomeTeam") %>'

此代码将二进制blob转换为图像:

var bitmapImage = new BitmapImage();
bitmapImage.SetSource(new MemoryStream(imageData));
newImage.Source = bitmapImage;
其中,
imageData
的类型为
byte[]


只要正确设置数据库映射,数据将采用正确的格式。

此代码将二进制blob转换为图像:

var bitmapImage = new BitmapImage();
bitmapImage.SetSource(new MemoryStream(imageData));
newImage.Source = bitmapImage;
其中,
imageData
的类型为
byte[]


只要正确设置数据库映射,数据将采用正确的格式。

最简单的方法是指向每个图像标记的src属性(或者,如果您使用的是ASP.NET Image control,则ASHX处理程序中的ImageUrl属性将接受查询字符串上的图像ID,执行相应的SQL查询,并将图像写入响应输出流

下面是最近的一篇文章,介绍了如何异步执行此操作:


实现这一点的最简单方法是将每个图像标记的src属性(或者如果使用ASP.NET图像控件,则将ImageUrl属性指向ASHX处理程序,该处理程序将接受查询字符串上的图像ID,执行相应的SQL查询,并将图像写入响应输出流

下面是最近的一篇文章,介绍了如何异步执行此操作:


的可能重复,或的可能重复,或者我尝试了这种方式,但它无法识别BitmapImage,因此无法识别Image1中的“源”。源也无法识别。我找不到正确的名称空间。这是asp.net是否有区别。请在此帮助我?(代码如下)conn.Open();字节[]HomeTeamImageByte=(字节[])HomeTeamcmd.ExecuteScalar();字节[]AwayTeamImageByte=(字节[])AwayTeamcmd.ExecuteScalar();变量bitmapImage=new bitmapImage();bitmapImage.SetSource(新内存流(HomeTeamImageByte));Image1.Source=bitmapImage;@Seesharp-将代码添加到您的问题中。
bitmapImage
应该可用。您可能需要添加对程序集的引用-这可以解释它找不到它的原因。我已将代码添加到问题中,因为codebehindI尝试了这种方法,但它无法识别bitmapImage和因此,Image1中的“Source”也无法识别。我找不到它的正确命名空间。这是一个asp.net是否有区别。请您在这里帮助我好吗?(代码在下面)conn.Open();byte[]HomeTeamImageByte=(byte[])HomeTeamcmd.ExecuteScalar();byte[]AwayTeamImageByte=(byte[])AwayTeamcmd.ExecuteScalar();var bitmapImage=new bitmapImage();bitmapImage.SetSource(新内存流(HomeTeamImageByte));Image1.Source=bitmapImage;@Seesharp-将代码添加到问题中。
bitmapImage
应该可用。您可能需要添加对程序集的引用-这可以解释它找不到它的原因。我已将代码添加到问题中,因为codebehindI尝试使用页面上的处理程序,但仍然没有t正在工作,请检查我上面的代码,看看问题出在哪里。我已尝试使用页面上的处理程序,但它仍然不工作,请检查我上面的代码,看看问题出在哪里。