asp.net网页中如何实现把二进制转换成图片显示功能 我从数据库中读取出了二进制流,现在遇到的困难是如何把该二进制流转换成图片,注意是在.aspx网页中显示图片不是在桌面应用程序中显示图片 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //打开文件图片,并存放在文件流中 FileStream stream = new FileStream(Server.MapPath("logo.jpg"),FileMode.Open); //首先声明一个文件流的类,并且初始化,使用Server.MapPath方法获取到绝对路径,使用FileMode.Open设置图片文件模式为打开。 long filesize = stream.Length;//声明一个长整形变量,用来保存文件流的长度。 byte[] buffer=new byte[(int)filesize];//定义一个二进制的数组,数组大小就是文件流的长度 stream.Read(buffer, 0, (int)filesize);//用文件流的read方法来读取这个文件,并将该数据写入缓冲区内。 stream.Close();//关闭流 Response.BinaryWrite(buffer);//将图片输出到页面中 你要先将图片读到一个文件夹内,然后把路径给它就可以了,参考:MemoryStream ms=new MemoryStream ((Byte[])imgname);//imgname是图片的名字Bitmap img=new Bitmap(ms);string filepath=Server.MapPath("Files/");DirectoryInfo dir=new DirectoryInfo(filepath);FileInfo[] filecount=dir.GetFiles();int i=filecount.Length;string imagename=filepath+(i+1)+.jpg);img.Save(imagename);Image1.ImageUrl="Files/+((i+1)+".jpg"); SqlConnection conn = new SqlConnection("........."); SqlCommand comm = new SqlCommand("select image字段 from 表 where id=1", conn); conn.Open(); SqlDataReader reader = comm.ExecuteReader(); if (reader.Read()) { Response.ContentType = "image/jpeg"; Response.BinaryWrite((Byte[])reader["image字段"]); } Response.End(); conn.Close(); int Id=int.Parse(Request.QueryString["id"]); using(SqlConnection conn=new SqlConnection()) { conn.ConnectionString=""; string strSql="select * from Tb where Id='"+Id+"'"; SqlCommand cmd=new SqlCommand(strSql,conn) ; conn.Open(); SqlDataReader reader=cmd.ExecuteReader(); if(reader.Read()) { Response.ContentType = "image/jpeg"; Response.BinaryWrite((Byte[])reader["Photo"]); } Response.End(); conn.Close(); } 下面代码是什么意思啊 ASP.NET一个显示的小问题 .net 访问Oracle数据库出错 关于页面打回车提交的小问题 新到asp.net版块,散分交朋友.随便求点入门经验 强名数据集参数绑定 WEB窗口上做出类似DELPHI中pageconctrol的效果??? C# 方法层级关系写法 ●救命呀!!DataGrid 有没有DataSource属性???? 有关模态对话框和网页对话框TITLE的问题! 请朋友们给我点提高负载(并发?)的思路.问题如下! asp:DropDownList选择问题
FileStream stream = new FileStream(Server.MapPath("logo.jpg"),FileMode.Open); //首先声明一个文件流的类,并且初始化,使用Server.MapPath方法获取到绝对路径,使用FileMode.Open设置图片文件模式为打开。
long filesize = stream.Length;//声明一个长整形变量,用来保存文件流的长度。
byte[] buffer=new byte[(int)filesize];//定义一个二进制的数组,数组大小就是文件流的长度
stream.Read(buffer, 0, (int)filesize);//用文件流的read方法来读取这个文件,并将该数据写入缓冲区内。
stream.Close();//关闭流
Response.BinaryWrite(buffer);//将图片输出到页面中
MemoryStream ms=new MemoryStream ((Byte[])imgname);//imgname是图片的名字
Bitmap img=new Bitmap(ms);
string filepath=Server.MapPath("Files/");
DirectoryInfo dir=new DirectoryInfo(filepath);
FileInfo[] filecount=dir.GetFiles();
int i=filecount.Length;
string imagename=filepath+(i+1)+.jpg);
img.Save(imagename);Image1.ImageUrl="Files/+((i+1)+".jpg");
SqlCommand comm = new SqlCommand("select image字段 from 表 where id=1", conn);
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
if (reader.Read())
{
Response.ContentType = "image/jpeg";
Response.BinaryWrite((Byte[])reader["image字段"]);
}
Response.End();
conn.Close();
int Id=int.Parse(Request.QueryString["id"]);
using(SqlConnection conn=new SqlConnection())
{
conn.ConnectionString="";
string strSql="select * from Tb where Id='"+Id+"'";
SqlCommand cmd=new SqlCommand(strSql,conn) ;
conn.Open();
SqlDataReader reader=cmd.ExecuteReader();
if(reader.Read())
{
Response.ContentType = "image/jpeg";
Response.BinaryWrite((Byte[])reader["Photo"]);
}
Response.End();
conn.Close();
}