2
votes

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);
}
2
Calling these lines 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 questionNilay Vishwakarma
Thanks @NilayVishwakarma, if i call pictureBox1.Click += form_MouseClick, it is taking mouse click, i want to use context menu items SaveimageAs clickNag Arjun Reddy
Why are you displaying savefiledialog in Form.MouseClick?Nilay Vishwakarma
To save a image in selected folderNag Arjun Reddy

2 Answers

0
votes

Add a ContextMenuStrip to your Form. Add an Item 'SaveImageAs' to your context menu. Set the ContextMenuStrip as ContextMenu for PictureBox via this line:

this.pictureBox1.ContextMenuStrip = this.contextMenuStrip1;

Rest of the code is self-explanatory

    private void saveImageAsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        ShowSaveDialog();
    }

    private void ShowSaveDialog()
    {
        PictureBox pb = pictureBox1;
        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);
    }
0
votes

Give it a shot!

using Module = System.Windows.Forms;

public Form1()
{
    var menu = new Module.ContextMenuStrip();

    {
        var submenu = new Module.ToolStripMenuItem();

        submenu.Text = "Sub-menu 1";

        var item = new Module.ToolStripMenuItem();

        item.Text = "Sub-item 1";
        item.MouseUp += (object sender,MouseEventArgs e) =>
        {
            // Todo
        };
        submenu.DropDownItems.Add( item );

        item = new Module.ToolStripMenuItem();
        item.Text = "Sub-item 2";
        submenu.DropDownItems.Add( item );

        menu.Items.Add( submenu );
    }

    pictureBox1.ContextMenuStrip = menu;
}