When I right-click on a picturebox, by using context menu items I am showing a menu item saveImageAs.
Problem: When I right click on the picture box, it shows saveImageAs, when I click saveImageAs it will hit
private void saveImageAsToolStripMenuItem_Click(object sender, EventArgs e)
{
//what should i use instead of click to hit form_Mouseclick
pictureBox1.Click += form_MouseClick;
pictureBox2.Click += form_MouseClick;
}
Here what should I use instead of pictureBox1_click()
to hit form_MouseClick()
. If anyone could help I would be most grateful.
private void saveImageAsToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.Click += form_MouseClick;
pictureBox2.Click += form_MouseClick;
}
private void form_MouseClick(object sender, MouseEventArgs e)
{
PictureBox pb = sender as PictureBox;
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Images|*.png;*.bmp;*.jpg";
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string filepath = System.IO.Path.GetExtension(sfd.FileName);
}
if(pb != null && sfd.FileName != null)
{
Image im = pb.Image;
SaveImage(im, sfd.FileName);
}
}
private static void SaveImage(Image im, string destPath)
{
im.Save(destPath, System.Drawing.Imaging.ImageFormat.Png);
}
pictureBox1.Click += form_MouseClick;
pictureBox2.Click += form_MouseClick;
in an event which is raised multiple time would add multiple event handlers to same event. This should be avoided. BTW what is your question – Nilay Vishwakarma