首页 新闻 会员 周边 捐助

如何将一个字节流转换为图片文件显示到页面上呢!!

0
悬赏园豆:20 [已解决问题] 解决于 2008-11-06 16:29

如何将一个字节流转换为图片文件显示到页面上呢!!

Jonas的主页 Jonas | 初学一级 | 园豆:185
提问于:2008-11-04 01:28
< >
分享
最佳答案
0

//Byte[] result;

System.IO.MemoryStream ms =new  MemoryStream(result, 0, result.Length) 
  Response.ClearContent();
  Response.ContentType = "image/Gif";
  Response.BinaryWrite(ms.ToArray());

或者添加一个处理图片的Handler,内容如下:
<%@ WebHandler Language="C#" Class="Handler" %>

using System.Web;
using System.IO;


public class Handler : IHttpHandler {
   
  public void ProcessRequest (HttpContext context) {
  
  int CategoryID = int.Parse(context.Request.QueryString["CategoryID"]);
  //调用Categories.GetPicture取得图片stream
  Stream stream = CategoriesPicture.GetPicture(CategoryID);
   
  if (stream !=null) {
  //取得图片stream大小
  int buffersize = (int)stream.Length;
  //建立buffer
  System.Byte[] buffer = new System.Byte[buffersize ] ;
  //调用stream.Read,从stream读取到buffer,并返回count
  int count = stream.Read(buffer, 0, buffersize);
  //返回图片字段buffer
  if (count!=0)
  context.Response.OutputStream.Write(buffer, 0, count);
  }

  }
 
  public bool IsReusable {
  get {
  return false;
  }
  }

}

上不了岸的鱼 | 老鸟四级 |园豆:4613 | 2008-11-04 01:57
其他回答(1)
0
Code

private void imgToString(string imgPath)
{
FileStream fs
= new FileStream(imgPath, FileMode.Open);
BinaryReader br
= new BinaryReader(fs);

byte[] imageBuffer = new byte[br.BaseStream.Length];
br.Read(imageBuffer,
0, Convert.ToInt32(br.BaseStream.Length));
string textString = System.Convert.ToBase64String(imageBuffer);
fs.Close();
br.Close();
}
private void stringToImg(string imgString)
{
byte[] img = System.Convert.FromBase64String(imgString);
MemoryStream ms
= new MemoryStream(img, true);
ms.Read(img,
0, img.Length);
Image imge
= Image.FromStream(ms);
}
天堂口 | 园豆:514 (小虾三级) | 2008-11-04 08:37
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册