图片框问题

发布于 2024-10-01 04:36:32 字数 289 浏览 1 评论 0原文

我有一个问题:

我有 3 个图片框,其中有 3 个不同的图像如图像

我可以将什么设置为pictureBox3,以便两个图像看起来相同......

alt text

编辑: 我想将 pictureBox3 移动到 pictureBox2 上,

因此没有选项将它们合并为单个图像

I have a problem:

I have 3 picture boxes with 3 different images as in Image

what can i set to pictureBox3 so both images look same.....

alt text

EDITED:
I want to move pictureBox3 on pictureBox2,

So there is no Option to merge them to single image

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

很快妥协 2024-10-08 04:36:32

确保pictureBox3中的图像是透明的。将BackColor 设置为透明。在代码中,将pictureBox3Parent 属性设置为pictureBox2。调整 pictureBox3Location 坐标,因为更改 Parent 后它们将相对于 pictureBox2 的坐标>。

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

    }

在设计器中你不会看到透明度,但在运行时你会看到。

更新

图中,左侧显示设计器视图,右侧是运行时版本。
左:设计器视图,右:运行时的样子

另一个更新

我真的不知道了解这怎么可能不适合你。我想我们一定在做一些不同的事情。我将描述创建工作示例所需的确切步骤。如果您遵循完全相同的步骤,我想知道我们是否会得到相同的结果。接下来的步骤描述了要做什么并使用我在网上找到的两张图像。

  • 使用 Visual Studio 2008,使用模板 Windows 窗体应用程序创建一个新项目。确保该项目面向 .NET Framework 3.5。
  • 将窗体大小设置为 457;483。
  • 将 PictureBox 控件拖到窗体上。将其位置设置为 0;0,将大小设置为 449;449。
  • 单击图像属性旁边的省略号,单击导入...按钮并导入 http 处的图像://a.dryicons.com/files/graphics_previews/retro_blue_background.jpg(只需在文件名文本框中输入 URL,然后单击打开)。然后单击“确定”以使用该图像。
  • 将另一个 PictureBox 拖到窗体上,将其位置设置为 0;0,将其大小设置为 256;256。还将其 BackColor 属性设置为透明。
  • 使用与上述相同的方法,导入图像 http://www.axdn.com/redist/axiw_i .png 这是一个透明图像。
  • 现在将以下代码放入表单的 OnLoad 事件处理程序中:

    private void Form1_Load(对象发送者,EventArgs e)
    {
        图片Box2.Parent = 图片Box1;
    }
    

就是这样!如果我运行这个程序,我会在另一个图像之上得到一个透明图像。

Make sure the image in pictureBox3 is transparent. Set the BackColor to transparent. In code, set the Parent property of the pictureBox3 to be pictureBox2. Adjust the Location coordinates of pictureBox3 since they will be relative to the coordinates of pictureBox2 once you've changed the 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);

    }

In designer you will not see the transparency, but at runtime you will.

Update

In the image, the left side shows the designer view, the right side is the runtime version.
Left: Designer view, Right: How it looks at runtime

Another update

I really don't understand how it would be possible that this doesn't work for you. I suppose there must be something we are doing different. I'll describe the exact steps to take to create a working sample. If you follow the exact same steps, I wonder if we'll get the same results or not. Next steps describe what to do and use two images I found on the net.

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

That's it! If I run this program I get a transparent image on top of another image.

时光瘦了 2024-10-08 04:36:32

我将添加另一个示例,根据更新的要求允许移动 image3。
要使其正常工作,请将透明图像放入 Resources\transp.png
这对所有三个图像使用相同的图像,但您可以简单地将 image1 和 image2 的透明Img 替换为合适的图像。

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

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

I'll add another example that according to the updated requirement allows for moving image3.
To get it working, put an image with transparency in Resources\transp.png
This uses the same image for all three images, but you can simply replace transparentImg for image1 and image2 to suitable images.

Once the demo is started the middle image can be dragged-dropped around the form.

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;
    }
}
戴着白色围巾的女孩 2024-10-08 04:36:32

这段代码可以解决这个问题:

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 中的图像。

This code will do the trick:

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

It will draw the image from pictureBox2 on the existing image of pictureBox1.

∞梦里开花 2024-10-08 04:36:32

首先,将 PictureBox3 的 BackColor 属性设置为透明。这应该适用于几乎所有情况。

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


更新
根据我收到的回复,将 BackColor 设置为透明似乎不起作用。在这种情况下,最好处理 PictureBox 的 Paint 事件,并自己绘制新图像,如 阿尔宾建议

For starters, set the BackColor property of PictureBox3 to Transparent. This should work in almost all cases.

You should also use an image with a transparent background instead of white so you do not have the white borders around your purple circle. (Recommended image format: PNG)


Update
Following the replies I got, it appears setting the BackColor to Transparent doesn't work. In that case, it's best you handle the Paint event of the PictureBox and do the painting of the new image yourself as Albin suggested.

冷月断魂刀 2024-10-08 04:36:32

您可以通过覆盖 OnPaint 等进行一些黑客攻击,例如 这里

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

You might do some hack by overriding OnPaint and stuff, example here.

But I'd recommend to merge the pictures in pictureBox2 and 3 into a single image before displaying them in a single pictureBox.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文