图片框中的多个图像 [英] Multiple images in a picturebox

查看:90
本文介绍了图片框中的多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程的初学者,我正在尝试自己学习它。所以我必须在图片框中添加2个不同的图像(在这种情况下是一个桌子和一个链接到2个工具条的按钮)。我需要放置多个桌椅,以便我可以移动它们或者之后移除它们。但当我放置桌子并想要添加椅子时,我的桌子消失了。

是否还有一种方法可以保存图片框中图像的位置,所以当我重新打开项目时,放置的图像将会打开他们以前的位置?



我尝试过:



这是主要形式

I'm a beginner in programming and I'm trying to learn it by myself.So I have to add 2 different images to a picturebox ( in this case a table and a chair that are linked to 2 toolstripbuttons). I need to place multiple tables and chairs so I can move them or remove them afterwards. But when I placed tables and want to add the chairs my tables disappear.
Is there also a way to save the locations of the images in the picturebox so when I reopen the project the placed images will be on their previous locations?

What I have tried:

This is the main form

<pre>namespace WORKSPACE
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        SPACE space;

        enum OPTION
        {
            PLACE,
            REMOVE,
            REPLACE
        }

        OPTION OPTIONS = OPTION.PLACE;

        Image figure;

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void pLACEToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OPTIONS = OPTION.PLACE;
        }

        private void rEMOVEToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OPTIONS = OPTION.REMOVE;

        }

        private void rEPLACEToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OPTIONS = OPTION.REPLACE;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel1.Text = "X:" + e.X.ToString() + "Y:" + e.Y.ToString();
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (OPTIONS== OPTION.PLACE)
            {
                space.PLACE(e.X, e.Y);
            }
            else
                if (OPTIONS == OPTION.REPLACE)
            {
                space.REPLACE(e.X, e.Y);
            }
            else
                if (OPTIONS == OPTION.REMOVE)
            {
                space.REMOVE(e.X, e.Y);
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            figure = toolStripButton1.Image;
            space = new SPACE(pictureBox1.CreateGraphics(), figure);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            figure = toolStripButton2.Image;
            space = new SPACE(pictureBox1.CreateGraphics(), figure);
        }
    }
}
This is the class I created


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace WORKSPACE
{
    class SPACE
    {
        int COUNTER = 0;
        
        
        Graphics G;
        List<Rectangle> SPACES;
        Rectangle MOUSE;
        Boolean STATUSREPLACE = false;
        int SPACECOUNTER;
        int DUMMY;
        Image figure;
        

        public SPACE(Graphics g,Image f)
        {
            G = g;
            figure = f;
          
            
            SPACES = new List<Rectangle>();
        }

        public void PLACE(int X, int Y)
        {
            COUNTER++;
            SPACES.Add(new Rectangle(X, Y, 20, 20));
            DRAWSPACE();
        }

        public void DRAWSPACE()
        {
            SPACETELLER = 1;
            G.Clear(Color.White);
            foreach (Rectangle r in SPACES)
            {
                G.DrawImage(figure,r.X,r.Y);
                SPACECOUNTER++;
            }
        }
        public void DRAWSPACECHAIR()
        {
            SPACECOUNTER = 1;
            G.Clear(Color.White);
            foreach (Rectangle r in SPACES)
            {
                G.DrawImage(figure, r.X, r.Y);
                SPACECOUNTER++;
            }
        }

        public void REPLACE(int X, int Y)
        {
            SPACECOUNTER = 1;
            MOUSE = new Rectangle(X, Y, 2, 2);
            foreach (Rectangle r in SPACES)
            {
                if (MOUSE.IntersectsWith(r))
                {
                    if (STATUSREPLACE == false)
                    {
                        SPACES.Remove(r);
                        STATUSREPLACE = true;
                        DUMMY = SPACECOUNTER;
                        return;
                    }
                }
                SPACECOUNTER++;
            }
            if (STATUSREPLACE == true)
            {
                SPACES.Insert(--DUMMY, new Rectangle(X, Y, 20, 20));
                STATUSREPLACE = false;
                DRAWSPACE();
                DRAWSPACECHAIR();
            }
        }
        public void REMOVE(int X, int Y)
        {
            SPACECOUNTER = 1;
            MOUSE = new Rectangle(X, Y, 2, 2);
            foreach (Rectangle r in SPACES)
            {
                if (MOUSE.IntersectsWith(r))
                {
                    
                    SPACES.Remove(r);
                    break;
                    
                }
                
            }
            DRAWSPACE();
            DRAWSPACECHAIR();
        }

    }  
}

推荐答案

你只能放一张图片一个PictureBox控件,所以你必须自己绘制图像。说实话,我根本不会使用PictrueBox,但是你正在学习,保留它。



你必须创建一个新的Bitmap,画你的在该位图中需要的图像,然后设置PictureBox以显示新图像。



使用GDI图形对象时,请确保在对象上调用Dispose。你有全局变量变量,这是不好的做法。您只需在需要的地方声明它们。
You can only put ONE image in a PictureBox control, so you have to paint the images yourself. Truthfully, I wouldn't use a PictrueBox for this at all, but scine you're learning, keep it.

You have to create a new Bitmap, paint your required images in that Bitmap, then set the PictureBox to show the new image.

When using the GDI graphic objects, make sure you call Dispose on the objects. You've got global-everyhing variables, which is bad practice. You only declare them where you need them.


这篇关于图片框中的多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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