-2
public void SaveFormPicutreBoxToBitMapIncludingDrawings()
{
    using (Bitmap b = new Bitmap(pictureBox1.Width, pictureBox1.Height))
    {
        pictureBox1.DrawToBitmap(b, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
        string fn = @"d:\PictureBoxToBitmap\" + PbToBitmap.ToString("D6") + ".bmp";
        if (File.Exists(fn))
        {
        }
        else
        {
            b.Save(@"d:\PictureBoxToBitmap\" + PbToBitmap.ToString("D6") + ".bmp"); // to fix/repair give gdi error exception.
            PbToBitmap++;
        }
    } 
}

トラックバーを右に移動すると、最初のファイル 000000.bmp が保存され、次にファイル 000001.bmp が保存されます。

しかし、一度左に戻ると、変数 fn は存在しない 000002.bmp になり、実際には 000001.bmp である前の画像が保存されます。

左奥に移動したときの動作は 000001.bmp である必要があります。存在することを確認し、何もしません。

このチェックを行わない場合、トラックバーを右または左に移動すると、常にファイルが保存され続けるため、数回後にはほぼすべて同じ90個以上のファイルが作成されます。

どうすれば解決できますか?

変数 PbtoBitmap は、Form1 の上部にある int 型です。0 から開始しました。PbToBitmap = 0;

私が話している trackBar は trackBar1 で、スクロール イベントでこの SaveFormPicutreBoxToBitMapInclusiveDrawings function を呼び出しています。

これは trackBar1 スクロール イベントです。

private void trackBar1_Scroll(object sender, EventArgs e)
{
    currentFrameIndex = trackBar1.Value;
    textBox1.Text = "Frame Number : " + trackBar1.Value;
    wireObject1.woc.Set(wireObjectAnimation1.GetFrame(currentFrameIndex)); 

    trackBar1.Minimum = 0;
    trackBar1.Maximum = fi.Length - 1;
    setpicture(trackBar1.Value);
    pictureBox1.Refresh();

    button1.Enabled = false;
    button2.Enabled = false;
    button3.Enabled = false;
    button4.Enabled = false;
    button8.Enabled = false;
    SaveFormPicutreBoxToBitMapIncludingDrawings();
    return;
}
4

1 に答える 1

1

何を達成しようとしているのかは明確ではありませんが、カウンターからファイル名を生成していますPbToBitmap。このカウンターは増加するだけで減少することはないため、もちろん「ファイルを保存し続けるだけです...」.

カウンターを現在のフレームと一致させたい場合は、呼び出しPbToBitmapを取り除きます。trackBar1_Scroll

string dir = @"d:\PictureBoxToBitmap";
SaveFormPictureBoxToBitMapIncludingDrawings(dir, currentFrameIndex);

次のように変更SaveFormPictureBoxToBitMapIncludingDrawingsします。

public void SaveFormPictureBoxToBitMapIncludingDrawings(string dir, int frameIndex)
{
    string fn = Path.Combine(dir, frameIndex.ToString("D6") + ".bmp");
    if (!File.Exists(fn))
    {
        using (Bitmap b = new Bitmap(pictureBox1.Width, pictureBox1.Height))
        {
            pictureBox1.DrawToBitmap(b, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
            b.Save(fn);
        }
    } 
}
于 2012-07-24T00:43:27.130 に答える