C#将图片和字节流互相转换并显示到页面上
作者:mrr 时间:2021-08-18 23:41:31
图片转换成字节流先要转换的IMage对象,转换之后返回字节流。字节流转换成图片,要转换的字节流,转换得到的Image对象,根据图片路径返回图片的字节流,感兴趣的朋友看下下面的代码。
C#将图片和字节流相互转换代码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Drawing;
usingSystem.IO;
namespaceMicrosoft.Form.Base
{
classImageToByte
{
/// <summary>
/// 图片转换成字节流
/// </summary>
/// <param name="img">要转换的Image对象</param>
/// <returns>转换后返回的字节流</returns>
publicstaticbyte[] ImgToByt(Image img)
{
MemoryStream ms = newMemoryStream();
byte[] imagedata = null;
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
imagedata = ms.GetBuffer();
returnimagedata;
}
/// <summary>
/// 字节流转换成图片
/// </summary>
/// <param name="byt">要转换的字节流</param>
/// <returns>转换得到的Image对象</returns>
publicstaticImage BytToImg(byte[] byt)
{
MemoryStream ms = newMemoryStream(byt);
Image img = Image.FromStream(ms);
returnimg;
}
//
/// <summary>
/// 根据图片路径返回图片的字节流byte[]
/// </summary>
/// <param name="imagePath">图片路径</param>
/// <returns>返回的字节流</returns>
privatestaticbyte[] getImageByte(stringimagePath)
{
FileStream files = newFileStream(imagePath, FileMode.Open);
byte[] imgByte = newbyte[files.Length];
files.Read(imgByte, 0, imgByte.Length);
files.Close();
returnimgByte;
}
}
}
将字节流转换为图片文件显示到页面上
//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;
}
}
}
标签:c#,字节流,图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JDBC基础教程
2021-10-26 03:47:09
C#使用远程服务调用框架Apache Thrift
2023-05-07 01:05:01
Android应用开发SharedPreferences存储数据的使用方法
2023-06-23 16:02:47
![](https://img.aspxhome.com/file/2023/0/82410_0s.jpg)
MapStruct对象映射转换解决Bean属性拷贝性能问题
2023-05-14 04:10:55
![](https://img.aspxhome.com/file/2023/6/77086_0s.jpg)
Java实现Web应用中的定时任务(实例讲解)
2022-08-12 23:40:02
C++类中的static和const用法实例教程
2023-07-03 23:19:07
java开发ShardingSphere的路由引擎类型示例详解
2023-11-29 01:18:56
Java多线程编程之读写锁ReadWriteLock用法实例
2021-10-13 17:01:14
![](https://img.aspxhome.com/file/2023/0/65620_0s.png)
Java实现线程安全单例模式的五种方式的示例代码
2023-09-26 16:41:23
mvc开启gzip压缩示例分享
2022-05-03 08:34:29
线程局部变量的实现 ThreadLocal使用及场景介绍
2023-11-10 03:19:26
![](https://img.aspxhome.com/file/2023/1/59031_0s.jpg)
2021最新Android笔试题总结美团Android岗职能要求
2023-11-29 15:24:52
![](https://img.aspxhome.com/file/2023/9/93299_0s.jpg)
opencv配置的完整步骤(win10+VS2015+OpenCV3.1.0)
2023-06-28 14:55:19
![](https://img.aspxhome.com/file/2023/4/100304_0s.png)
Java日常练习题,每天进步一点点(63)
2021-10-22 12:35:44
Java判断字符串是否是整数或者浮点数的方法
2022-04-30 10:06:20
java 根据经纬度获取地址实现代码
2023-09-05 04:54:50
Java详细讲解包的作用以及修饰符的介绍
2021-09-14 05:27:26
OpenCV图像旋转Rotate的详细介绍
2023-07-01 08:22:27
![](https://img.aspxhome.com/file/2023/2/95522_0s.jpg)
关于Unsupported major.minor version 49.0的错误解决办法
2023-06-04 22:57:44
![](https://img.aspxhome.com/file/2023/7/96077_0s.jpg)
使用maven命令安装jar包到本地仓库的方法步骤
2022-10-19 21:57:58
![](https://img.aspxhome.com/file/2023/8/80958_0s.png)