How to drag part of image from pictureBox1 to pictureBox2 AllowDrop not exist:

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Chocolade
    New Member
    • Aug 2010
    • 69

    How to drag part of image from pictureBox1 to pictureBox2 AllowDrop not exist:

    I have this code wich let me the option to draw a Rectangle on pictureBox1. Now i did in the end a function that save to hard disk the Rectangle area from the image in the picturebox1 if i mark some area in the image in the pictureBox1 it will save only the area inside the red Rectangle.

    Now i want to do that the user will be able to drag with the mouse the Rectangle with the image part inside to pictureBox2.

    First problem is i dont know how to do it. Second thing is every example i looked in the internet is using pictureBox2.All owDrop but i dont have this property.
    In my new form in the constructor im doing pictureBox2.
    But AllowDrop dosent exist.


    Here is the complete code with the rectangle save and all the things.

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    
    namespace WindowsFormsApplication1
    {
         public partial class ImagesPixelsColorsComparison : Form
        {
             static Label label_section;
            static string selected_section;
            static FolderBrowserDialog section_dialog;
            Bitmap newImage;
            ToolTip mytip1;
            FileInfo fi;
            bool button_switch;
            bool copy_mode;
            Bitmap f1;
            Bitmap f2;
            Bitmap tempBmp;
            ImagesComparison ImagesComparion1;
            Rectangle Rect;
            Rectangle RectImage;
            Bitmap FirstImage = null;
            Bitmap SecondImage = null;
            Bitmap ThirdImage = null;
            Bitmap FourthImage = null;
            bool StopPaint;
            bool StartPaint;
            bool _dontDrawRect = false;
            public ImagesPixelsColorsComparison()
            {
                InitializeComponent();
                this.AllowDrop = true;
                label_section = new Label();
                this.Controls.Add(label_section);
                selected_section = Options_DB.get_selected_section();
                section_dialog = new FolderBrowserDialog();
                if (!Directory.Exists(selected_section))
                {
                    textBox9.Text = "";
                }
                else
                {
                    textBox9.Text = selected_section;
                }
                copy_mode = false;
                button_switch = true;
                ImagesComparion1 = new ImagesComparison();
                textBox6.Text = this.Size.ToString();
                StartPaint = false;
                StopPaint = true;
    
              /*  ThirdImage = Properties.Resources.RadarImageActive;
                FourthImage = Properties.Resources.RadarImageCloseDemo;
                pictureBox1.Size = ThirdImage.Size;
                pictureBox2.Size = FourthImage.Size;
                this.pictureBox1.Image = this.ThirdImage;
                this.pictureBox2.Image = this.FourthImage;*/
                button1.Location = new Point(pictureBox1.Location.X + pictureBox1.Width / 2 - button1.Width / 2 , pictureBox1.Bounds.Location.Y - 28); // חישוב מדוייק של האמצע של הקונטרול והמיקום לש הכפתור בדיוק באמצע. 
                button2.Location = new Point(pictureBox2.Location.X + pictureBox2.Width / 2 - button2.Width / 2, pictureBox2.Bounds.Location.Y - 28);
            }
    
            private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
            {
                if (StopPaint == false)
                {
                    return;
                }
                else
                {
                    if (e.Button == MouseButtons.Left && this._dontDrawRect == false)
                    {
                        textBox1.Text = "";
                        textBox2.Text = "";
                        textBox3.Text = "";
                        StartPaint = true;
                        Rect = new Rectangle(e.X, e.Y, 0, 0);
                        textBox4.Text = "Top " + Rect.Top + " Left " + Rect.Left;
                        pictureBox1.Invalidate();
                    }
                }
            }
    
            private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
            {
                textBox7.Text = "Position on X: " + e.X + "Position on Y: " + e.Y;
                if (StopPaint == false)
                {
                    return;
                }
                else
                {
                    if (e.Button == MouseButtons.Left && this._dontDrawRect == false)
                    {
                        Rect = new Rectangle(Rect.X, Rect.Y, e.X - Rect.X, e.Y - Rect.Y);
                        pictureBox1.Invalidate();
                    }
                }
            }
    
            private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Left)
                {
                    if (pictureBox1.Image == null)
                    {
                        return;
                    }
                    tempBmp = new Bitmap(FirstImage);
                    float newLeft = 0;
                    newLeft =  ((float)Rect.Left / (float)pictureBox1.Width) * ((float)FirstImage.Width);
                    float newRight = ((float)Rect.Right/(float)pictureBox1.Width)*((float)FirstImage.Width);
                    float newTop = 0;
                    newTop = ((float)Rect.Top / (float)pictureBox1.Height) * ((float)FirstImage.Height);
                    float newBottom = ((float)Rect.Bottom/(float)pictureBox1.Height)*((float)FirstImage.Height);
                    RectImage = new Rectangle((int)newLeft, (int)newTop, (int)newRight - (int)newLeft, (int)newBottom - (int)newTop);
                    // you saw that the Rect region is outside of the picture box..
                    // and you don't check the 4 points you get from the mouse..
                    // you must  as always  .. make an input check on parameters..
                    // in this case it means that you must check left right top bottom..
                    // or the Rect you got.. to see that it is not too big.. or too fat or too small or too anything..
                    // that might be not ok to continue with this parameters..
                    // in the for the i went from left to right.. and since right was calaculated
                    // to be more than 512 (its 614 i think when i did it ) .. than of course
                    // the for will fail when the GetColor try to get a pixel from 512,y..
                    // so in the case the RectImage bounds are not "good" put a message to the user
                    // and don't continue to doing the rest..
    
    
    
                    
                    if (e.X > pictureBox1.Size.Width || e.Y > pictureBox1.Size.Height)
                        
                    // || RectImage.Bottom > pictureBox1.Bottom) //|| RectImage.Bottom > pictureBox1.Bottom) //||
                       // RectImage.Size.Width > pictureBox1.Bounds.Left || RectImage.Width > pictureBox1.Bounds.Right)    //bounds are not ok )
                    {
                        MessageBox.Show("You are out of region!");
    
                        return; // not doing the rest cause the "input parameters"(the mouse coordinates) are not legal to continue
                    }
    
    
                    // if cound are ok.. the program will continue here... and it will work fine..
                    
                    int i, j;
                    for (i=RectImage.Left;i<RectImage.Right;i++)
                        for (j = RectImage.Top; j < RectImage.Bottom; j++)
                        {
                            Color c = FirstImage.GetPixel(i, j);
                            tempBmp.SetPixel(i, j, c);
                        }
                    pictureBox1.Image = tempBmp;
                    Copy(FirstImage, RectImage);
                    textBox5.Text = "Right " + RectImage.Right + " Bottom " + RectImage.Bottom;
                    textBox4.Text = "Left " + RectImage.Left + " Top " + RectImage.Top;
                    //check, if picture-variables are set
                    if (FirstImage != null && SecondImage != null)
                    {
                        //check, if selected the correct rectangle for comparing,
                        //if so, set further rectangle-selecting to false
                        if (this._dontDrawRect == false)
                        {
                            if (MessageBox.Show("Use this Rectangle for comparing?", "Question",
                             MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
                             System.Windows.Forms.DialogResult.Yes)
                            {
                                this._dontDrawRect = true;
                            }
                        }
    
                        //if correct rectangle selected, do the job
                        if (_dontDrawRect)
                        {
                            if (copy_mode == true)
                            {
                                MessageBox.Show("hi");
                            }
                            else
                            {
                                if (ImagesComparion1.ImageComparison(FirstImage, SecondImage, Rect)) // לבדוק איך להעביר נתונים מהצד השני לכאן לטקסט בוקסים.
                                {
                                    textBox1.Text = ImagesComparion1.textbox1;
                                    textBox2.Text = ImagesComparion1.textbox2;
                                    textBox3.Text = ImagesComparion1.textbox3;
                                    MessageBox.Show("identical");
                                }
                                else
                                {
                                    textBox1.Text = ImagesComparion1.textbox1;
                                    textBox2.Text = ImagesComparion1.textbox2;
                                    textBox3.Text = ImagesComparion1.textbox3;
                                    MessageBox.Show("different");
                                }
                            }
                        }
                    }
                }
                else if (e.Button == System.Windows.Forms.MouseButtons.Right)
                    this._dontDrawRect = false;
    
            }
    
            private void pictureBox1_Paint(object sender, PaintEventArgs e)
            {
                using (Pen pen = new Pen(Color.Red, 2))
                {
                    if (StartPaint == true)
                    {
                        e.Graphics.DrawRectangle(pen, Rect);
                        pictureBox2.Invalidate();
                        pictureBox2.Paint += new PaintEventHandler(pictureBox2_Paint);
                    }
                }
            }
    
            private void ImagesPixelsColorsComparison_FormClosing(object sender, FormClosingEventArgs e)
            {
                //dispose
                if (FirstImage != null)
                    FirstImage.Dispose();
                if (SecondImage != null)
                    SecondImage.Dispose();
                if (ThirdImage != null)
                    ThirdImage.Dispose();
                if (FourthImage != null)
                    FourthImage.Dispose();
            }
    
            private void ImagesPixelsColorsComparison_Resize(object sender, EventArgs e)
            {
                textBox6.Text = this.Size.ToString();
            }
    
            private void pictureBox2_Paint(object sender, PaintEventArgs e)
            {
                using (Pen pen = new Pen(Color.Red, 2))
                {
                        e.Graphics.DrawRectangle(pen, Rect);
                }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                openFileDialog1.Title = "Select Bitmap file or Jpg file to load into the pictureBox";
                openFileDialog1.InitialDirectory = "c:\\";
                openFileDialog1.FileName = null;
                openFileDialog1.Filter = "First Bitmap Or Jpg File|*.bmp;*.jpg|Bitmap Or Jpg File|*.bmp;*.jpg";
                openFileDialog1.FilterIndex = 1;
                openFileDialog1.RestoreDirectory = true;
                DialogResult result1 = openFileDialog1.ShowDialog();
                string file1 = openFileDialog1.FileName;
                if (result1 == DialogResult.OK)
                {
                    f1 = new Bitmap(file1);
                    FirstImage = f1;
                    pictureBox1.Image = f1; // ScalImage(f1, pictureBox1.Size);
                  //  pictureBox1.Size = f1.Size;
                    fi = new FileInfo(file1);
                   // textBox1.Text = fi.DirectoryName;
                   // textBox2.Text = fi.Extension;
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                openFileDialog2.Title = "Select Bitmap file or Jpg to load into the pictureBox";
                openFileDialog2.InitialDirectory = "c:\\";
                openFileDialog2.FileName = null;
                openFileDialog2.Filter = "Second Bitmap Or Jpg File|*.bmp;*.jpg|Bitmap Or Jpg File|*.bmp;*.jpg";
                openFileDialog2.FilterIndex = 1;
                openFileDialog2.RestoreDirectory = true;
                DialogResult result2 = openFileDialog2.ShowDialog();
                string file2 = openFileDialog2.FileName;
                if (result2 == DialogResult.OK)
                {
                    f2 = new Bitmap(file2);
                    SecondImage = f2;
                    pictureBox2.Image = f2; // ScalImage(f2, pictureBox2.Size);
                 //   pictureBox2.Size = f2.Size;
                }
            }
    
            private void ImagesPixelsColorsComparison_MouseMove(object sender, MouseEventArgs e)
            {
                textBox8.Text = "Position on X: " + e.X + " Position on Y: " + e.Y;
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                if (StopPaint)
                {
                    StartPaint = true;
                    StopPaint = false;
                    label10.Text = "Currently on automatic paint mode";
                    button3.Text = "Manual paint mode";
                    Rect = new Rectangle(25, 240, 366, 279);
                }
                else
                {
                    StopPaint = true;
                    label10.Text = "Currently on manual paint mode";
                    button3.Text = "Automatic paint mode";
                    Rect = new Rectangle(-1, -1, 0, 0);
                }
                this.pictureBox1.Invalidate();
                this.pictureBox2.Invalidate();
            }
    
    
             //*** This function put image on image and  create a black and white image from two colors or black and white images ***\\
             public  Bitmap black_and_white(Bitmap image1, Bitmap image2)
             {
                 if (pictureBox1.Image == null || pictureBox2.Image == null)
                 {
                     return null;
                 }
                     Color newColor;
                     Bitmap image_scan;
                     image_scan = new Bitmap(512, 512);
                     newImage = new Bitmap(image1.Width, image1.Height);
                     int x, y;
    
                     if (image1.Width != image2.Width || image1.Height != image2.Height)
                     {
                         MessageBox.Show("Images are different Size");
                         return null;
                     }
    
                     for (x = 0; x < newImage.Width; x++)
                     {
                         for (y = 0; y < newImage.Height; y++)
                         {
                             Color originalColor = image1.GetPixel(x, y);
                             Color originalColor1 = image2.GetPixel(x, y);
                             int WhiteBlack = (int)((originalColor.R) + (originalColor.G)
                       + (originalColor.B));
                             int WhiteBlack1 = (int)((originalColor1.R) + (originalColor1.G)
                      + (originalColor1.B));
                             if (WhiteBlack == WhiteBlack1)
                                 newColor = Color.FromArgb(0,0,0);
                             else
                                 newColor = Color.FromArgb(255, 255, 255);
                                 newImage.SetPixel(x, y, newColor);
                         }
                     }
                     newImage.Save(@"d:\newImage.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                     pictureBox3.Image = newImage;
                     return newImage;
             }
    
             private void button4_Click(object sender, EventArgs e)
             {
                 black_and_white (f1,f2);
             }
    
             private void pictureBox3_Click(object sender, EventArgs e)
             {
                 if (button_switch == true)
                 {
                     button_switch = false;
                     pictureBox3.Dock = DockStyle.Fill;
                     pictureBox3.BringToFront();
                 }
                 else
                 {
                     button_switch = true;
                     pictureBox3.Dock = DockStyle.None;
                 }
             }
    
             private void pictureBox1_MouseEnter(object sender, EventArgs e)
             {
                 if (pictureBox1.Image == null)
                 {
                 }
                 else
                 {
                     mytip1 = new ToolTip();
                     mytip1.UseFading = true;
                     mytip1.Show(fi.Extension, label4, 5000);
                 }
             }
    
             private void pictureBox1_MouseLeave(object sender, EventArgs e)
             {
                 if (pictureBox1.Image == null)
                 {
                 }
                 else
                 {
                     mytip1.Dispose();
                 }
             }
    
             private void button5_Click(object sender, EventArgs e)
             {
                 copy_mode = true;
             }
    
             static public Bitmap Copy(Bitmap srcBitmap, Rectangle section)
             {
                 Bitmap bmp;
                 bmp = new Bitmap(section.Width, section.Height);
                
                        
                         Graphics g = Graphics.FromImage(bmp);
    
                         // Draw the specified section of the source bitmap to the new one
                         g.DrawImage(srcBitmap, 0, 0, section, GraphicsUnit.Pixel);
    
                         // Clean up
                         g.Dispose();
    
                         // Return the bitmap
                         if (!Directory.Exists(selected_section))
                         {
    
                         }
                         else
                         {
                             bmp.Save(selected_section + @"\section.bmp", ImageFormat.Bmp);
                             label_section.Enabled = true;
                             label_section.Visible = true;
                             label_section.Location = new Point(7, 530);
                             label_section.Width = 300;
                             label_section.Text = "Image selected area saved";
                             
                         }
                 
                 // Create the new bitmap and associated graphics object
                 return bmp;
                 
             }
    
             private void button5_Click_1(object sender, EventArgs e)
             {
                  section_dialog.Description = "Select/Change the default directory for saving specific selection of image";
                  if (section_dialog.ShowDialog() == DialogResult.OK)
                  {
                      string message = "Are you sure you want to save the image to this location ?";
                      string caption = "Operation cancelled";
                      MessageBoxButtons buttons = MessageBoxButtons.YesNo;
                      DialogResult result;
                      result = MessageBox.Show(message, caption, buttons);
    
                      if (result == System.Windows.Forms.DialogResult.Yes)
                      {
                          selected_section = section_dialog.SelectedPath;
                          textBox9.Text = selected_section;
                          Options_DB.set_selected_section(selected_section);
                      }
                  }
             }
    
             
    
    
    
    
             
    
    
    
    
           }
    }

    Thanks.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I believe drag/drop on a picture box doesn't work. There was a thread on a similar topic that I was involved in a while back. I'll try to find it but no promises, haha.

    Anyway, the general solution was to just do drag/drop on a panel that contained pictureboxes. You can drag the picturebox, but you can't drop to one. I think it was something like that.

    *Edit: Found the thread... hope it helps!

    Comment

    • Chocolade
      New Member
      • Aug 2010
      • 69

      #3
      Great thanks for the thread.
      Another problem in this new form code near the end in the bottom of this code im using Copy function and a variable called bmp

      If i draw the rectangle to the right or bottom its ok but if i draw try to draw the rectangle in the pictueBox1 to the top up or to the right it dosent draw anything witch is ok but it also throw me exception on the bmp variable in the Copy function say its null.

      On this line: bmp = new Bitmap(section. Width, section.Height) ;
      bmp is null.
      And only when i try to draw to the right or up. I mean when i press the mouse left click button without leaving it and drag it to the left or up it dosent draw a rectangle and thats fine but it also throw exception that bmp is null.

      How can i avoid or check for this null thing ? I tried but without success.

      And this drawing to the left and up dosent got anything with the draging the image from pictureBox to pictureBox.

      I just dont understand why bmp is null and what to do ? I tried to check if bmp is null above the line bmp = new Bitmap(section. Width, section.Height) ; but then it says using unsigned bmp...

      Thanks for helping.

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        I feel like we talked about this in another thread when you were asking about your selection drawing rectangle. I seem to recall saying you would have trouble with this based on the way you were calculating the rectangle. I also seem to think you said you didn't want to deal with it then :P :P (http://bytes.com/topic/c-sharp/answe...tangle-borders)

        Put a breakpoint on that line and see what section.Width and section.Height are. I'm wondering if they are negative and that's why your bitmap is coming out null. If so, I'm pretty sure you just need the absolute value of the dimensions. However, when you copy for your offset you'll need the min of the click points as your start coordinate. Unless you copy backwards... but it's easier to just swap the coordinates :D

        Comment

        Working...