からくりブログ

株式会社からくり社員のブログです

VisualStudioのC#でFlashAirにアクセスするプログラムを組んでみる サムネイル編

こんにちは!お久しぶりです。

今回は少し真面目にプログラムを組んでみました。

FlashAirのアプリってWindows版が見当たらないので、今回はVisualStudioのC#を使って簡単にサムネイルをウィンドウに表示するプログラムを作成してみました。

まず、プロジェクトを作成します。今回は昔ながらのフォームアプリケーションです。

FormにButtonコントロールとListViewコントロールを配置します。

button1をダブルクリックしてボタンクリック時のイベント処理を追加します。

ソースコードは以下のような感じです。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Collections;

namespace FA_Thumbnails
{
    public partial class Form1 : Form
    {
        WebClient wc; // HTTP GET等をを処理する
        ImageList ilThumbnails; // ビットマップイメージをリストとして格納する

        public Form1()
        {
            // 初期化
            InitializeComponent();
            wc = new WebClient();
            wc.Encoding = Encoding.UTF8;
            wc.Proxy = null;
            ilThumbnails = new ImageList();
            ilThumbnails.ImageSize = new Size(160, 120); // サムネイルのサイズ
            ilThumbnails.ColorDepth = ColorDepth.Depth32Bit; // サムネイルの色数
        }

        ArrayList GetFileList(string pCsv)
        {
            ArrayList aryResult; // ファイルパスのリスト(リターン値用)
            StringReader objCsv; // テキストファイルを一行ずつ処理する
            string strLine;      // 1行格納用
            string strFilePath;  // ファイルパス

            try
            {
                objCsv = new StringReader(pCsv);
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
                return (ArrayList)null;
            }

            // FlashAirのファイルリストの取得 (op=100)から、サムネイルファイルパスリストを生成
            aryResult = new ArrayList();
            while ((strLine = objCsv.ReadLine()) != null)
            {
                string[] strRow = strLine.Split(',');

                if (strRow.Length != 6)
                {
                    // ファイル名が格納されていない行は無視
                    continue;
                }
                else
                {
                    string[] strFileName = strRow[1].Split('.');
                    if (strFileName[strFileName.Length - 1].Equals("jpg", StringComparison.CurrentCultureIgnoreCase))
                    {
                        // ディレクトリパスとファイル名を結合してファイルパスを生成
                        strFilePath = strRow[0] + "/" + strRow[1];
                    }
                    else
                    {
                        // JPEGファイル以外は無視
                        continue;
                    }
                }
                aryResult.Add(strFilePath);
            }

            return aryResult;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strResult;
            ArrayList aryFilePath;
            Stream stmThumbnail;
            Image imgThumbnail;

            try
            {
                // FlashAirにファイルリストの取得 (op=100)を送信して結果を受け取る
                strResult = wc.DownloadString("http://flashair/command.cgi?op=100&DIR=/DCIM");
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
                return;
            }

            // 取得したファイルリストをファイルパスリストに変換する
            aryFilePath = GetFileList(strResult);
            if (aryFilePath == null)
            {
                return;
            }

            // サムネイルをリストビューの大きいアイコンに設定する
            listView1.LargeImageList = ilThumbnails;

            // FlashAirからサムネイルを取得しイメージリストに格納しリストビューを更新する
            foreach (string strFilePath in aryFilePath)
            {
                try
                {
                    stmThumbnail = wc.OpenRead("http://flashair/thumbnail.cgi?" + strFilePath);
                    imgThumbnail = Image.FromStream(stmThumbnail, false, false);
                    string[] strFileName = strFilePath.Split('/');
                    ilThumbnails.Images.Add(strFileName[strFileName.Length - 1], imgThumbnail);
                    listView1.Items.Add(strFileName[strFileName.Length - 1], strFileName[strFileName.Length - 1]);
                }
                catch (Exception exp)
                {
                    MessageBox.Show(exp.Message);
                    return;
                }
            }
        }
    }
}

実行すると以下のようにサムネイルがWindow表示されます。

One response to “VisualStudioのC#でFlashAirにアクセスするプログラムを組んでみる サムネイル編”

  1. […] 前回はFlashAirに格納されている画像のサムネイルを表示するプログラムをC#で組む方法を取り上げましたが、もう少しアプリとしての完成度を上げていきたいと思います。 […]

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>