首页 技术 正文
技术 2022年11月18日
0 收藏 422 点赞 3,721 浏览 1681 个字

之前在http://www.cnblogs.com/JsonZhangAA/p/5568575.html博文中是利用的image控件来显示的二进制流图片,我现在想的是能

通过普通的<img id=”xx” src=”xx”/>这种形式来显示我的二进制流图片吗?必须可以(◑▽◐),就是写法稍微麻烦了一点,img要写成这个样子:

如何显示二进制流的图片(利用img控件),对你看的没错,它的地址指向了一个aspx页面,这个页面有个奇特之处,

就是我们新建后,不用写任何前台代码,WebForm1前后台代码如下:

前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="显示爬虫所爬的数据库中的图片.WebForm1" %>

后台代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using 显示爬虫所爬的数据库中的图片.Models;namespace 显示爬虫所爬的数据库中的图片
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int id = int.Parse(Request["id"].ToString());
DataClasses1DataContext db = new DataClasses1DataContext();
Response.ContentType = "application/binary;";
//这个地方图片可以从数据库中读取二进制图片
//byte[] img = DBHelper.ReadImg();
byte[] img = db.pictureUrl.Where(p=>p.Id==id).First().pictureUrl1.ToArray();
Response.BinaryWrite(img); Response.Flush();
Response.End();
}
}
}

我们主页面的前后台代码如下:

前台:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %><!DOCTYPE html><html>
<head runat="server">
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<%foreach(var item in ViewBag.Pictures) %>
<%{ %>
<img src="WebForm1.aspx?id=<%:item.Id %>" />
<%} %>
</div></body>
</html>

后台代码:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using 显示爬虫所爬的数据库中的图片.Models;namespace 显示爬虫所爬的数据库中的图片.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
DataClasses1DataContext db = new DataClasses1DataContext();
public ActionResult Index()
{
ViewBag.Pictures = db.pictureUrl;
return View();
}
}
}

最后运行的结果:

如何显示二进制流的图片(利用img控件)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,401
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,896