最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#將圖片和字節(jié)流互相轉(zhuǎn)換并顯示到頁面上

 更新時(shí)間:2015年08月04日 17:44:51   投稿:mrr  
本文主要介紹用C#實(shí)現(xiàn)圖片轉(zhuǎn)換成字節(jié)流,字節(jié)流轉(zhuǎn)換成圖片,并根據(jù)圖片路徑返回圖片的字節(jié)流,有需要的朋友可以參考下

圖片轉(zhuǎn)換成字節(jié)流先要轉(zhuǎn)換的IMage對象,轉(zhuǎn)換之后返回字節(jié)流。字節(jié)流轉(zhuǎn)換成圖片,要轉(zhuǎn)換的字節(jié)流,轉(zhuǎn)換得到的Image對象,根據(jù)圖片路徑返回圖片的字節(jié)流,感興趣的朋友看下下面的代碼。

C#將圖片和字節(jié)流相互轉(zhuǎn)換代碼:

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Drawing;
usingSystem.IO;
namespaceMicrosoft.Form.Base
{
classImageToByte
{
/// <summary>
/// 圖片轉(zhuǎn)換成字節(jié)流
/// </summary>
/// <param name="img">要轉(zhuǎn)換的Image對象</param>
/// <returns>轉(zhuǎn)換后返回的字節(jié)流</returns>
publicstaticbyte[] ImgToByt(Image img)
{
MemoryStream ms = newMemoryStream();
byte[] imagedata = null;
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
imagedata = ms.GetBuffer();
returnimagedata;
}
/// <summary>
/// 字節(jié)流轉(zhuǎn)換成圖片
/// </summary>
/// <param name="byt">要轉(zhuǎn)換的字節(jié)流</param>
/// <returns>轉(zhuǎn)換得到的Image對象</returns>
publicstaticImage BytToImg(byte[] byt)
{
MemoryStream ms = newMemoryStream(byt);
Image img = Image.FromStream(ms);
returnimg;
}
//
/// <summary>
/// 根據(jù)圖片路徑返回圖片的字節(jié)流byte[]
/// </summary>
/// <param name="imagePath">圖片路徑</param>
/// <returns>返回的字節(jié)流</returns>
privatestaticbyte[] getImageByte(stringimagePath)
{
FileStream files = newFileStream(imagePath, FileMode.Open);
byte[] imgByte = newbyte[files.Length];
files.Read(imgByte, 0, imgByte.Length);
files.Close();
returnimgByte;
}
}
}

將字節(jié)流轉(zhuǎn)換為圖片文件顯示到頁面上

//Byte[] result;
System.IO.MemoryStream ms =new MemoryStream(result, 0, result.Length) 
 Response.ClearContent();
 Response.ContentType = "image/Gif";
 Response.BinaryWrite(ms.ToArray());
或者添加一個(gè)處理圖片的Handler,內(nèi)容如下:
<%@ 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"]);
 //調(diào)用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 ] ;
 //調(diào)用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;
 }
 }
}

以上就是本文的全部內(nèi)容,希望對大家學(xué)習(xí)C#將圖片和字節(jié)流互相轉(zhuǎn)換并顯示到頁面上有所幫助。

相關(guān)文章

最新評論

井研县| 宣汉县| 个旧市| 桂阳县| 水城县| 浦东新区| 建昌县| 衡东县| 青海省| 惠州市| 霍城县| 石台县| 贞丰县| 中卫市| 上杭县| 襄城县| 屯昌县| 台中县| 多伦县| 枣强县| 玉田县| 昌江| 伊吾县| 曲麻莱县| 昭苏县| 海原县| 社会| 漳州市| 苍山县| 青岛市| 竹溪县| 鹰潭市| 兴海县| 德阳市| 诸城市| 嘉定区| 沙雅县| 岑溪市| 东至县| 横山县| 嘉黎县|