• 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;
    }
    }
    }

  • 相关阅读:
    hadoop优点和缺点
    HDFS的基本shell操作,hadoop fs操作命令
    HDFS的java操作方式
    HDFS的体系结构和操作
    windows主机无法访问服务器
    用公共key实现无密码ssh
    hadoop2的伪分布部署
    lsof/fuser卸载挂载文件
    编译Apache Hadoop2.2.0源代码
    三十分钟掌握STL
  • 原文地址:https://www.cnblogs.com/shuenjian901/p/3510227.html
Copyright © 2020-2023  润新知