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++;
}
}
}
If I move the trackBar to the right it will save the first file 000000.bmp then raise it by one next time it will save file 000001.bmp
But if I move back to the left once now the variable fn is 000002.bmp which doesn’t exist and it will save the previous image which is really 000001.bmp.
What it suppose to do when I move to the left back it should be 000001.bmp see that it exist and do nothing.
If will not make this checking it will just keep saving files all the time if i move the trackBar to the right or to the left so after few times i will have more then 90 files which are almost all the same.
How can I solve it?
The variable PbtoBitmap is type of int in the top of Form1 I just started it with 0.
PbToBitmap = 0;
The trackBar I’m talking about is trackBar1 where in the scroll event I’m calling this SaveFormPicutreBoxToBitMapIncludingDrawings function .
This is the trackBar1 scroll event:
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;
}
It is not clear what you are trying to accomplish, but you are generating file names from a counter
PbToBitmap. This counter only increases and never decreases so of course it will “just keep saving files… “.If you want the counter to match the frame you’re on, get rid of
PbToBitmapand insidetrackBar1_Scrollcall:Change your
SaveFormPictureBoxToBitMapIncludingDrawingsto: