EDN Admin
Well-known member
Hi,
My test program contains 2 buttons and a pictureBox on a form. The picture box has been assigned a image through its properties.
My problem is that button1 will crop the image based on the points that have been set up in the Points array. Button2 will overlay the area that is about the be cropped using the same array points.
When you compare the 2, they are slightly out. Can anyone help or point me in the right direction to solve this. My end aim is to try and come up with a program where the user can add multiple points to the image and move the points to
cut around an object.
Thanks...
Here is my code that you can paste straight into a new project:
<pre class="prettyprint 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;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Point[] points;
public Form1()
{
InitializeComponent();
//Define area that will be cropped (in this case a square - top right of image)
points = new Point[4];
points[0] = new Point(10, 10);
points[1] = new Point(10, 200);
points[2] = new Point(100, 200);
points[3] = new Point(100, 10);
//points[4] = new Point(250, 250);
}
private void button1_Click(object sender, EventArgs e)
{
using (GraphicsPath path = new GraphicsPath())
{
//path.AddCurve.(points);
path.AddLines(points);
using (Region reg = new Region(path))
{
RectangleF rect = reg.GetBounds(Graphics.FromImage(pictureBox1.Image));
using (Bitmap bmp = new Bitmap((int)rect.Width, (int)rect.Height))
{
using (Graphics g = Graphics.FromImage(bmp))
{
reg.Translate(rect.X, rect.Y);
g.Clip = reg;
g.DrawImage(pictureBox1.Image, new Rectangle(Point.Empty, bmp.Size), rect, GraphicsUnit.Pixel);
}
bmp.Save("c:\region.Png", ImageFormat.Png);
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
//Overlay points and lines that define area that will be cropped
Graphics dc = pictureBox1.CreateGraphics();
Pen redPen = new Pen(Color.Red, 1);
Pen bluePen = new Pen(Color.Blue, 1);
dc.DrawEllipse(redPen, points[0].X, points[0].Y, 1, 1);
dc.DrawEllipse(redPen, points[1].X,points[1].Y, 1, 1);
dc.DrawEllipse(redPen, points[2].X,points[2].Y, 1, 1);
dc.DrawEllipse(redPen, points[3].X, points[3].Y, 1, 1);
dc.DrawLine(bluePen, points[0], points[1]);
dc.DrawLine(bluePen, points[0], points[3]);
dc.DrawLine(bluePen, points[3], points[2]);
dc.DrawLine(bluePen, points[1], points[2]);
}
}
}
[/code]
<br/>
View the full article
My test program contains 2 buttons and a pictureBox on a form. The picture box has been assigned a image through its properties.
My problem is that button1 will crop the image based on the points that have been set up in the Points array. Button2 will overlay the area that is about the be cropped using the same array points.
When you compare the 2, they are slightly out. Can anyone help or point me in the right direction to solve this. My end aim is to try and come up with a program where the user can add multiple points to the image and move the points to
cut around an object.
Thanks...
Here is my code that you can paste straight into a new project:
<pre class="prettyprint 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;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Point[] points;
public Form1()
{
InitializeComponent();
//Define area that will be cropped (in this case a square - top right of image)
points = new Point[4];
points[0] = new Point(10, 10);
points[1] = new Point(10, 200);
points[2] = new Point(100, 200);
points[3] = new Point(100, 10);
//points[4] = new Point(250, 250);
}
private void button1_Click(object sender, EventArgs e)
{
using (GraphicsPath path = new GraphicsPath())
{
//path.AddCurve.(points);
path.AddLines(points);
using (Region reg = new Region(path))
{
RectangleF rect = reg.GetBounds(Graphics.FromImage(pictureBox1.Image));
using (Bitmap bmp = new Bitmap((int)rect.Width, (int)rect.Height))
{
using (Graphics g = Graphics.FromImage(bmp))
{
reg.Translate(rect.X, rect.Y);
g.Clip = reg;
g.DrawImage(pictureBox1.Image, new Rectangle(Point.Empty, bmp.Size), rect, GraphicsUnit.Pixel);
}
bmp.Save("c:\region.Png", ImageFormat.Png);
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
//Overlay points and lines that define area that will be cropped
Graphics dc = pictureBox1.CreateGraphics();
Pen redPen = new Pen(Color.Red, 1);
Pen bluePen = new Pen(Color.Blue, 1);
dc.DrawEllipse(redPen, points[0].X, points[0].Y, 1, 1);
dc.DrawEllipse(redPen, points[1].X,points[1].Y, 1, 1);
dc.DrawEllipse(redPen, points[2].X,points[2].Y, 1, 1);
dc.DrawEllipse(redPen, points[3].X, points[3].Y, 1, 1);
dc.DrawLine(bluePen, points[0], points[1]);
dc.DrawLine(bluePen, points[0], points[3]);
dc.DrawLine(bluePen, points[3], points[2]);
dc.DrawLine(bluePen, points[1], points[2]);
}
}
}
[/code]
<br/>
View the full article