如何从多个图片框中选择一个图片框进行图像输入

本文关键字:一个 输入 图像 选择 | 更新日期: 2023-09-27 17:49:46

我在Form1中从用户获取了一个图像输入,然后我将该图像切成几个图像并在Form2中显示它,现在我希望用户再次从几个切片图像中选择一个图像(图像的每个切片显示在一个单独的picturebox中),因此它将再次返回所选图像到Form1,这显示了在DataGridView中选择的图像。下面的代码将图像切片。我认为一些onclick或select属性将被应用,但如何应用?我不明白。

{
    frm = thumb_pic.Image;
    int widthThird = (int)((double)frm.Width / 25.0 + 0.5);
    int heightThird = (int)((double)frm.Height / 20.0 + 0.5);
    Bitmap[,] bmps = new Bitmap[20, 25];
    for (int i = 0; i < 20; i++)
        for (int j = 0; j < 25; j++)
        {
            bmps[i, j] = new Bitmap(widthThird, heightThird);
            Graphics g = Graphics.FromImage(bmps[i, j]);
            g.DrawImage(frm, new Rectangle(0, 0, widthThird, heightThird),      
                        new Rectangle(j * widthThird, i * heightThird, widthThird, heightThird), 
                        GraphicsUnit.Pixel);
            g.Dispose();
        }
}

............................................................................

private void pictureBox1_Click(object sender, EventArgs e)
{
    pb_selected.Image = pictureBox1.Image;
}
private void pictureBox2_Click(object sender, EventArgs e)
{
    pb_selected.Image = pictureBox2.Image;
}

实际上我想在每个图片框上都这样做,但是当我们有数百个图片框时,这将成为一个硬代码并且非常困难。所以我只想知道要设置的事件或属性,它返回所选图片框的ID

如何从多个图片框中选择一个图片框进行图像输入

这是我认为你正在寻找的一个例子,我希望我正确地理解了你。

public partial class SampleForm : Form
{
    private List<ImageBox> imageBoxes = new List<ImageBox>();
    public SampleForm()
    {
        InitializeComponent();
        // pictureBox1.Load("C:''sample.png");
        // Panel1 initialized as NonFlickeringPanel in code behind
        LoadImages();
        panel1.Invalidate();
    }
    private void LoadImages()
    {
        if (imageBoxes.Count > 0)
            imageBoxes.Clear();
        const int boxWidth = 25;
        const int boxHeight = 20;
        var img = pictureBox1.Image;
        int widthThird = (int)((double)img.Width / boxWidth + 0.5);
        int heightThird = (int)((double)img.Height / boxHeight + 0.5);
        for (int i = 0; i < boxHeight; i++)
        {
            for (int j = 0; j < boxWidth; j++)
            {
                var bitmap = new Bitmap(widthThird, heightThird);
                var rect = new Rectangle(j * widthThird, i * heightThird, widthThird, heightThird);
                Graphics g = Graphics.FromImage(bitmap);
                g.DrawImage(img, new Rectangle(0, 0, bitmap.Width, bitmap.Height), rect, GraphicsUnit.Pixel);
                g.Dispose();
                imageBoxes.Add(new ImageBox() { Image = bitmap, Rectangle = rect });
            }
        }
    }
    private void panel1_MouseDown(object sender, MouseEventArgs e)
    {
        var p = e.Location;
        foreach (var imgBox in imageBoxes)
        {
            if (imgBox.Rectangle.Contains(p))
                imgBox.Selected = true; // do whatever you want with the selected image using imgBox.Image
            else
                imgBox.Selected = false;
        }
        panel1.Invalidate();
    }
    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        if (imageBoxes == null || imageBoxes.Count == 0)
            return;
        foreach (var imgBox in imageBoxes)
        {
            var bitmap = imgBox.Image;
            var rect = imgBox.Rectangle;
            e.Graphics.DrawImage(bitmap, rect, new Rectangle(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
            if (imgBox.Selected)
                e.Graphics.DrawRectangle(new Pen(Color.Red, 2), rect);
        }
    }
}
public class ImageBox
{
    public Bitmap Image { get; set; }
    public Rectangle Rectangle { get; set; }
    public bool Selected { get; set; }
}
public class NonFlickeringPanel : System.Windows.Forms.Panel
{
    public NonFlickeringPanel()
    {
        this.SetStyle(
            System.Windows.Forms.ControlStyles.UserPaint |
            System.Windows.Forms.ControlStyles.AllPaintingInWmPaint |
            System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer,
            true);
    }
}