首页>Program>source

我有问题:

我有3个带有3个不同图像的图片框 as in Image

我可以设置为 pictureBox3 所以两个图像看起来都一样.....

EDITED: 我想在pictureBox2上移动pictureBox3,

因此没有选择将它们合并到单个图像中

最新回答
  • 2021-1-11
    1 #

    我将添加另一个示例,根据更新的要求,该示例允许移动图像3。
    为了使其正常工作,请将透明图像放入 Resources\transp.png
    这对所有三个图像都使用相同的图像,但是您只需将image1和image2的transparentImg替换为合适的图像即可。

    演示开始后,可以将中间图像拖放到表单周围。

    public partial class Form1 : Form
    {
        private readonly Image transparentImg; // The transparent image
        private bool isMoving = false;         // true while dragging the image
        private Point movingPicturePosition = new Point(80, 20);   // the position of the moving image
        private Point offset;   // mouse position inside the moving image while dragging
        public Form1()
        {
            InitializeComponent();
            // 
            // pictureBox1
            // 
            this.pictureBox1.Location = new System.Drawing.Point(0, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(231, 235);
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
            this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
            this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
            this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
            this.Controls.Add(this.pictureBox1);
            transparentImg = Image.FromFile("..\\..\\Resources\\transp.png");
        }
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            var g = e.Graphics;
            g.DrawImageUnscaled(transparentImg, new Point(20, 20));      // image1
            g.DrawImageUnscaled(transparentImg, new Point(140, 20));     // image2
            g.DrawImageUnscaled(transparentImg, movingPicturePosition);  // image3
        }
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            var r = new Rectangle(movingPicturePosition, transparentImg.Size);
            if (r.Contains(e.Location))
            {
                isMoving = true;
                offset = new Point(movingPicturePosition.X - e.X, movingPicturePosition.Y - e.Y);
            }
        }
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMoving)
            {
                movingPicturePosition = e.Location;
                movingPicturePosition.Offset(offset);
                pictureBox1.Invalidate();
            }
        }
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            isMoving = false;
        }
    }
    

  • 2021-1-11
    2 #

    确保在 pictureBox3中显示图像 是透明的.设置 BackColor 透明.在代码中,设置 Parent pictureBox3的属性 成为 pictureBox2 .调整 Location pictureBox3的坐标 因为它们将相对于 pictureBox2的坐标 一旦您更改了 Parent

       private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox3.Parent = pictureBox2;
            pictureBox3.Location =
                new Point(
                    pictureBox3.Location.X
                    - pictureBox2.Location.X,
                    pictureBox3.Location.Y
                    - pictureBox2.Location.Y);
        }
    

    在设计器中您不会看到透明性,但是在运行时您将看到透明性。

    Update

    在图像中,左侧显示设计器视图,右侧是运行时版本。

    Another update

    我真的不明白这怎么可能对您不起作用.我想我们必须做些不同的事情.我将描述创建工作样本所要采取的确切步骤.如果您遵循完全相同的步骤,我想知道我们是否会获得相同的结果.后续步骤描述了如何使用并使用我在网上发现的两张图片。

      Using Visual Studio 2008, create a New Project using template Windows Forms Application. Make sure the project is targeted at the .NET Framework 3.5.

      Set the Size of the Form to 457;483.

      Drag a PictureBox control onto the form. Set its Location to 0;0 and its Size to 449;449.

      Click the ellipsis besides its Image property, click the Import... button and import the image at http://a.dryicons.com/files/graphics_previews/retro_blue_background.jpg (just type the URL in the File name text box and click Open). Then click OK to use the image.

      Drag another PictureBox onto the form, set its Location to 0;0 and its Size to 256;256. Also set its BackColor property to Transparent.

      Using the same method as described above, import image http://www.axdn.com/redist/axiw_i.png which is a transparent image.

      Now place the following code in the form's OnLoad event handler:

       
      private void Form1_Load(object sender, EventArgs e)
      {
          pictureBox2.Parent = pictureBox1;
      }
      

      就是这样! 如果我运行此程序,则会在另一个图像的顶部获得透明图像。

  • 2021-1-11
    3 #

    对于初学者,请设置 BackColor PictureBox3的"透明"属性.几乎在所有情况下都可以使用。

    您还应该使用背景透明而不是白色的图像,这样紫色圆圈周围就不会出现白色边框. (推荐图片格式:PNG)


    Update
    在收到我的回复后,似乎设置了 BackColor 透明不起作用.在这种情况下,最好您处理 Paint PictureBox事件,并按照Albin的建议自己绘制新图像。

  • 2021-1-11
    4 #

    此代码可以解决问题:

    using (Graphics g = Graphics.FromImage(pictureBox1.Image))
    {
        g.DrawImage(pictureBox2.Image, 
            (int)((pictureBox1.Image.Width - pictureBox2.Image.Width) / 2),
            (int)((pictureBox1.Image.Height - pictureBox2.Image.Height) / 2));
        g.Save();
        pictureBox1.Refresh();
    }
    

    它将在pictureBox1的现有图像上绘制来自pictureBox2的图像。

  • 2021-1-11
    5 #

    您可能会通过覆盖OnPaint和其他内容(例如此处的示例)来进行一些破解。

    但是我建议将pictureBox2和3中的图片合并为一个图像,然后再将其显示在单个pictureBox中。

  • php:警告:mysqli_real_escape_string()恰好需要2个参数,给定1个……我做错了什么?
  • java:局部变量的垃圾回收