C#中图片与流之间的互相转换

本文档展示了如何使用C#将图片转换为字节流,以及如何从字节流中还原图片。提供了从Image对象保存到MemoryStream,从MemoryStream获取Image对象,以及从文件读取图片字节流的示例代码。
摘要由CSDN通过智能技术生成

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Web;

namespace Microsoft.Form.Base
{
class ImageToByte
{
///
/// 图片转换成字节流
///
/// 要转换的Image对象
/// 转换后返回的字节流
public static byte[] ImgToByt(Image img)
{
MemoryStream ms = new MemoryStream();
byte[] imagedata = null;
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png );
imagedata = ms.GetBuffer();
return imagedata;
}
///
/// 字节流转换成图片
///
/// 要转换的字节流
/// 转换得到的Image对象
public static Image BytToImg(byte[] byt)
{
MemoryStream ms = new MemoryStream(byt);
Image img = Image.FromStream(ms);
return img;
}
//
///
/// 根据图片路径返回图片的字节流byte[]
///
/// 图片路径
/// 返回的字节流

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值