はじめまして。
C#初心者で色々と分からないため、質問させていただきます。
現在、C#にてフォルダ内の画像を取得し、一枚ずつPicutreBoxに動画のように表示していこうとしているのですが、なかなかうまくいかず困っております。
現状試したことは、GIFに変換生成してからPictureBoxに表示するですが、これはGIFが再生されない上、メモリを多く食ってしまうため、諦めました。
お知恵を拝借できれば幸いです。
スタック・オーバーフローはプログラマーとプログラミングに熱心な人のためのQ&Aサイトです。すぐ登録できます。
登録してこのコミュニティに参加するはじめまして。
C#初心者で色々と分からないため、質問させていただきます。
現在、C#にてフォルダ内の画像を取得し、一枚ずつPicutreBoxに動画のように表示していこうとしているのですが、なかなかうまくいかず困っております。
現状試したことは、GIFに変換生成してからPictureBoxに表示するですが、これはGIFが再生されない上、メモリを多く食ってしまうため、諦めました。
お知恵を拝借できれば幸いです。
質問文から、求める回答が読み取れませんでした。
類似の質問を見かけることの多い、下記3点のサンプルコードをご提示します。
サンプルコードを動作させるにはWindows FormsにPictureBox(pictureBox1)
とTimer(timer1)
を貼り付けます。
Form
にLoad
とShown
イベントを、PictureBox
にMouseClick
イベントを追加し、Timer
にTick
イベントを追加する必要があります。
using System;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private int Index;
private bool IsMouseMode;
public Form1()
{
InitializeComponent();
IsMouseMode = false;
}
private void Form1_Load(object sender, EventArgs e)
{
//アニメーションの元画像を64枚作成しておく
CreateSampleImage();
}
private void CreateSampleImage()
{
var bmp = new Bitmap(64, 64);
var range = Enumerable.Range(0, 64);
//背景を適当に塗りつぶし
var zip = from x in range
from y in range
select new { x, y };
zip.ToList().ForEach(p => { bmp.SetPixel(p.x, p.y, Color.Black); });
//正弦派的なお絵描き
range.Select(x =>
{
var rad = System.Math.PI * x / 12d;
return new { x = x, y = (int)(Math.Sin(rad) * 5 + bmp.Height / 2) };
}).ToList().ForEach(p =>
{
bmp.SetPixel(p.x, p.y, Color.White);
bmp.Save(string.Format("{0}.png", p.x), ImageFormat.Png);
});
}
private void Form1_Shown(object sender, EventArgs e)
{
Text = "最速アニメーション";
DrawRapidly();
Text = "10FPSアニメーション";
DrawOnTimer(100); //終了するとクリックアニメーションモードになる
}
/// <summary>
/// とにかく最速でアニメーション描画する
/// </summary>
private void DrawRapidly()
{
pictureBox1.WaitOnLoad = true;
Enumerable.Range(0, 64).ToList().ForEach(i =>
{
pictureBox1.Load(string.Format("{0}.png", i));
pictureBox1.Update();
});
}
/// <summary>
/// intervelの周期に従って描画する
/// </summary>
/// <param name="interval"></param>
private void DrawOnTimer(int interval)
{
timer1.Interval = interval;
pictureBox1.WaitOnLoad = false;
Index = 0;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1.LoadAsync(string.Format("{0}.png", Index));
pictureBox1.Update();
Index++;
if(64 <= Index)
{
timer1.Stop();
DrawOnClick();
}
}
private void DrawOnClick()
{
Text = "クリックアニメーション";
IsMouseMode = true;
Index = 0;
}
/// <summary>
/// 左クリックで線が伸びる。それ以外のクリックで線が縮むよう描画する
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
if (!IsMouseMode) return;
Index += e.Button == MouseButtons.Left ? 1 : -1;
Index = Math.Max(Math.Min(Index, 63), 0);
pictureBox1.LoadAsync(string.Format("{0}.png", Index));
}
}
}
もしも質問したい内容が下記のような場合は、具体的な質問やサンプルコードの追記をご検討ください。