绘制网格后无法保存图像 [英] Not able to save the image after drawing grids

查看:87
本文介绍了绘制网格后无法保存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用加载图像并将图像划分为相等的4,8或9网格的工具.用于绘制网格的线是可拖动的,即它是一种标尺.但是绘制网格后,我无法保存编辑后的图像.
我正在使用Windows窗体和图片框加载图像.

请对此提供帮助.

在此先感谢..

Hi,

I am working on a tool which loads an image and divide the image into equal 4,8 or 9 grids. the lines used to draw grids are dragable i.e., its a kind of ruler.But after drawing grids, i am not able to save the edited image.
I am using windows form and picture box for loading image.

please help in this regard.

Thanks in Advance..

推荐答案

您需要先保存图像对象,然后再对其进行修改.在PictureBox内部绘图(使用Paint事件)不会更改其Image.

要修改位图的内部数据,请查看以下链接,例如:
http://www.bobpowell.net/lockingbits.htm [
You need to modify the image object before saving it. Drawing inside the PictureBox (using the Paint event) doesn''t change its Image.

To modify bitmap''s internal data, have a look to this link for example:
http://www.bobpowell.net/lockingbits.htm[^]


使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;
命名空间PM_Imaging
{
公共局部类abdomenalForm:形式
{
公共字符串str;
bool imageOpened = false;
静态整数象限= 0;
UserRect rect;
公共abdomenalForm()
{
InitializeComponent();
}
私人void openToolStripMenuItem_Click(object sender,EventArgs e)
{
OpenFileDialog openFileDialog1 =新的OpenFileDialog();
如果(openFileDialog1.ShowDialog()!= DialogResult.Cancel)
{
//在PictureBox中显示图像.
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
//在状态栏中显示已加载图像的分辨率
imgSizeStatusLabel.Text =" + pictureBox1.Image.Width +"x" + pictureBox1.Image.Height +";
this.DisplayScrollBars();
this.SetScrollBarValues();
}
imageOpened = true;
}
私人void saveToolStripMenuItem_Click(object sender,EventArgs e)
{
如果(imageOpened == true)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter ="JPEG文件(* .jpg)| * .jpg";
位图b11 =(位图)pictureBox1.Image;
//保存编辑后的文件
如果(sfd.ShowDialog()== DialogResult.OK)
b11.Save(sfd.FileName);
}
其他
MessageBox.Show(没有要保存的图像");
}
私有void HandleScroll(对象发送者,ScrollEventArgs e)
{
//处理滚动事件
图形g = pictureBox1.CreateGraphics();
g.DrawImage(pictureBox1.Image,
新Rectangle(0,0,pictureBox1.Right-vScrollBar1.Width,
pictureBox1.Bottom-hScrollBar1.Height),
新的Rectangle(hScrollBar1.Value,vScrollBar1.Value,
pictureBox1.Right-vScrollBar1.Width,
pictureBox1.Bottom-hScrollBar1.Height),
GraphicsUnit.Pixel);
pictureBox1.Update();
}
公共无效的DisplayScrollBars()
{
//如果图像比PictureBox宽,请显示HScrollBar.
如果(pictureBox1.Width& pictureBox1.Image.Width-this.vScrollBar1.Width)
{
hScrollBar1.Visible = false;
}
其他
{
hScrollBar1.Visible = true;
}
//如果图像比PictureBox高,请显示VScrollBar.
如果(pictureBox1.Height& gt;
pictureBox1.Image.Height-this.hScrollBar1.Height)
{
vScrollBar1.Visible = false;
}
其他
{
vScrollBar1.Visible = true;
}
}
公共无效SetScrollBarValues()
{
//设置最大值,最小值,LargeChange和SmallChange属性.
this.vScrollBar1.Minimum = 0;
this.hScrollBar1.Minimum = 0;

//如果偏移量不使最大值"小于零,请设置其值.
if((this.pictureBox1.Image.Size.Width-pictureBox1.ClientSize.Width)& gt; 0)
{
this.hScrollBar1.Maximum =
this.pictureBox1.Image.Size.Width-pictureBox1.ClientSize.Width;
}
//如果VScrollBar可见,请调整
的最大值 //HSCrollBar以考虑VScrollBar的宽度.
如果(this.vScrollBar1.Visible)
{
this.hScrollBar1.Maximum + = this.vScrollBar1.Width;
}
this.hScrollBar1.LargeChange = this.hScrollBar1.Maximum/10;
this.hScrollBar1.SmallChange = this.hScrollBar1.Maximum/20;

//调整最大值以使原始最大值
//通过用户互动即可实现.
this.hScrollBar1.Maximum + = this.hScrollBar1.LargeChange;
//如果偏移量不使最大值"小于零,请设置其值.
如果((this.pictureBox1.Image.Size.Height-pictureBox1.ClientSize.Height)& gt; 0)
{
this.vScrollBar1.Maximum =
this.pictureBox1.Image.Size.Height-pictureBox1.ClientSize.Height;
}
//如果HScrollBar可见,请调整
的最大值 //VSCrollBar表示HScrollBar的宽度.
如果(this.hScrollBar1.Visible)
{
this.vScrollBar1.Maximum + = this.hScrollBar1.Height;
}
this.vScrollBar1.LargeChange = this.vScrollBar1.Maximum/10;
this.vScrollBar1.SmallChange = this.vScrollBar1.Maximum/20;
//调整最大值以使原始最大值
//通过用户互动即可实现.
this.vScrollBar1.Maximum + = this.vScrollBar1.LargeChange;
}
私有void clearToolStripMenuItem_Click(对象发送者,EventArgs e)
{
pictureBox1.Image = null;
}
private void grid4ToolStripMenuItem2_Click(object sender,EventArgs e)
{
str =";
pictureBox1.MouseClick + =新的MouseEventHandler(pictureBox1_MouseClick);
//在加载的图像上绘制4个网格
笔p1 =新笔(Color.White,1);
笔p2 =新笔(彩色.白色,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;
位图b1 =(位图)pictureBox1.Image;
pictureBox1.Image = b1;
图形g1 = Graphics.FromImage(b1);
//g1.DrawLine(p1,xmid,0,xmid,imght);
//g1.DrawLine(p2,0,ymid,imgwd,ymid);
rect =新的UserRect(新的Rectangle((int)xmid,0,1,(int)imght));
rect.SetPictureBox(pictureBox1);
rect =新的UserRect(新的Rectangle(0,(int)ymid,(int)imgwd,1));
rect.SetPictureBox(pictureBox1);
象限= 4;
}
私有void grid8ToolStripMenuItem3_Click(对象发送者,EventArgs e)
{
str =";
pictureBox1.MouseClick + =新的MouseEventHandler(pictureBox1_MouseClick);
//在已加载的图像上绘制8个网格
笔p3 =新笔(彩色.白色,1);
笔p4 =新笔(Color.White,1);
笔p5 =新笔(彩色,白色,1);
笔p6 =新笔(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;
位图b2 =(位图)pictureBox1.Image;
pictureBox1.Image = b2;
图形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 =新的UserRect(新的Rectangle((int)xmid,0,1,(int)imght));
rect.SetPictureBox(this.pictureBox1);
rect =新的UserRect(新的Rectangle((int)xmid1,0,1,(int)imght));
rect.SetPictureBox(this.pictureBox1);
rect =新的UserRect(新的Rectangle((int)xmid2,0,1,(int)imght));
rect.SetPictureBox(this.pictureBox1);
rect =新的UserRect(新的Rectangle(0,(int)ymid,(int)imgwd,1));
rect.SetPictureBox(this.pictureBox1);
象限= 8;
}
private void grid9ToolStripMenuItem4_Click(object sender,EventArgs e)
{
str =";
pictureBox1.MouseClick + =新的MouseEventHandler(pictureBox1_MouseClick);
//在已加载的图像上绘制9个网格
笔p3 =新笔(彩色.白色,1);
笔p4 =新笔(Color.White,1);
笔p5 =新笔(彩色,白色,1);
笔p6 =新笔(彩色.白色,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;
位图b2 =(位图)pictureBox1.Image;
pictureBox1.Image = b2;
图形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 =新的UserRect(新的Rectangle((int)xmid,0,1,(int)imght));
rect.SetPictureBox(this.pictureBox1);
rect =新的UserRect(新的Rectangle((int)xmid1,0,1,(int)imght));
rect.SetPictureBox(this.pictureBox1);
rect =新的UserRect(新的Rectangle(0,(int)ymid,(int)imgwd,1));
rect.SetPictureBox(this.pictureBox1);
rect =新的UserRect(新的Rectangle(0,(int)ymid1,(int)imgwd,1));
rect.SetPictureBox(this.pictureBox1);
象限= 9;
}
私有空pictureBox1_MouseClick(对象发送者,MouseEventArgs e)
{
//鼠标单击时,显示在状态栏中单击了哪个网格
if((e.Button& MouseButtons.Left)== MouseButtons.Left)
{
位图b =(位图)pictureBox1.Image;
pictureBox1.Image = b;
图形g = Graphics.FromImage(b);
int x = e.X;
int y = e.Y;
如果(象限== 4)
{
//1
if(x& lt; =(b.Width)/2& amp; amp; y& lt; =(b.Height)/2)
{
GridStatusLabel.Text =(1,1)";
}
否则,如果(x& lt; = b.宽度& amp; amp; y& lt; =(b.高度)/2)
{
GridStatusLabel.Text =(1,2)";
}
否则(x& lt; =(b.Width)/2& amp; amp; y& lt; = b.Height)
{
GridStatusLabel.Text =(2,1)";
}
否则如果(x& lt ==(b.Width)& amp; amp; y& lt; =(b.Height))
{
GridStatusLabel.Text =(2,2)";
}
}
否则,如果(象限== 8)
{
if(x& lt; =(b.Width)/4& amp; amp; y& lt; =(b.Height)/2)
{
GridStatusLabel.Text =(1,1)";
}
否则(x& lt; =(b.Width)/2& amp; amp; y& lt; =(b.Height)/2)
{
GridStatusLabel.Text =(1,2)";
}
否则(x& lt =(3 *(b.Width))/4& amp; y& lt ==(b.Height)/2)
{
GridStatusLabel.Text =(1,3)";
}
否则,如果(x& lt; = b.宽度& amp; amp; y& lt; =(b.高度)/2)
{
GridStatusLabel.Text =(1,4)";
}
否则(x& lt; =(b.Width)/4& amp; amp; y& lt; = b.Height)
{
GridStatusLabel.Text =(2,1)";
}
否则(x& lt; =(b.Width)/2& amp; amp; y& lt; = b.Height)
{
GridStatusLabel.Text =(2,2)";
}
否则,如果(x& lt ==(3 *(b.Width))/4& amp; y& lt; = b.Height)
{
GridStatusLabel.Text =(2,3)";
}
否则如果(x& lt ==(b.Width)& amp; amp; y& lt; =(b.Height))
{
GridStatusLabel.Text =(2,4)";
}
}
否则,如果(象限== 9)
{
如果(x& lt ==(b.Width)/3& amp; amp; y& lt; =(b.Height)/3)
{
GridStatusLabel.Text =(1,1)";
}
否则如果(x& lt =(2 *(b.Width))/3& amp; y& lt ==(b.Height)/3)
{
GridStatusLabel.Text =(1,2)";
}
否则,如果(x& lt; =(b.Width)& amp; amp; y& lt; =(b.Height)/3)
{
GridStatusLabel.Text =(1,3)";
}
否则如果(x& lt; =(b.Width)/3& amp; amp; y& lt; =(2 *(b.Height))/3)
{
GridStatusLabel.Text =(2,1)";
}
否则if(x& lt ==(2 *(b.Height))/3& amp; y& lt; =(2 *(b.Height))/3)
{
GridStatusLabel.Text =(2,2)";
}
否则((x& lt; =(b.Width)& amp; amp; y& lt; =(2 *(b.Height))/3)
{
GridStatusLabel.Text =(2,3)";
}
否则,如果(x& lt ==(b.Width)/3& amp; amp; y& lt; = b.Height)
{
GridStatusLabel.Text =(3,1)";
}
否则,如果(x& lt ==(2 *(b.Width))/3& amp; y& lt ==(b.Height))
{
GridStatusLabel.Text =(3,2)";
}
否则(x& lt; = b.Width& amp; amp; y& lt; = b.Height)
{
GridStatusLabel.Text =(3,3)";
}
}
}
}

/********************************//
/*这是用于绘制可移动矩形的类*/
/*将线条绘制为宽度为1的矩形*/
使用系统;
使用System.Collections.Generic;
使用System.Text;
使用System.Drawing;
使用System.Drawing.Drawing2D;
使用System.Windows.Forms;
命名空间PM_Imaging
{
类UserRect
{
私人PictureBox mPictureBox;
公共矩形rect;
私人布尔mIsClick = false;
私人布尔mMove = false;
private int oldX;
private int oldY;
私人位图mBmp = null;
私有PosSizableRect nodeSelected = PosSizableRect.None;
私有枚举PosSizableRect
{

};
public UserRect(矩形r)
{
rect = r;
mIsClick = false;
}
公共无效Draw(Graphics g)
{
g.DrawRectangle(new Pen(Color.White,2),rect);
foreach(Enum.GetValues(typeof(PosSizableRect))中的PosSizableRect pos)
{
g.DrawRectangle(new Pen(Color.Red,2),GetRect(pos));
}
}
公共无效SetBitmapFile(字符串文件名)
{
this.mBmp = new Bitmap(filename);
}
公共无效SetBitmap(位图bmp)
{
this.mBmp = bmp;
}
公共无效SetPictureBox(PictureBox p)
{
this.mPictureBox = p;
mPictureBox.MouseDown + =新的MouseEventHandler(mPictureBox_MouseDown);
mPictureBox.MouseUp + =新的MouseEventHandler(mPictureBox_MouseUp);
mPictureBox.MouseMove + =新的MouseEventHandler(mPictureBox_MouseMove);
mPictureBox.Paint + =新的PaintEventHandler(mPictureBox_Paint);
}
私有void mPictureBox_Paint(对象发送者,PaintEventArgs e)
{
试试
{
Draw(e.Graphics);
}
catch(Exception exp)
{
System.Console.WriteLine(exp.Message);
}
}
私有void mPictureBox_MouseDown(对象发送者,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(对象发送者,MouseEventArgs e)
{
mIsClick = false;
mMove = false;
}
private void mPictureBox_MouseMove(对象发送方,MouseEventArgs e)
{
ChangeCursor(e.Location);
如果(mIsClick == false)
{
返回;
}
矩形backupRect = rect;
开关(nodeSelected)
{
默认值:
如果(mMove)
{
rect.X = rectX X + e.X-oldX;
rect.Y = rect.Y + e.Y-oldY;
}
休息;
}
oldX = e.X;
oldY = e.Y;
TestIfRectInsideArea();
mPictureBox.Invalidate();
}
私有无效TestIfRectInsideArea()
{
//测试矩形是否仍在该区域内.
如果(rect.X& lt; 0)rect.X = 0;
如果(rect.Y& lt; 0)rect.Y = 0;
如果(rect.Width& lt; = 0)rect.Width = 1;
如果(rect.Height& lt; = 0)rect.Height = 1;
如果(rect.X + rect.Width& gt; mPictureBox.Width)
{
rect.Width = mPictureBox.Width-矩形X-1; //-1仍然显示
}
如果(rect.Y + rect.Height& gt; mPictureBox.Height)
{
rect.Height = mPictureBox.Height-矩形Y-1;//-1仍要显示
}
}
私人矩形GetRect(PosSizableRect p)
{
开关(p)
{
默认值:
返回新的Rectangle();
}
}
私有PosSizableRect GetNodeSelectable(Point p)
{
foreach(Enum.GetValues(typeof(PosSizableRect)中的PosSizableRect r))
{
如果(GetRect(r).Contains(p))
{
返回r;
}
}
返回PosSizableRect.None;
}
private void ChangeCursor(Point p)
{
mPictureBox.Cursor = GetCursor(GetNodeSelectable(p));
}
私有Cursor GetCursor(PosSizableRect p)
{
开关(p)
{
case PosSizableRect.None:
返回Cursors.SizeAll;
默认值:
返回Cursors.Default;
}
}
}
}
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;
}
}
}
}


这篇关于绘制网格后无法保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆