C#实现图片文件到数据流再到图片文件的转换
//----引入必要的命名空间
using System.IO;
using System.Drawing.Imaging;
//----代码部分----//
private byte[] photo;//公用缓冲区
public string SourFilePath;//源图片文件路径
public string ObjFilePath;//目标图片路径
public int FileToStream()//文件到流的转换
{
Image img = new Bitmap(SourFilePath);
MemoryStream stream = new MemoryStream();
img.Save(stream, ImageFormat.Bmp);
BinaryReader br = new BinaryReader(stream);
photo = stream.ToArray();
stream.Close();
return 0;
}
public Image ShowPic()//根据流显图
{
byte[] bytes = photo;
MemoryStream ms = new MemoryStream(bytes);
ms.Position = 0;
Image img = Image.FromStream(ms);
ms.Close();
return img;
}
public int StreamToFile()//反向转换
{
byte[] bytes = photo;
FileStream fs = new FileStream(ObjFilePath, FileMode.Create, FileAccess.Write);
fs.Write(bytes, 0, bytes.Length);
fs.Flush();
fs.Close();
return 0;
}
最新文章
- Android 和 H5 通信
- css中的选择器
- gcc杂谈
- 解决xshell连接linux总是异常断开的问题?
- cocos2d-x jsbinding 资源下载实现
- 如何保护 .NET 应用的安全?
- 转:Oracle EBS FND User Info API
- Android代码混淆
- mock server相关解决方案
- CakePHP下使用paginator需要对多个字段排序的做法
- 将ArrayList<;HashMap<;String, String>;>;转为ArrayList<;Bundle>;类型的解决方案
- centos搭建java web服务器
- Stanford CS224N 第二课: word2vec详细介绍
- android动画介绍--Animation 实现loading动画效果
- git简介及安装(win10)
- Table &#39;performance_schema.session_status&#39; doesn&#39;t exist错误,解决办法
- kafka安装和部署
- SQL Server 表分区备忘
- create table b1 as select * from b建表锁表测试
- OPENSSL编程起步