1
當我右鍵單擊一個圖片框時,通過使用上下文菜單項,我顯示一個菜單項saveImageAs。將右鍵單擊菜單項添加到圖片框
問題:當我右鍵點擊圖片框,它顯示saveImageAs,當我點擊saveImageAs它會打
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;
}
這裏我應該怎麼用的,而不是pictureBox1_click()
打form_MouseClick()
。如果有人能幫助,我會非常感激。
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;'在多次引發的事件中,會將多個事件處理程序添加到同一個事件中。這應該避免。順便說一句你的問題 –
謝謝@NilayVishwakarma,如果我打電話pictureBox1.Click + = form_MouseClick,它採取鼠標單擊,我想使用上下文菜單項SaveimageAs點擊 –
爲什麼你在Form.MouseClick中顯示savefiledialog? –