首頁>Program>source

我有問题:

我有3个帶有3个不同圖像的圖片框 as in Image

我可以設置為 pictureBox3 所以两个圖像看起来都一樣.....

EDITED: 我想在pictureBox2上移動pictureBox3,

因此没有選擇將它们合並到單个圖像中

最新回復
  • 5月前
    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;
        }
    }
    

  • 5月前
    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;
      }
      

      就是這樣! 如果我執行此程式,則会在另一个圖像的頂部获得透明圖像。

  • 5月前
    3 #

    對於初學者,請設置 BackColor PictureBox3的"透明"屬性.几乎在所有情况下都可以使用。

    您還應该使用背景透明而不是白色的圖像,這樣紫色圆圈週圍就不会出現白色邊框. (推荐圖片格式:PNG)


    Update
    在收到我的迴複後,似乎設置了 BackColor 透明不起作用.在這種情况下,最好您處理 Paint PictureBox事件,並按照Albin的建議自己绘製新圖像。

  • 5月前
    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的圖像。

  • 5月前
    5 #

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

    但是我建議將pictureBox2和3中的圖片合並為一个圖像,然後再將其顯示在單个pictureBox中。

  • php:警告:mysqli_real_escape_string()恰好需要2个引數,给定1个……我做錯了什麼?
  • java:區域性變數的垃圾迴收