Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I made a script for 1 form with 2 pictureboxes, until here everything is fine.

If you execute the code below, than you can see you can move the picturebox1 and also drop it inside picturebox2. Now i would like that the dropped picturebox1 can be resized, rotated and moved around inside picturebox2 (once executed by client). I have looked around but can not find the answers to this problem. Any help i would appreciate, Thank you

Ps; I have to tell also that the picturebox2 has a image to it has a size much bigger (892; 502) Then picturebox1 (you will see that wen dragging picturebox1 it overlaps the picturebox2 completely And that is just what i don´t want!!, i would like to conserve the picturebox1 size (163; 195))

So my Goal (if possible) conserve the same size (picturebox1), resize it once dragged inside picturebox2 , Move it around on picturebox2, change the mouseclic (wen click on picturebox1 it decreases the image)

Here is the code:
C#
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int x = 0;
        int y = 0;
        bool drag = false;

        private void IncreaseSize(PictureBox p, int dt)
        {
            Size size = p.Size;
            size.Height = size.Height + dt;
            size.Width = size.Width + dt;
            p.Size = size;

        }

        private void DecreaseSize(PictureBox p, int dt)
        {
            Size size = p.Size;
            size.Height = size.Height - dt;
            size.Width = size.Width - dt;
            p.Size = size;
        }

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

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.Copy);
            x = e.X;
            y = e.Y;
            drag = true;
            DecreaseSize(pictureBox1, 5);
           
        
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (drag)
            {
                //position new get
                pictureBox1.Top += e.Y - y;
                pictureBox1.Left += e.X - x;

            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            drag = false;
        }

        private void pictureBox2_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void pictureBox2_DragDrop(object sender, DragEventArgs e)
        {
            pictureBox2.Image = (Image)e.Data.GetData(DataFormats.Bitmap);
            
        }
    }
}
Posted
Comments
[no name] 12-Jun-14 13:29pm    
PictureBoxes are not containers for other controls so when picturebox1 overlaps picturebox2, that is exactly what is supposed to happen.
John Nederveen 12-Jun-14 13:41pm    
thank you Wes, but are there any suggestions? to make it work?. supposed i can not work with a second picturebox. thank you (as see i am a newbie)
Sergey Alexandrovich Kryukov 12-Jun-14 16:02pm    
Exactly. The whole idea of using PictureBox is wrong (why so many beginners falls into this?)
Please see my detailed answer.
—SA

1 solution

Don't do any complex manipulations with PictureBox, even if you can succeed. This control is absolutely useless for anything except showing some static picture. Doing anything else is possible, but the control won't be helping, will only waste your development time and eat up some extra resources. See also the comment to the question by Wes Aday.

What to do instead? I'll tell you. Please see my past answers:
Append a picture within picturebox[^],
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^].

And for detail of image rendering, please see these answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^],
How to speed up my vb.net application?[^],
Zoom image in C# .net mouse wheel[^] (specifically on zoom).

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900