zoukankan      html  css  js  c++  java
  • C#读取图片流保存到文件,再读取流文件,把图片再显示出来

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace 将图片保存文件流
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                StringBuilder sb = new StringBuilder();
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "(*.jpg)|*.jpg";
              
                  string fileName = ofd.FileName;
                //if (ofd.ShowDialog() == DialogResult.OK )// && !string.IsNullOrEmpty(fileName) ) 
                //{
                //    FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
                //    byte[] imageByte =  new byte[fs.Length];
                //    BinaryReader br = new BinaryReader(fs);
                //    imageByte = br.ReadBytes(Convert.ToInt32(fs.Length));
    
                //    for (int i = 0; i < imageByte.Length; i++)
                //    {
                //       sb.Append(imageByte[i].ToString());
                //    }
    
                //    textBox1.Text = sb.ToString();
    
                //}
    
                  if (ofd.ShowDialog() == DialogResult.OK)// && !string.IsNullOrEmpty(fileName) ) 
                  {
                      FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.ReadWrite);
                      
    
                      FileStream fSource = new FileStream(@"D:ABC.dat", FileMode.OpenOrCreate, FileAccess.ReadWrite);
                      BinaryWriter bw = new BinaryWriter(fSource);
    
                      byte[] imageByte = new byte[fs.Length];
                      BinaryReader br = new BinaryReader(fs);
                      imageByte = br.ReadBytes(Convert.ToInt32(fs.Length));
    
                      bw.Write(imageByte); 
                      int length = Convert.ToInt32(imageByte.Length);
                      //for (int i = 0; i < imageByte.Length; i++)
                      //{
                      //    sb.Append(imageByte[i].ToString());
                      //}
                      fs.Close();
                      //textBox1.Text = sb.ToString();
                      bw.Close();
                    
                      fSource.Close();
    
                  }
    
    
               
    
               // FileStream fs = new FileStream();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "(*.dat)|*.dat";
                if (ofd.ShowDialog() == DialogResult.OK) {
    
                
                    FileStream fs = new FileStream(ofd.FileName, FileMode.Open,FileAccess.Read);
                    byte[] imageByte =new byte[ofd.FileName.Length];
                    BinaryReader br = new BinaryReader(fs);
                    imageByte = br.ReadBytes(Convert.ToInt32(fs.Length));
    
                    MemoryStream ms = new MemoryStream(imageByte);
                    Bitmap bm = new Bitmap(ms);
    
                    pictureBox1.Image = bm;
                    ms.Close();
                    fs.Close();
                    fs.Dispose();
                }
            }
        }
    }
    

      

  • 相关阅读:
    find指令使用手册
    IP封包协议头/TCP协议头/TCP3次握手/TCP4次挥手/UDP协议头/ICMP协议头/HTTP协议(请求报文和响应报文)/IP地址/子网掩码(划分子网)/路由概念/MAC封包格式
    Vmare虚拟机中的3种网络连接方式
    Windows10下Apache2.4配置Django
    网站配色
    js 图片轮播
    Window10下Apache2.4的安装和运行
    sqlite数据库转换为mysql数据库
    windows10 安装 mysql 5.6 教程
    win10 nginx + django +flup 配置
  • 原文地址:https://www.cnblogs.com/nymz/p/14226605.html
Copyright © 2011-2022 走看看