如何从几个图片框中选择一个图片框来输入图像

时间:2015-05-20 06:52:09

标签: c# image-processing picturebox

我已经从Form1中的用户输入了图像输入,而不是将该图像分割成多个图像并在Form2中显示它现在我希望用户再次从几个切片图像中选择一个图像(图像的每个切片都显示在一个图像中)单独的图片框)所以它将再次将所选图像返回到form1,它显示DataGridView中的所选图像。下面的代码已切片图像。我认为有些onclick或select属性会应用但是怎么样?我不懂。

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

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

1 个答案:

答案 0 :(得分:0)

以下是我认为您正在寻找的一个例子,我希望我能正确理解您。

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