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;
namespace PM_Imaging
{
public partial class abdomenalForm : Form
{
public string str;
bool imageOpened = false;
static int quadrants = 0;
UserRect rect;
public abdomenalForm()
{
InitializeComponent();
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
{
// Display the image in the PictureBox.
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
// Display the Resolution of a loaded image in a status bar
imgSizeStatusLabel.Text = "" + pictureBox1.Image.Width + "x" + pictureBox1.Image.Height + "";
this.DisplayScrollBars();
this.SetScrollBarValues();
}
imageOpened = true;
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (imageOpened == true)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "JPEG Files(*.jpg)|*.jpg";
Bitmap b11 = (Bitmap)pictureBox1.Image;
//save the edited file
if (sfd.ShowDialog() == DialogResult.OK)
b11.Save(sfd.FileName);
}
else
MessageBox.Show("No image to save");
}
private void HandleScroll(object sender, ScrollEventArgs e)
{
// Handling the scroll events
Graphics g = pictureBox1.CreateGraphics();
g.DrawImage(pictureBox1.Image,
new Rectangle(0, 0, pictureBox1.Right - vScrollBar1.Width,
pictureBox1.Bottom - hScrollBar1.Height),
new Rectangle(hScrollBar1.Value, vScrollBar1.Value,
pictureBox1.Right - vScrollBar1.Width,
pictureBox1.Bottom - hScrollBar1.Height),
GraphicsUnit.Pixel);
pictureBox1.Update();
}
public void DisplayScrollBars()
{
// If the image is wider than the PictureBox, show the HScrollBar.
if (pictureBox1.Width > pictureBox1.Image.Width - this.vScrollBar1.Width)
{
hScrollBar1.Visible = false;
}
else
{
hScrollBar1.Visible = true;
}
// If the image is taller than the PictureBox, show the VScrollBar.
if (pictureBox1.Height >
pictureBox1.Image.Height - this.hScrollBar1.Height)
{
vScrollBar1.Visible = false;
}
else
{
vScrollBar1.Visible = true;
}
}
public void SetScrollBarValues()
{
// Set the Maximum, Minimum, LargeChange and SmallChange properties.
this.vScrollBar1.Minimum = 0;
this.hScrollBar1.Minimum = 0;
// If the offset does not make the Maximum less than zero, set its value.
if ((this.pictureBox1.Image.Size.Width - pictureBox1.ClientSize.Width) > 0)
{
this.hScrollBar1.Maximum =
this.pictureBox1.Image.Size.Width - pictureBox1.ClientSize.Width;
}
// If the VScrollBar is visible, adjust the Maximum of the
// HSCrollBar to account for the width of the VScrollBar.
if (this.vScrollBar1.Visible)
{
this.hScrollBar1.Maximum += this.vScrollBar1.Width;
}
this.hScrollBar1.LargeChange = this.hScrollBar1.Maximum / 10;
this.hScrollBar1.SmallChange = this.hScrollBar1.Maximum / 20;
// Adjust the Maximum value to make the raw Maximum value
// attainable by user interaction.
this.hScrollBar1.Maximum += this.hScrollBar1.LargeChange;
// If the offset does not make the Maximum less than zero, set its value.
if ((this.pictureBox1.Image.Size.Height - pictureBox1.ClientSize.Height) > 0)
{
this.vScrollBar1.Maximum =
this.pictureBox1.Image.Size.Height - pictureBox1.ClientSize.Height;
}
// If the HScrollBar is visible, adjust the Maximum of the
// VSCrollBar to account for the width of the HScrollBar.
if (this.hScrollBar1.Visible)
{
this.vScrollBar1.Maximum += this.hScrollBar1.Height;
}
this.vScrollBar1.LargeChange = this.vScrollBar1.Maximum / 10;
this.vScrollBar1.SmallChange = this.vScrollBar1.Maximum / 20;
// Adjust the Maximum value to make the raw Maximum value
// attainable by user interaction.
this.vScrollBar1.Maximum += this.vScrollBar1.LargeChange;
}
private void clearToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.Image = null;
}
private void grid4ToolStripMenuItem2_Click(object sender, EventArgs e)
{
str = "";
pictureBox1.MouseClick += new MouseEventHandler(pictureBox1_MouseClick);
// Draw 4 grids on a loaded image
Pen p1 = new Pen(Color.White, 1);
Pen p2 = new Pen(Color.White, 1);
p1.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
p2.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
float imght, imgwd, xmid, ymid;
imght = pictureBox1.Image.Height;
imgwd = pictureBox1.Image.Width;
xmid = (imgwd) / 2;
ymid = (imght) / 2;
Bitmap b1 = (Bitmap)pictureBox1.Image;
pictureBox1.Image = b1;
Graphics g1 = Graphics.FromImage(b1);
//g1.DrawLine(p1, xmid, 0, xmid, imght);
//g1.DrawLine(p2, 0, ymid, imgwd, ymid);
rect = new UserRect(new Rectangle((int)xmid, 0, 1, (int)imght));
rect.SetPictureBox(pictureBox1);
rect = new UserRect(new Rectangle(0,(int)ymid,(int)imgwd,1));
rect.SetPictureBox(pictureBox1);
quadrants = 4;
}
private void grid8ToolStripMenuItem3_Click(object sender, EventArgs e)
{
str = "";
pictureBox1.MouseClick += new MouseEventHandler(pictureBox1_MouseClick);
// Draw 8 grids on a loaded image
Pen p3 = new Pen(Color.White, 1);
Pen p4 = new Pen(Color.White, 1);
Pen p5 = new Pen(Color.White, 1);
Pen p6 = new Pen(Color.White, 1);
p3.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
p4.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
p5.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
p6.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
float imght, imgwd, xmid, xmid1, xmid2, ymid;
imght = pictureBox1.Image.Height;
imgwd = pictureBox1.Image.Width;
xmid = (imgwd) / 2;
xmid1 = (imgwd) / 4;
xmid2 = 3 * (imgwd) / 4;
ymid = (imght) / 2;
Bitmap b2 = (Bitmap)pictureBox1.Image;
pictureBox1.Image = b2;
Graphics g2 = Graphics.FromImage(b2);
//g2.DrawLine(p3, xmid, 0, xmid, imght);
//g2.DrawLine(p4, xmid1, 0, xmid1, imght);
//g2.DrawLine(p5, xmid2, 0, xmid2, imght);
//g2.DrawLine(p6, 0, ymid, imgwd, ymid);
rect = new UserRect(new Rectangle((int)xmid, 0, 1, (int)imght));
rect.SetPictureBox(this.pictureBox1);
rect = new UserRect(new Rectangle((int)xmid1, 0, 1, (int)imght));
rect.SetPictureBox(this.pictureBox1);
rect = new UserRect(new Rectangle((int)xmid2, 0, 1, (int)imght));
rect.SetPictureBox(this.pictureBox1);
rect = new UserRect(new Rectangle(0, (int)ymid, (int)imgwd, 1));
rect.SetPictureBox(this.pictureBox1);
quadrants = 8;
}
private void grid9ToolStripMenuItem4_Click(object sender, EventArgs e)
{
str = "";
pictureBox1.MouseClick += new MouseEventHandler(pictureBox1_MouseClick);
// Draw 9 grids on a loaded image
Pen p3 = new Pen(Color.White, 1);
Pen p4 = new Pen(Color.White, 1);
Pen p5 = new Pen(Color.White, 1);
Pen p6 = new Pen(Color.White, 1);
p3.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
p4.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
p5.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
p6.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
float imght, imgwd, xmid, xmid1, ymid, ymid1;
imght = pictureBox1.Image.Height;
imgwd = pictureBox1.Image.Width;
xmid = (imgwd) / 3;
xmid1 = 2 * (imgwd) / 3;
ymid = (imght) / 3;
ymid1 = 2 * (imght) / 3;
Bitmap b2 = (Bitmap)pictureBox1.Image;
pictureBox1.Image = b2;
Graphics g2 = Graphics.FromImage(b2);
//g2.DrawLine(p3, xmid, 0, xmid, imght);
//g2.DrawLine(p4, xmid1, 0, xmid1, imght);
//g2.DrawLine(p5, 0, ymid, imgwd, ymid);
//g2.DrawLine(p6, 0, ymid1, imgwd, ymid1);
rect = new UserRect(new Rectangle((int)xmid, 0, 1, (int)imght));
rect.SetPictureBox(this.pictureBox1);
rect = new UserRect(new Rectangle((int)xmid1, 0, 1, (int)imght));
rect.SetPictureBox(this.pictureBox1);
rect = new UserRect(new Rectangle(0, (int)ymid, (int)imgwd, 1));
rect.SetPictureBox(this.pictureBox1);
rect = new UserRect(new Rectangle(0, (int)ymid1, (int)imgwd, 1));
rect.SetPictureBox(this.pictureBox1);
quadrants = 9;
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
// on mouse click, display which grid is clicked in a status bar
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
Bitmap b = (Bitmap)pictureBox1.Image;
pictureBox1.Image = b;
Graphics g = Graphics.FromImage(b);
int x = e.X;
int y = e.Y;
if (quadrants == 4)
{
//1
if (x <= (b.Width) / 2 && y <= (b.Height) / 2)
{
GridStatusLabel.Text = "(1,1)";
}
else if (x <= b.Width && y <= (b.Height) / 2)
{
GridStatusLabel.Text = "(1,2)";
}
else if (x <= (b.Width) / 2 && y <= b.Height)
{
GridStatusLabel.Text = "(2,1)";
}
else if (x <= (b.Width) && y <= (b.Height))
{
GridStatusLabel.Text = "(2,2)";
}
}
else if (quadrants == 8)
{
if (x <= (b.Width) / 4 && y <= (b.Height) / 2)
{
GridStatusLabel.Text = "(1,1)";
}
else if (x <= (b.Width) / 2 && y <= (b.Height) / 2)
{
GridStatusLabel.Text = "(1,2)";
}
else if (x <= (3 * (b.Width)) / 4 && y <= (b.Height) / 2)
{
GridStatusLabel.Text = "(1,3)";
}
else if (x <= b.Width && y <= (b.Height) / 2)
{
GridStatusLabel.Text = "(1,4)";
}
else if (x <= (b.Width) / 4 && y <= b.Height)
{
GridStatusLabel.Text = "(2,1)";
}
else if (x <= (b.Width) / 2 && y <= b.Height)
{
GridStatusLabel.Text = "(2,2)";
}
else if (x <= (3 * (b.Width)) / 4 && y <= b.Height)
{
GridStatusLabel.Text = "(2,3)";
}
else if (x <= (b.Width) && y <= (b.Height))
{
GridStatusLabel.Text = "(2,4)";
}
}
else if (quadrants == 9)
{
if (x <= (b.Width) / 3 && y <= (b.Height) / 3)
{
GridStatusLabel.Text = "(1,1)";
}
else if (x <= (2*(b.Width)) / 3 && y <= (b.Height) / 3)
{
GridStatusLabel.Text = "(1,2)";
}
else if (x <= (b.Width) && y <= (b.Height) / 3)
{
GridStatusLabel.Text = "(1,3)";
}
else if (x <= (b.Width) / 3 && y <= (2 * (b.Height)) / 3)
{
GridStatusLabel.Text = "(2,1)";
}
else if (x <= (2 * (b.Width)) / 3 && y <= (2 * (b.Height)) / 3)
{
GridStatusLabel.Text = "(2,2)";
}
else if (x <= (b.Width) && y <= (2 * (b.Height)) / 3)
{
GridStatusLabel.Text = "(2,3)";
}
else if (x <= (b.Width) / 3 && y <= b.Height)
{
GridStatusLabel.Text = "(3,1)";
}
else if (x <= (2 * (b.Width)) / 3 && y <= (b.Height))
{
GridStatusLabel.Text = "(3,2)";
}
else if (x <= b.Width && y <= b.Height)
{
GridStatusLabel.Text = "(3,3)";
}
}
}
}
/********************************/
/* This is a class used to draw movable rectangles*/
/* am drawing lines as rectangles with width 1 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace PM_Imaging
{
class UserRect
{
private PictureBox mPictureBox;
public Rectangle rect;
private bool mIsClick=false;
private bool mMove=false;
private int oldX;
private int oldY;
private Bitmap mBmp=null;
private PosSizableRect nodeSelected = PosSizableRect.None;
private enum PosSizableRect
{
None
};
public UserRect(Rectangle r)
{
rect = r;
mIsClick = false;
}
public void Draw(Graphics g)
{
g.DrawRectangle(new Pen(Color.White,2),rect);
foreach (PosSizableRect pos in Enum.GetValues(typeof(PosSizableRect)))
{
g.DrawRectangle(new Pen(Color.Red,2),GetRect(pos));
}
}
public void SetBitmapFile(string filename)
{
this.mBmp = new Bitmap(filename);
}
public void SetBitmap(Bitmap bmp)
{
this.mBmp = bmp;
}
public void SetPictureBox(PictureBox p)
{
this.mPictureBox = p;
mPictureBox.MouseDown +=new MouseEventHandler(mPictureBox_MouseDown);
mPictureBox.MouseUp += new MouseEventHandler(mPictureBox_MouseUp);
mPictureBox.MouseMove += new MouseEventHandler(mPictureBox_MouseMove);
mPictureBox.Paint += new PaintEventHandler(mPictureBox_Paint);
}
private void mPictureBox_Paint(object sender, PaintEventArgs e)
{
try
{
Draw(e.Graphics);
}
catch (Exception exp)
{
System.Console.WriteLine(exp.Message);
}
}
private void mPictureBox_MouseDown(object sender, MouseEventArgs e)
{
mIsClick = true;
nodeSelected = PosSizableRect.None;
nodeSelected = GetNodeSelectable(e.Location);
if (rect.Contains(new Point(e.X, e.Y)))
{
mMove = true;
}
oldX = e.X;
oldY = e.Y;
}
private void mPictureBox_MouseUp(object sender, MouseEventArgs e)
{
mIsClick = false;
mMove = false;
}
private void mPictureBox_MouseMove(object sender, MouseEventArgs e)
{
ChangeCursor(e.Location);
if (mIsClick == false)
{
return;
}
Rectangle backupRect = rect;
switch (nodeSelected)
{
default:
if (mMove)
{
rect.X = rect.X + e.X - oldX;
rect.Y = rect.Y + e.Y - oldY;
}
break;
}
oldX = e.X;
oldY = e.Y;
TestIfRectInsideArea();
mPictureBox.Invalidate();
}
private void TestIfRectInsideArea()
{
// Test if rectangle still inside the area.
if (rect.X < 0) rect.X = 0;
if (rect.Y < 0) rect.Y = 0;
if (rect.Width <= 0) rect.Width = 1;
if (rect.Height <= 0) rect.Height = 1;
if (rect.X + rect.Width > mPictureBox.Width)
{
rect.Width = mPictureBox.Width - rect.X - 1; // -1 to be still show
}
if (rect.Y + rect.Height > mPictureBox.Height)
{
rect.Height = mPictureBox.Height - rect.Y - 1;// -1 to be still show
}
}
private Rectangle GetRect(PosSizableRect p)
{
switch (p)
{
default :
return new Rectangle();
}
}
private PosSizableRect GetNodeSelectable(Point p)
{
foreach (PosSizableRect r in Enum.GetValues(typeof(PosSizableRect)))
{
if (GetRect(r).Contains(p))
{
return r;
}
}
return PosSizableRect.None;
}
private void ChangeCursor(Point p)
{
mPictureBox.Cursor = GetCursor(GetNodeSelectable(p));
}
private Cursor GetCursor(PosSizableRect p)
{
switch (p)
{
case PosSizableRect.None:
return Cursors.SizeAll;
default:
return Cursors.Default;
}
}
}
}