Здравствуйте! Вот два правильных варианта.
**---1 вариант---
Default.aspx**
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<% Fun(); %>
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
//подключить пространство имен
using System.Configuration;
namespace WebApplication1 {
public partial class Default : System.Web.UI.Page {
public void Fun() {
byte[] byteArray = new byte[0];
string stringConnect = ConfigurationManager.ConnectionStrings["zzz"].ConnectionString;
using(SqlConnection connect = new SqlConnection(stringConnect)) {
connect.Open();
string sql = "SELECT Photo FROM zzz";
SqlCommand cmd = new SqlCommand(sql, connect);
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read()) {
byteArray = new byte[((byte[])reader["Photo"]).Length];
byteArray = (byte[])reader["Photo"];
/*обязательно нужно КОНВЕРТИРОВАТЬ подробнее здесь*/
//https://msdn.microsoft.com/ru-ru/library/dhx0d524%28v=vs.110%29.aspx
string base64String;
base64String = System.Convert.ToBase64String(byteArray, 0, byteArray.Length);
/*вот теперь здесь ВСЁ так*/
Response.Write("<img src=\"data:image/png;base64,"+base64String+"\" width=\"250\" height=\"400\" />");
}
}
}
protected void Page_Load(object sender, EventArgs e) {
}
}
}
---2 вариант---
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<img src="Handler1.ashx" />
</div>
</form>
</body>
</html>
Handler1.ashx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Configuration;
namespace WebApplication1 {
public class Handler1 : IHttpHandler {
public void ProcessRequest(HttpContext context) {
HttpRequest req = context.Request;
HttpResponse response = context.Response;
context.Response.ContentType = "image/jpeg";
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.BufferOutput = false;
writeSingleImage(req.QueryString["Photo"], response.OutputStream);
}
private void writeSingleImage(string p, Stream stream) {
//throw new NotImplementedException();
byte[] img;
string stringConnect = ConfigurationManager.ConnectionStrings["zzz"].ConnectionString;
using(SqlConnection connect = new SqlConnection(stringConnect)) {
connect.Open();
string sql = "SELECT Photo FROM zzz";
SqlCommand cmd = new SqlCommand(sql, connect);
img = (byte[])cmd.ExecuteScalar();
}
stream.Write(img, 0, img.Length);
}
public bool IsReusable {
get {
return false;
}
}
}
}