目次

全ソースコード

 作成した内容のソースコードは、以下です。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace vtxt
{

  public partial class mainForm : Form
  {
    string gStartDirectory;
    string gFName;

    const int xlast = 512;
    const int ylast = 480;
    const int ysize = 1537;

    public mainForm()
    {
      InitializeComponent();
      // get File Path
      gStartDirectory = Path.GetDirectoryName(Application.ExecutablePath);
      int idx;
      idx = gStartDirectory.IndexOf("\\vtxt");
      gFName = gStartDirectory.Substring(0, idx + 5);
      gStartDirectory = gFName;
      gFName = "";
      lblFileName.Text = gFName ;
      // set file path
      openFileDlg.InitialDirectory = gStartDirectory;
      saveFileDlg.InitialDirectory = gStartDirectory;
      // default file name
      openFileDlg.FileName = gFName;
      saveFileDlg.FileName = gFName;
      // file filter
      openFileDlg.Filter = "text file(*.txt)|*.txt";
      string stmp;
      stmp  = "BMP file(*.bmp)|*.bmp|";
      stmp += "GIF file(*.gif)|*.gif|";
      stmp += "JPEG file(*.jpg)|*.jpg|";
      stmp += "TIFF file(*.tif)|*.tif";
      saveFileDlg.Filter = stmp ;
    }

    // Load
    private void btnLoad_Click(object sender, EventArgs e)
    {
      string sline;
      string stmp;
      int tmp;
      int x, y;

      if (openFileDlg.ShowDialog() == DialogResult.OK)
      {
        // get file name
        gFName = openFileDlg.FileName;
        // get context of text file
        string stext ;
        StreamReader sr = new StreamReader(gFName, Encoding.Default);
        stext = sr.ReadToEnd();
        sr.Close();
        // show file name
        lblFileName.Text = gFName;
        // generate area
        Bitmap bmp = new Bitmap(xlast, ylast);
        // store data to area
        for ( y = 0 ; y < ylast ; y++ )
        {
          // get 1 line
          sline = stext.Substring(y*ysize,ysize);
          for ( x = 0 ; x < xlast ; x++ )
          {
            // get value
            stmp = sline.Substring(3*x,2);
            // convert
            tmp = hex2digit(stmp[0]) * 16 + hex2digit(stmp[1]);
            // store pixel data
            bmp.SetPixel(x,y,Color.FromArgb(255,tmp,tmp,tmp));
          }
        }
        // show
        picBox.Image = bmp ;
      }
    }

    // Ascii to digit number
    private byte hex2digit(int x)
    {
      int result;
      result = 0 ;
      if ('0' <= x && x <= '9') { result = x - '0'; }
      if ('A' <= x && x <= 'F') { result = x - 'A' + 10; }
      if ('a' <= x && x <= 'f') { result = x - 'a' + 10; }
      return (byte)result;
    }

    // Save
    private void btnSave_Click(object sender, EventArgs e)
    {
      if (saveFileDlg.ShowDialog() == DialogResult.OK)
      {
        // confirm file name
        string fname = saveFileDlg.FileName ;
        // save
        int idx = saveFileDlg.FilterIndex;
        switch ( idx ) {
          case 1 : picBox.Image.Save(fname, ImageFormat.Bmp); break;
          case 2 : picBox.Image.Save(fname, ImageFormat.Gif); break;
          case 3 : picBox.Image.Save(fname, ImageFormat.Jpeg); break;
          case 4 : picBox.Image.Save(fname, ImageFormat.Tiff); break;
        }
      }
    }

    // Exit
    private void btnExit_Click(object sender, EventArgs e)
    {
      Application.Exit();
    }
  }
}


目次

inserted by FC2 system