C# 文件转byte数组,byte数组再转换文件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//----引入必要的命名空间
using System.IO;
using System.Drawing.Imaging;
namespace FileStreamOperator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private byte[] photo;//公用缓冲区
public string SourFilePath;//源图片文件路径
public string ObjFilePath;//目标图片路径
private void btnOuput_Click(object sender, EventArgs e)
{
try
{
SourFilePath = txtInputPath.Text.Trim();
string outputPath = txtOutputPath.Text.Trim();
string outputFileName = txtOutputFileName.Text.Trim();
ObjFilePath = outputPath +"\\"+ outputFileName;
if (SourFilePath == "" || outputPath == "" || outputFileName == "")
{
MessageBox.Show("输入路径和输出路径不能为空!");
return;
}
FileToStream();
StreamToFile();
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
private void btnSelectInput_Click(object sender, EventArgs e)
{
txtInputPath.Text=selectFile();
}
private string selectFile() {
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
return openFileDialog1.FileName;
}
else {
return "";
}
}
private string selectFolder()
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
return folderBrowserDialog1.SelectedPath;
}
else
{
return "";
}
}
private void btnSelectOutput_Click(object sender, EventArgs e)
{
txtOutputPath.Text = selectFolder();
}
private void btnClear_Click(object sender, EventArgs e)
{
txtInputPath.Text = "";
txtOutputFileName.Text = "";
txtOutputPath.Text = "";
}
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;
}
public int imgToStream()//图片到流的转换
{
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 int FileToStream()//文件到流的转换
{
photo = File.ReadAllBytes(SourFilePath);
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;
}
}
}
C# 文件转byte数组,byte数组再转换文件的更多相关文章
- 将文件内容转化为byte数组返回
如何将文件内容转化为byte数组并返回呢?对于这个问题,我献上我第一次成功的代码~ package com.succez.task1; import java.io.ByteArrayOutputSt ...
- 文件读写(三)利用FileStream类操作字节数组byte[]、BinaryFormatter、内存流MemoryStream
一.Stream类概述 在.NET Framework中,文件和流是有区别的.文件是存储在磁盘上的数据集,它具有名称和相应的路径.当打开一个文件并对其进行读/写时,该文件就称为流(stream).但是 ...
- java byte【】数组与文件读写(增加新功能)
今天在测试直接写的文章: java byte[]数组与文件读写 时,想调用FileHelper类对字节数组以追加的方式写文件,结果无论怎样竟然数据录入不全,重新看了下文件的追加模式,提供了两种方式: ...
- java byte【】数组与文件读写
此文全文参考http://blog.csdn.net/sniffer_wang/article/details/7455701,自己加以改进应用,谢了 import java.io.ByteArray ...
- c#中字节数组byte[]、图片image、流stream,字符串string、内存流MemoryStream、文件file,之间的转换
字节数组byte[]与图片image之间的转化 字节数组转换成图片 public static Image byte2img(byte[] buffer) { MemoryStream ms = ne ...
- 代码实现:定义一个文件输入流,调用read(byte[] b)方法,将a.txt文件中的内容打印出来(byte数组大小限制为5)
package com.loaderman.test; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; im ...
- 接口请求,上传byte数组byte[]数据异常,负数变正数/负数变63
一.背景 最近项目中有个需求,就是需要把一个byte[]数组上传到服务端.但是我发现发送的byte数组和服务端接收的数组不一样,所有的正数在传递时正确,数组长度也没变化,但是负数变成了63或者负数全部 ...
- Byte和byte[]数组
Byte和byte[]数组,“表示一个 8 位无符号整数, 一般为8位二进制数”. Byte是计算机最基础的存储单位和最基础的通讯单位. 而所有的类型都是支持由byte[]类型转换而来. 为什么说By ...
- C# 字符串string和内存流MemoryStream及比特数组byte[]之间相互转换
定义string变量为str,内存流变量为ms,比特数组为bt 1.字符串转比特数组 复制代码 代码如下: (1)byte[] bt=System.Text.Encoding.Default.GetB ...
随机推荐
- 清空/var/adm/wtmp 文件内容
清/var/adm/wtmp 文件内容 用于显示登录系统和重启机器的情况 /var/adm/wtmp文件过大. 可用du -sm /var/adm/wtmp查看 cat /dev/null>/v ...
- 推荐学习《Python与量化投资从基础到实战》PDF及代码+《量化投资以Python为工具》PDF及代码
利用python分析量化投资问题是现在研究的热点,推荐两份资料用于学习 <Python与量化投资:从基础到实战>主要讲解如何利用Python进行量化投资,包括对数据的获取.整理.分析挖掘. ...
- Firewalld 用法解析
其实还是我写的啦 https://www.jianshu.com/p/3444d9413461 1.防火墙firewall的基本概述 现在的RedHat/CentOS7版本默认都使用firewall防 ...
- 初尝Perl -- 使用aapt给apk软件包批量重命名
不知道什么是Perl猛戳这个链接 http://zh.wikipedia.org/wiki/Perl 任务: 随着手机/平板的各方面性能的不断发展(CPU,内存 ...
- jdbc的数据库驱动类DriverManager.getConnection()详解
1.Oracle8/8i/9i数据库(thin模式) Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); ...
- FreeModbus TCP
一.Modbus TCP协议格式 Modbus TCP协议数据格式如下图所示: MBAP报文头: Modbus TCP与Modbus RTU相比,没有检验码,也没有所谓的地址码,并且在RTU协议的基础 ...
- Android中级第九讲--相机调焦
博客出自:http://blog.csdn.net/liuxian13183,转载注明出处! All Rights Reserved ! 相机调焦:原理,使用竖直seekbar,根据用户拖拉来获得距离 ...
- cogs P1578【模板】 次小生成树初级练习题
1578. 次小生成树初级练习题 ☆ 输入文件:mst2.in 输出文件:mst2.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] 求严格次小生成树 [输入格式 ...
- [Javascript] Identify the most important words in a document using tf-idf in Natural
Tf-idf, or term frequency-inverse document frequency, is a statistic that indicates how important a ...
- 測试CPU支持指令集AVX,AVX2,SSE情况的代码【VS2010调试通过】
完整代码例如以下所看到的 http://download.csdn.net/detail/vbskj/7723827 本人的測试结果 watermark/2/text/aHR0cDovL2Jsb2cu ...