如何删除图像的硬编码路径 [英] How to remove the hard coded path for image

查看:88
本文介绍了如何删除图像的硬编码路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我试图复制并保存图片框中的图像。

我有五个图片框。



但是我am硬编码image im = PictureBox1.Image;

和图像名称,



这样我可以复制并保存图像picturebox1。

如何从其他图片框中复制图像。

Here I am trying to copy and save the image from picture box.
I am having five picture box.

but I am Hardcoded the "image im = PictureBox1.Image;"
and image name,

this way i can copy and save the image from picturebox1.
How to copy the image from other picture box.

private void MouseClick()
            {     
                PictureBox1.MouseClick += form_MouseClick;
              
                PictureBox2.MouseClick += form_MouseClick;
    
                PictureBox3.MouseClick += form_MouseClick;
    
                PictureBox4.MouseClick += form_MouseClick;

                PictureBox5.MouseClick += form_MouseClick;
            }
    		
    	private void frmRightClick_Disposed(object sender, EventArgs e)
            {
                folderBrowser.ShowDialog();
                string filepath = folderBrowser.SelectedPath;
                Image im = PictureBox1.Image;
                var obj = new Random();
                SaveImage(im, filepath + "\\" + obj.Next() + ".png");
            }
    		
    		private void SaveImage(Image im, string destPath)
            {
                im.Save(destPath, System.Drawing.Imaging.ImageFormat.Png);
            }





我的尝试:



我尝试使用发件人



What I have tried:

I tried using the sender

private void frmRightClick_Disposed(object sender, EventArgs e)
            {
                folderBrowser.ShowDialog();
                string filepath = folderBrowser.SelectedPath;
                Image im = PictureBox1.Image;
                var obj = new Random();
                SaveImage(im, filepath + "\\" + obj.Next() + ".png");
            }

推荐答案

事件处理程序的 sender 参数是出于这个原因:

The sender parameter of the event handler is there for just that reason:
private void form_MouseClick(object sender, EventArgs e)
    {
    PictureBox pb = sender as PictureBox;
    if (pb != null)
        {
        folderBrowser.ShowDialog();
        string filepath = folderBrowser.SelectedPath;
        Image im = pb.Image;
        var obj = new Random();
        SaveImage(im, filepath + "\\" + obj.Next() + ".png");
        }
    }


这篇关于如何删除图像的硬编码路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆