How do i save the current frame/image in pictureBox1 to a bitmap file ?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I tried to make this function:

<pre class="prettyprint private void SavePictureBoxToBitmap()
{
PbToBitmap++;
String tempFile = @"d:PictureBoxToBitmap" + PbToBitmap.ToString("D6") + ".bmp";
pictureBox1.Image.Save(tempFile);
}[/code]
<br/>

And i want it to make the saving only when i move the trackBar1 scroller to the right:

<pre class="prettyprint private void trackBar1_Scroll(object sender, EventArgs e)
{
LoadPictureAt(trackBar1.Value, sender);
currentFrameIndex = trackBar1.Value;
textBox1.Text = "Frame Number : " + trackBar1.Value;

int idx = -1;

try
{
var w = wireObjectAnimation1._coordinatesList.First(a => a.FrameNumber == currentFrameIndex);
idx = wireObjectAnimation1._coordinatesList.IndexOf(w);
}
catch
{

}

if (idx > -1)
{
// *** When moving the trackBar1 scroll to the right ***\

if (idx == 0)
{
//*** If moved the scroll to the first frame to the left ***

// button8.Enabled = true;
}
wireObjectCoordinates1 = wireObjectAnimation1._coordinatesList[idx];
//...
//do the things you want to do when the list contains the object
wireObject1._point_X = new List<float>(wireObjectCoordinates1.Point_X);
wireObject1._point_Y = new List<float>(wireObjectCoordinates1.Point_Y);
}
else
{
// *** When moving the trackBar1 scroll to the right ***\

wireObjectCoordinates1 = new WireObjectCoordinates() { FrameNumber = currentFrameIndex };
wireObjectAnimation1._coordinatesList.Add(wireObjectCoordinates1);
WireObjectCoordinatesCloneFrame();
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button8.Enabled = false;
SavePictureBoxToBitmap();
}

}[/code]
I used a breakpoint when its getting to the SavePictureBoxToBitmap();
And its doing the pictureBox1.Image.SAve....But on the hard disk nothing is saved.
I need to save from the pictureBox1 the background image/frame and also the points i drawed on the pictureBox. So maybe i need to do it somehow in the paint event ?

This is the paint event:

<pre class="prettyprint private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
SolidBrush brush;
Pen p=null;
Point connectionPointStart;
Point connectionPointEnd;
Graphics g = e.Graphics;
//g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

moveCounter++;
label6.Text = moveCounter.ToString();

brush = new SolidBrush(Color.Red);
p = new Pen(brush);
for (int idx = 0; idx < wireObject1._point_X.Count; ++idx)
{
Point dPoint = new Point((int)wireObject1._point_X[idx], (int)wireObject1._point_Y[idx]);
dPoint.X = dPoint.X - 5; // was - 2
dPoint.Y = dPoint.Y - 5; // was - 2
Rectangle rect = new Rectangle(dPoint, new Size(10, 10));
g.FillEllipse(brush, rect);


// g.FillEllipse(brush, rect);
}

for (int i = 0; i < wireObject1._connectionstart.Count; i++)
{

int startIndex = wireObject1._connectionstart;
int endIndex = wireObject1._connectionend;



connectionPointStart = new Point((int)wireObject1._point_X[startIndex], (int)wireObject1._point_Y[startIndex]);
connectionPointEnd = new Point((int)wireObject1._point_X[endIndex], (int)wireObject1._point_Y[endIndex]);
p.Width = 2;
g.DrawLine(p, connectionPointStart, connectionPointEnd);
}

}[/code]
<br/>
I dont want to save only the image of the pictureBox1 but also what is drawed on it.
<hr class="sig danieli

View the full article
 
Back
Top