1

私の質問は次のとおりです: タイマーがあり、1 ミリ秒ごとに画像を読み込み、win フォームの画像ボックスに表示したい 最初の画像は非常にうまく機能しますが、ある時点から画像の読み込みが難しくなります。この問題を解決する方法を教えてください。

お時間をいただきありがとうございます:)

   private string path = "";
    private int index = 0;
    private void fileSourceToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            string fullPath = ofd.FileName;
            string fileName = ofd.SafeFileName;

            path = fullPath.Replace(fileName, "");

            MessageBox.Show("Path for file source has been selected");
        }
    }

    private const string IMAGENAME = "TestImage";
    Bitmap b;
    private void timer_Tick(object sender, EventArgs e)
    {
        try
        {
            string currentImage = IMAGENAME + index.ToString() + ".bmp";
            index++;
            b = new Bitmap(path + "\\" + currentImage);

            pb.SizeMode = PictureBoxSizeMode.StretchImage;
            pb.Image = b;
        }
        catch { };

    }

    private void button1_Click(object sender, EventArgs e)
    {
        timer.Start();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        timer.Stop();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        try
        {
            string currentImage = IMAGENAME + index.ToString() + ".bmp";
            index += 1;
            Bitmap b = new Bitmap(path + "\\" + currentImage);

            pb.Image = b;
        }
        catch { };
    }

    private void button4_Click(object sender, EventArgs e)
    {
        try
        {
            index -= 1;
            string currentImage = IMAGENAME + index.ToString() + ".bmp";
            Bitmap b = new Bitmap(path + "\\" + currentImage);
            pb.Image = b;
        }
        catch { };
    }
4

2 に答える 2

0

GDI+ は非常に遅く、ダブル バッファリングを使用している場合でも低速です。「ちらつき」を修正する方法が実際にはないため、代替手段を見つけることをお勧めします。

ただし、ティックハンドルに実際に画像をロードする方法は、CPU をかなり消費します。

必要な画像のすべてのバイトを何らかのコレクションにロードすることをお勧めします。配列。次に、画像バイトからビットマップを作成するだけです。

さらに、次の画像に新しいメモリを割り当てる前に、現在の画像を破棄する必要さえありません。

于 2013-11-10T17:34:42.213 に答える