How can i drag out of the pictureBox1 bounds part of an image ? And move it around the form area unt

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
In this code i can draw on pictureBox1 a Rectangle and when i leave the left button of the mouse its saving the rectangle in a file so i can see the section i drawed the rectangle.
Now i want that if the user drawing and while pressing the left mouse button and dragging the rectangle or maybe dragging will be after leaving the left click mouse button and the rectangle is show up so pressing without leaving the right mouse button will
allow the user to drag the rectangle with the part of the image inside around the form.

Here is the code:


<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.ComponentModel;
<span style="color:Blue; using System.Data;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Windows.Forms;
<span style="color:Blue; using System.Drawing.Drawing2D;
<span style="color:Blue; using System.Drawing.Imaging;
<span style="color:Blue; using System.IO;

<span style="color:Blue; namespace WindowsFormsApplication1
{
<span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class ImagesPixelsColorsComparison : Form
{
<span style="color:Blue; static Label label_section;
<span style="color:Blue; static <span style="color:Blue; string selected_section;
<span style="color:Blue; static FolderBrowserDialog section_dialog;
Bitmap newImage;
ToolTip mytip1;
FileInfo fi;
<span style="color:Blue; bool button_switch;
<span style="color:Blue; bool copy_mode;
Bitmap f1;
Bitmap f2;
Bitmap tempBmp;
ImagesComparison ImagesComparion1;
Rectangle Rect;
Rectangle RectImage;
Bitmap FirstImage = <span style="color:Blue; null;
Bitmap SecondImage = <span style="color:Blue; null;
Bitmap ThirdImage = <span style="color:Blue; null;
Bitmap FourthImage = <span style="color:Blue; null;
<span style="color:Blue; bool StopPaint;
<span style="color:Blue; bool StartPaint;
<span style="color:Blue; bool _dontDrawRect = <span style="color:Blue; false;
<span style="color:Blue; public ImagesPixelsColorsComparison()
{
InitializeComponent();
<span style="color:Blue; this.AllowDrop = <span style="color:Blue; true;
label_section = <span style="color:Blue; new Label();
<span style="color:Blue; this.Controls.Add(label_section);
selected_section = Options_DB.get_selected_section();
section_dialog = <span style="color:Blue; new FolderBrowserDialog();
<span style="color:Blue; if (!Directory.Exists(selected_section))
{
textBox9.Text = <span style="color:#A31515; "";
}
<span style="color:Blue; else
{
textBox9.Text = selected_section;
}
copy_mode = <span style="color:Blue; false;
button_switch = <span style="color:Blue; true;
ImagesComparion1 = <span style="color:Blue; new ImagesComparison();
textBox6.Text = <span style="color:Blue; this.Size.ToString();
StartPaint = <span style="color:Blue; false;
StopPaint = <span style="color:Blue; true;

<span style="color:Green; /* 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 = <span style="color:Blue; new Point(pictureBox1.Location.X + pictureBox1.Width / 2 - button1.Width / 2 , pictureBox1.Bounds.Location.Y - 28); <span style="color:Green; // חישוב מדוייק של האמצע של הקונטרול והמיקום לש הכפתור בדיוק באמצע.
button2.Location = <span style="color:Blue; new Point(pictureBox2.Location.X + pictureBox2.Width / 2 - button2.Width / 2, pictureBox2.Bounds.Location.Y - 28);
}

<span style="color:Blue; private <span style="color:Blue; void pictureBox1_MouseDown(<span style="color:Blue; object sender, MouseEventArgs e)
{
<span style="color:Blue; if (StopPaint == <span style="color:Blue; false)
{
<span style="color:Blue; return;
}
<span style="color:Blue; else
{
<span style="color:Blue; if (e.Button == MouseButtons.Left && <span style="color:Blue; this._dontDrawRect == <span style="color:Blue; false)
{
textBox1.Text = <span style="color:#A31515; "";
textBox2.Text = <span style="color:#A31515; "";
textBox3.Text = <span style="color:#A31515; "";
StartPaint = <span style="color:Blue; true;
Rect = <span style="color:Blue; new Rectangle(e.X, e.Y, 0, 0);
textBox4.Text = <span style="color:#A31515; "Top " + Rect.Top + <span style="color:#A31515; " Left " + Rect.Left;
pictureBox1.Invalidate();
}
}
}

<span style="color:Blue; private <span style="color:Blue; void pictureBox1_MouseMove(<span style="color:Blue; object sender, MouseEventArgs e)
{
textBox7.Text = <span style="color:#A31515; "Position on X: " + e.X + <span style="color:#A31515; "Position on Y: " + e.Y;
<span style="color:Blue; if (StopPaint == <span style="color:Blue; false)
{
<span style="color:Blue; return;
}
<span style="color:Blue; else
{
<span style="color:Blue; if (e.Button == MouseButtons.Left && <span style="color:Blue; this._dontDrawRect == <span style="color:Blue; false)
{
Rect = <span style="color:Blue; new Rectangle(Rect.X, Rect.Y, e.X - Rect.X, e.Y - Rect.Y);
pictureBox1.Invalidate();
}
}
}

<span style="color:Blue; private <span style="color:Blue; void pictureBox1_MouseUp(<span style="color:Blue; object sender, MouseEventArgs e)
{
<span style="color:Blue; if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
<span style="color:Blue; if (pictureBox1.Image == <span style="color:Blue; null)
{
<span style="color:Blue; return;
}
tempBmp = <span style="color:Blue; new Bitmap(FirstImage);
<span style="color:Blue; float newLeft = 0;
newLeft = ((<span style="color:Blue; float)Rect.Left / (<span style="color:Blue; float)pictureBox1.Width) * ((<span style="color:Blue; float)FirstImage.Width);
<span style="color:Blue; float newRight = ((<span style="color:Blue; float)Rect.Right/(<span style="color:Blue; float)pictureBox1.Width)*((<span style="color:Blue; float)FirstImage.Width);
<span style="color:Blue; float newTop = 0;
newTop = ((<span style="color:Blue; float)Rect.Top / (<span style="color:Blue; float)pictureBox1.Height) * ((<span style="color:Blue; float)FirstImage.Height);
<span style="color:Blue; float newBottom = ((<span style="color:Blue; float)Rect.Bottom/(<span style="color:Blue; float)pictureBox1.Height)*((<span style="color:Blue; float)FirstImage.Height);
RectImage = <span style="color:Blue; new Rectangle((<span style="color:Blue; int)newLeft, (<span style="color:Blue; int)newTop, (<span style="color:Blue; int)newRight - (<span style="color:Blue; int)newLeft, (<span style="color:Blue; int)newBottom - (<span style="color:Blue; int)newTop);
<span style="color:Green; // you saw that the Rect region is outside of the picture box..
<span style="color:Green; // and you dont check the 4 points you get from the mouse..
<span style="color:Green; // you must as always .. make an input check on parameters..
<span style="color:Green; // in this case it means that you must check left right top bottom..
<span style="color:Green; // or the Rect you got.. to see that it is not too big.. or too fat or too small or too anything..
<span style="color:Green; // that might be not ok to continue with this parameters..
<span style="color:Green; // in the for the i went from left to right.. and since right was calaculated
<span style="color:Green; // to be more than 512 (its 614 i think when i did it ) .. than of course
<span style="color:Green; // the for will fail when the GetColor try to get a pixel from 512,y..
<span style="color:Green; // so in the case the RectImage bounds are not "good" put a message to the user
<span style="color:Green; // and dont continue to doing the rest..




<span style="color:Blue; if (e.X > pictureBox1.Size.Width || e.Y > pictureBox1.Size.Height)

<span style="color:Green; // || RectImage.Bottom > pictureBox1.Bottom) //|| RectImage.Bottom > pictureBox1.Bottom) //||
<span style="color:Green; // RectImage.Size.Width > pictureBox1.Bounds.Left || RectImage.Width > pictureBox1.Bounds.Right) //bounds are not ok )
{
MessageBox.Show(<span style="color:#A31515; "You are out of region!");

<span style="color:Blue; return; <span style="color:Green; // not doing the rest cause the "input parameters"(the mouse coordinates) are not legal to continue
}


<span style="color:Green; // if cound are ok.. the program will continue here... and it will work fine..

<span style="color:Blue; int i, j;
<span style="color:Blue; for (i=RectImage.Left;i<RectImage.Right;i++)
<span style="color:Blue; 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 = <span style="color:#A31515; "Right " + RectImage.Right + <span style="color:#A31515; " Bottom " + RectImage.Bottom;
textBox4.Text = <span style="color:#A31515; "Left " + RectImage.Left + <span style="color:#A31515; " Top " + RectImage.Top;
<span style="color:Green; //check, if picture-variables are set
<span style="color:Blue; if (FirstImage != <span style="color:Blue; null && SecondImage != <span style="color:Blue; null)
{
<span style="color:Green; //check, if selected the correct rectangle for comparing,
<span style="color:Green; //if so, set further rectangle-selecting to false
<span style="color:Blue; if (<span style="color:Blue; this._dontDrawRect == <span style="color:Blue; false)
{
<span style="color:Blue; if (MessageBox.Show(<span style="color:#A31515; "Use this Rectangle for comparing?", <span style="color:#A31515; "Question",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
System.Windows.Forms.DialogResult.Yes)
{
<span style="color:Blue; this._dontDrawRect = <span style="color:Blue; true;
}
}

<span style="color:Green; //if correct rectangle selected, do the job
<span style="color:Blue; if (_dontDrawRect)
{
<span style="color:Blue; if (copy_mode == <span style="color:Blue; true)
{
MessageBox.Show(<span style="color:#A31515; "hi");
}
<span style="color:Blue; else
{
<span style="color:Blue; if (ImagesComparion1.ImageComparison(FirstImage, SecondImage, Rect)) <span style="color:Green; // לבדוק איך להעביר נתונים מהצד השני לכאן לטקסט בוקסים.
{
textBox1.Text = ImagesComparion1.textbox1;
textBox2.Text = ImagesComparion1.textbox2;
textBox3.Text = ImagesComparion1.textbox3;
MessageBox.Show(<span style="color:#A31515; "identical");
}
<span style="color:Blue; else
{
textBox1.Text = ImagesComparion1.textbox1;
textBox2.Text = ImagesComparion1.textbox2;
textBox3.Text = ImagesComparion1.textbox3;
MessageBox.Show(<span style="color:#A31515; "different");
}
}
}
}
}
<span style="color:Blue; else <span style="color:Blue; if (e.Button == System.Windows.Forms.MouseButtons.Right)
<span style="color:Blue; this._dontDrawRect = <span style="color:Blue; false;

}

<span style="color:Blue; private <span style="color:Blue; void pictureBox1_Paint(<span style="color:Blue; object sender, PaintEventArgs e)
{
<span style="color:Blue; using (Pen pen = <span style="color:Blue; new Pen(Color.Red, 2))
{
<span style="color:Blue; if (StartPaint == <span style="color:Blue; true)
{
e.Graphics.DrawRectangle(pen, Rect);
pictureBox2.Invalidate();
pictureBox2.Paint += <span style="color:Blue; new PaintEventHandler(pictureBox2_Paint);
}
}
}

<span style="color:Blue; private <span style="color:Blue; void ImagesPixelsColorsComparison_FormClosing(<span style="color:Blue; object sender, FormClosingEventArgs e)
{
<span style="color:Green; //dispose
<span style="color:Blue; if (FirstImage != <span style="color:Blue; null)
FirstImage.Dispose();
<span style="color:Blue; if (SecondImage != <span style="color:Blue; null)

SecondImage.Dispose();
<span style="color:Blue; if (ThirdImage != <span style="color:Blue; null)
ThirdImage.Dispose();
<span style="color:Blue; if (FourthImage != <span style="color:Blue; null)
FourthImage.Dispose();
}

<span style="color:Blue; private <span style="color:Blue; void ImagesPixelsColorsComparison_Resize(<span style="color:Blue; object sender, EventArgs e)
{
textBox6.Text = <span style="color:Blue; this.Size.ToString();
}

<span style="color:Blue; private <span style="color:Blue; void pictureBox2_Paint(<span style="color:Blue; object sender, PaintEventArgs e)
{
<span style="color:Blue; using (Pen pen = <span style="color:Blue; new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, Rect);
}
}

<span style="color:Blue; private <span style="color:Blue; void button1_Click(<span style="color:Blue; object sender, EventArgs e)
{
openFileDialog1.Title = <span style="color:#A31515; "Select Bitmap file or Jpg file to load into the pictureBox";
openFileDialog1.InitialDirectory = "c:\";
openFileDialog1.FileName = <span style="color:Blue; null;
openFileDialog1.Filter = <span style="color:#A31515; "First Bitmap Or Jpg File|*.bmp;*.jpg|Bitmap Or Jpg File|*.bmp;*.jpg";
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = <span style="color:Blue; true;
DialogResult result1 = openFileDialog1.ShowDialog();
<span style="color:Blue; string file1 = openFileDialog1.FileName;
<span style="color:Blue; if (result1 == DialogResult.OK)
{
f1 = <span style="color:Blue; new Bitmap(file1);
FirstImage = f1;
pictureBox1.Image = f1; <span style="color:Green; // ScalImage(f1, pictureBox1.Size);
<span style="color:Green; // pictureBox1.Size = f1.Size;
fi = <span style="color:Blue; new FileInfo(file1);
<span style="color:Green; // textBox1.Text = fi.DirectoryName;
<span style="color:Green; // textBox2.Text = fi.Extension;
}
}

<span style="color:Blue; private <span style="color:Blue; void button2_Click(<span style="color:Blue; object sender, EventArgs e)
{
openFileDialog2.Title = <span style="color:#A31515; "Select Bitmap file or Jpg to load into the pictureBox";
openFileDialog2.InitialDirectory = "c:\";
openFileDialog2.FileName = <span style="color:Blue; null;
openFileDialog2.Filter = <span style="color:#A31515; "Second Bitmap Or Jpg File|*.bmp;*.jpg|Bitmap Or Jpg File|*.bmp;*.jpg";
openFileDialog2.FilterIndex = 1;
openFileDialog2.RestoreDirectory = <span style="color:Blue; true;
DialogResult result2 = openFileDialog2.ShowDialog();
<span style="color:Blue; string file2 = openFileDialog2.FileName;
<span style="color:Blue; if (result2 == DialogResult.OK)
{
f2 = <span style="color:Blue; new Bitmap(file2);
SecondImage = f2;
pictureBox2.Image = f2; <span style="color:Green; // ScalImage(f2, pictureBox2.Size);
<span style="color:Green; // pictureBox2.Size = f2.Size;
}
}

<span style="color:Blue; private <span style="color:Blue; void ImagesPixelsColorsComparison_MouseMove(<span style="color:Blue; object sender, MouseEventArgs e)
{
textBox8.Text = <span style="color:#A31515; "Position on X: " + e.X + <span style="color:#A31515; " Position on Y: " + e.Y;
}

<span style="color:Blue; private <span style="color:Blue; void button3_Click(<span style="color:Blue; object sender, EventArgs e)
{
<span style="color:Blue; if (StopPaint)
{
StartPaint = <span style="color:Blue; true;
StopPaint = <span style="color:Blue; false;
label10.Text = <span style="color:#A31515; "Currently on automatic paint mode";
button3.Text = <span style="color:#A31515; "Manual paint mode";
Rect = <span style="color:Blue; new Rectangle(25, 240, 366, 279);
}
<span style="color:Blue; else
{
StopPaint = <span style="color:Blue; true;
label10.Text = <span style="color:#A31515; "Currently on manual paint mode";
button3.Text = <span style="color:#A31515; "Automatic paint mode";
Rect = <span style="color:Blue; new Rectangle(-1, -1, 0, 0);
}
<span style="color:Blue; this.pictureBox1.Invalidate();
<span style="color:Blue; this.pictureBox2.Invalidate();
}


<span style="color:Green; //*** This function put image on image and create a black and white image from two colors or black and white images ***\
<span style="color:Blue; public Bitmap black_and_white(Bitmap image1, Bitmap image2)
{
<span style="color:Blue; if (pictureBox1.Image == <span style="color:Blue; null || pictureBox2.Image == <span style="color:Blue; null)
{
<span style="color:Blue; return <span style="color:Blue; null;
}
Color newColor;
Bitmap image_scan;
image_scan = <span style="color:Blue; new Bitmap(512, 512);
newImage = <span style="color:Blue; new Bitmap(image1.Width, image1.Height);
<span style="color:Blue; int x, y;

<span style="color:Blue; if (image1.Width != image2.Width || image1.Height != image2.Height)
{
MessageBox.Show(<span style="color:#A31515; "Images are different Size");
<span style="color:Blue; return <span style="color:Blue; null;
}

<span style="color:Blue; for (x = 0; x < newImage.Width; x++)
{
<span style="color:Blue; for (y = 0; y < newImage.Height; y++)
{
Color originalColor = image1.GetPixel(x, y);
Color originalColor1 = image2.GetPixel(x, y);
<span style="color:Blue; int WhiteBlack = (<span style="color:Blue; int)((originalColor.R) + (originalColor.G)
+ (originalColor.B));
<span style="color:Blue; int WhiteBlack1 = (<span style="color:Blue; int)((originalColor1.R) + (originalColor1.G)
+ (originalColor1.B));
<span style="color:Blue; if (WhiteBlack == WhiteBlack1)
newColor = Color.FromArgb(0,0,0);
<span style="color:Blue; else
newColor = Color.FromArgb(255, 255, 255);
newImage.SetPixel(x, y, newColor);
}
}
newImage.Save(<span style="color:#A31515; @"d:newImage.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
pictureBox3.Image = newImage;
<span style="color:Blue; return newImage;
}

<span style="color:Blue; private <span style="color:Blue; void button4_Click(<span style="color:Blue; object sender, EventArgs e)
{
black_and_white (f1,f2);
}

<span style="color:Blue; private <span style="color:Blue; void pictureBox3_Click(<span style="color:Blue; object sender, EventArgs e)
{
<span style="color:Blue; if (button_switch == <span style="color:Blue; true)
{
button_switch = <span style="color:Blue; false;
pictureBox3.Dock = DockStyle.Fill;
pictureBox3.BringToFront();
}
<span style="color:Blue; else
{
button_switch = <span style="color:Blue; true;
pictureBox3.Dock = DockStyle.None;
}
}

<span style="color:Blue; private <span style="color:Blue; void pictureBox1_MouseEnter(<span style="color:Blue; object sender, EventArgs e)
{
<span style="color:Blue; if (pictureBox1.Image == <span style="color:Blue; null)
{
}
<span style="color:Blue; else
{
mytip1 = <span style="color:Blue; new ToolTip();
mytip1.UseFading = <span style="color:Blue; true;
mytip1.Show(fi.Extension, label4, 5000);
}
}

<span style="color:Blue; private <span style="color:Blue; void pictureBox1_MouseLeave(<span style="color:Blue; object sender, EventArgs e)
{
<span style="color:Blue; if (pictureBox1.Image == <span style="color:Blue; null)
{
}
<span style="color:Blue; else
{
mytip1.Dispose();
}
}

<span style="color:Blue; private <span style="color:Blue; void button5_Click(<span style="color:Blue; object sender, EventArgs e)
{
copy_mode = <span style="color:Blue; true;
}

<span style="color:Blue; static <span style="color:Blue; public Bitmap Copy(Bitmap srcBitmap, Rectangle section)
{
Bitmap bmp;
bmp = <span style="color:Blue; new Bitmap(section.Width, section.Height);


Graphics g = Graphics.FromImage(bmp);

<span style="color:Green; // Draw the specified section of the source bitmap to the new one
g.DrawImage(srcBitmap, 0, 0, section, GraphicsUnit.Pixel);

<span style="color:Green; // Clean up
g.Dispose();

<span style="color:Green; // Return the bitmap
<span style="color:Blue; if (!Directory.Exists(selected_section))
{

}
<span style="color:Blue; else
{
bmp.Save(selected_section + <span style="color:#A31515; @"section.bmp", ImageFormat.Bmp);
label_section.Enabled = <span style="color:Blue; true;
label_section.Visible = <span style="color:Blue; true;
label_section.Location = <span style="color:Blue; new Point(7, 530);
label_section.Width = 300;
label_section.Text = <span style="color:#A31515; "Image selected area saved";

}

<span style="color:Green; // Create the new bitmap and associated graphics object
<span style="color:Blue; return bmp;

}

<span style="color:Blue; private <span style="color:Blue; void button5_Click_1(<span style="color:Blue; object sender, EventArgs e)
{
section_dialog.Description = <span style="color:#A31515; "Select/Change the default directory for saving specific selection of image";
<span style="color:Blue; if (section_dialog.ShowDialog() == DialogResult.OK)
{
<span style="color:Blue; string message = <span style="color:#A31515; "Are you sure you want to save the image to this location ?";
<span style="color:Blue; string caption = <span style="color:#A31515; "Operation cancelled";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;
result = MessageBox.Show(message, caption, buttons);

<span style="color:Blue; if (result == System.Windows.Forms.DialogResult.Yes)
{
selected_section = section_dialog.SelectedPath;
textBox9.Text = selected_section;
Options_DB.set_selected_section(selected_section);
}
}
}







<span style="color:Gray; ///<span style="color:Green; ///////////////////// to check why he keep showing saving directory even if i deleted the directory manualy! It suppose not to show anything in textBox9 and tell the user to select first a directory.
<span style="color:Gray; ///<span style="color:Green; ////////////// maybe to make a button switch for selection are saving mode or something that will tell the user he can save or each time he select a section its saving it.
<span style="color:Gray; ///<span style="color:Green; timer or using something to delete/remove the label_section.Text = "Image selected area saved"; message//// and more things to fix/repair/check /////////////////////
}
}

[/code]
<div style="color:Black;background-color:White; <pre>
Thanks.
[/code]
<hr class="sig danieli

View the full article
 
Back
Top