//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;
}
}
}
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);
}