Download RiffPaletteLoader.zip version 1.0.0.0, last updated 18/02/2017 (15.41 KB)

Download
  • md5: 3b1ed86c3076e401c7f8c1d79c610800
  • sha1: b71a2ed2f72637eb844530aa845fa9642c16b74f
  • sha256: 65ed960371418198622905bab618209fe66b36fb490c66c0f40a2f0548a42b3e
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

// Loading Microsoft RIFF Palette (pal) files with C#
// http://cyotek.com/blog/loading-microsoft-riff-palette-pal-files-with-csharp

namespace Cyotek.Demonstrations.RiffPaletteLoader
{
  public partial class MainForm : Form
  {
    #region Constants

    private const int _cellSize = 24;

    private const int _defaultX = 6;

    private const int _defaultY = 6;

    private const int _spacing = 6;

    #endregion

    #region Fields

    private RiffPalette _loadedPalette;

    #endregion

    #region Constructors

    public MainForm()
    {
      this.InitializeComponent();
    }

    #endregion

    #region Methods

    /// <summary>
    /// Raises the <see cref="E:System.Windows.Forms.Form.Load"/> event.
    /// </summary>
    /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data. </param>
    protected override void OnLoad(EventArgs e)
    {
      base.OnLoad(e);

      this.Text = Application.ProductName;
      this.AddFiles("pal");
    }

    private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
    {
      using (Form dialog = new AboutDialog())
        dialog.ShowDialog(this);
    }

    private void AddFiles(string extension)
    {
      string path;

      path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data");
      foreach (string fileName in Directory.GetFiles(path, "*." + extension))
        filesListBox.Items.Add(new FileInfo(fileName));
    }

    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
      this.Close();
    }

    private void filesListBox_SelectedIndexChanged(object sender, EventArgs e)
    {
      FileInfo selectedFile;

      selectedFile = filesListBox.SelectedItem as FileInfo;

      if (selectedFile != null)
      {
        _loadedPalette = new RiffPalette();
        _loadedPalette.Load(selectedFile.FullPath);

        this.Text = $"{Path.GetFileName(selectedFile.FullPath)} - {Application.ProductName}";
      }
      else
      {
        _loadedPalette = null;
        this.Text = Application.ProductName;
      }

      palettePanel.Invalidate();
    }

    private void palettePanel_Paint(object sender, PaintEventArgs e)
    {
      if (_loadedPalette != null)
      {
        int x;
        int y;

        x = _defaultX;
        y = _defaultY;

        e.Graphics.Clear(palettePanel.BackColor);

        foreach (Color color in _loadedPalette)
        {
          Rectangle bounds;

          if (x > palettePanel.Width - (_cellSize + _defaultX))
          {
            x = _defaultX;
            y += _defaultY + _cellSize + _spacing;
          }

          bounds = new Rectangle(x, y, _cellSize, _cellSize);

          using (Brush brush = new SolidBrush(color))
            e.Graphics.FillRectangle(brush, bounds);

          e.Graphics.DrawRectangle(Pens.Black, bounds);

          x += _cellSize + _spacing;
        }
      }
    }

    #endregion
  }
}

Donate

Donate