hi there, i'm creating an app in C# which allows users to select an image by double clicking the "pictureBox1", from there it selects the image, creates a new Graphics object and passes the Bitmap to it.
Once the picture has been loaded the user should be able to move the picture around using the mouseDown, mouseMove, mouseUp events.
I have got the image to load correctly, but it does not move.. if i alter the code, it moves, but the user is unable to select a new image(by double clicking), and it will just load a box with a red border instead.
I am fairly new to C#, so i'm learning as i go. Any tips or advice will be appriciated.
these are my methods:.
private void pictureBox1_DoubleClick(object sender, EventArgs e)
{
if (pictureBox1.Image != null) //check if picture is already there, if so, delete it.
{
pictureBox1.Image.Dispose();
}
openFD.InitialDirectory = "C:";
openFD.Title = "Select an Image";
openFD.FileName = "";
openFD.Filter = "JPEG Images|*.jpg|GIF Images|*.gif|BMP Images|*.bmp";
if (openFD.ShowDialog() == DialogResult.Cancel)
{
MessageBox.Show("cancelled");
}
else
{
Chosen_File = openFD.FileName;
MyBitmap = Image.FromFile(Chosen_File);
// Create graphics object for alteration.
newGraphics = Graphics.FromImage(MyBitmap);
pictureBox1.Image = MyBitmap;
this.Refresh();
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
isDragging = true;
oldX = e.X;
oldY = e.Y;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
xMouse = e.X;
yMouse = e.Y;
Refresh();
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
newX = e.X;
newY = e.Y;
}
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawImage(pictureBox1.Image, xMouse, yMouse);
}