HeyCHのブログ

慢性疲労のへいちゃんです

【C#】Streamって何よ

Streamとはデータの流れを表していて、ファイルの読み書きや、ネットワークのデータのやり取りを行う事ができます。

プロジェクトの準備

前回の記事を参考にSplitContainerにTextBoxとPictureBoxを配置します。
TextBoxのReadOnlyとMulutilineプロパティをTrueに設定し、ScrollBarsプロパティをBothに設定。
また、Form1をダブルクリックしてForm1_Loadイベントを追加します。
heych.hatenablog.com
f:id:HeyCH:20200413235805p:plain

テキストファイルの読み込み

以下のようにする事でテキストファイルを読み込み、TextBoxに表示することができます。
※Sample.txtは各自で用意する必要があります。
※上のusingのところに、「using System.IO;」を追加しています。

        private void Form1_Load(object sender, EventArgs e) {
            using (StreamReader r = new StreamReader(@"C:\Users\ユーザー名\Desktop\Sample.txt", Encoding.UTF8)) {
                textBox1.Text = r.ReadToEnd();
            }
        }

ファイルへのテキスト書き込み

以下のようにする事でTextBoxの内容をファイルに書き込むことができます。

        private void Form1_Load(object sender, EventArgs e) {
            using (StreamReader r = new StreamReader(@"C:\Users\ユーザー名\Desktop\Sample.txt", Encoding.UTF8)) {
                textBox1.Text = r.ReadToEnd();
            }
            using (StreamWriter w = new StreamWriter(@"C:\Users\ユーザー名\Desktop\Sample2.txt", false, Encoding.UTF8)) {
                for (int i = 0; i < textBox1.Text.Length; i++) {
                    w.Write(textBox1.Text[i]);
                }
            }
        }

画像ファイルの読み込み

以下のようにする事で画像ファイルを読み込み、PictureBoxに表示することができます。
※Sample.pngは各自で用意する必要があります。

        private void Form1_Load(object sender, EventArgs e) {
            using (FileStream r = new FileStream(@"C:\Users\ユーザー名\Desktop\Sample.png", FileMode.Open, FileAccess.Read)) {
                pictureBox1.Image = Image.FromStream(r);
            }
        }

ファイルへの画像書き込み

以下のようにする事でPictureBoxの内容をファイルに書き込むことができます。
※上のusingのところに、「using System.Drawing.Imaging;」を追加しています。

        private void Form1_Load(object sender, EventArgs e) {
            using (FileStream r = new FileStream(@"C:\Users\ユーザー名\Desktop\Sample.png", FileMode.Open, FileAccess.Read)) {
                pictureBox1.Image = Image.FromStream(r);
            }
            using (FileStream w = new FileStream(@"C:\Users\ユーザー名\Desktop\Sample2.png", FileMode.CreateNew, FileAccess.Write)) {
                pictureBox1.Image.Save(w, ImageFormat.Png);
            }
        }

インターネット上のHTMLの読み込み

以下のようにする事で、インターネット上にあるHTMLファイルの内容をTextBoxに表示することができます。
※上のusingのところに、「using System.Net;」を追加しています。

        private void Form1_Load(object sender, EventArgs e) {
            HttpWebRequest req = WebRequest.CreateHttp("https://heych.hatenablog.com/");
            using (var res = req.GetResponse()) {
                using (var r = res.GetResponseStream()) {
                    using (var sr = new StreamReader(r, Encoding.UTF8)) {
                        textBox1.Text = sr.ReadToEnd();
                    }
                }
            }
        }

インターネット上の画像の読み込み

以下のようにする事で、インターネット上の画像ファイルの内容をPictureBoxに表示することができます。

            HttpWebRequest req2 = WebRequest.CreateHttp("https://cdn-ak.f.st-hatena.com/images/fotolife/H/HeyCH/20200408/20200408001941.png");
            using (var res = req2.GetResponse()) {
                using (var r = res.GetResponseStream()) {
                    pictureBox1.Image = Image.FromStream(r);
                }
            }