将右键菜单项添加到图片框

时间:2017-04-18 05:24:03

标签: c# winforms c#-4.0

当我右键单击图片框时,通过使用上下文菜单项我显示一个菜单项saveImageAs。

问题:当我右键单击图片框时,会显示saveImageAs,当我点击saveImageAs时会点击

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); } 。如果有人能提供帮助,我将非常感激。

Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: java.lang.RuntimeException: java.lang.RuntimeException: Unable to pre-dex 'C:\Users\xxxx\.android\build-cache\8a8f5dc798b45b1d3ff133df3988ae430f7e4158\output\jars\classes.jar' to 'F:\yyyyy\happy \app\build\intermediates\transforms\dex\debug\folders\1000\10\classes_88e225087cead8e4043cef843dd9c6adee64176a'

2 个答案:

答案 0 :(得分:0)

将ContextMenuStrip添加到表单中。添加项目' SaveImageAs'到您的上下文菜单。通过以下行将ContextMenuStrip设置为PictureBox的ContextMenu:

this.pictureBox1.ContextMenuStrip = this.contextMenuStrip1;

其余代码不言自明

    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);
    }

答案 1 :(得分:0)

试一试!

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;
}