C#画图,Graphics只有在使用CreateGraphics()方法时才能显示,在上面作图
使用创建bitmap,Graphics.FromImage()不能在上面作图。publicpartialclassForm1:Form{privateboolopenFi...
使用创建bitmap,Graphics.FromImage()不能在上面作图。
public partial class Form1 : Form
{
private bool openFile = false;
private bool draw = false;
private int pointNums = 0;
private Point start, end,third;
private int choice = 0;
const int LINE = 100001;
const int CIRCLE = 100002;
const int ELLIPSE = 100003;
private string fileString = "C:\\Users\\lenovo\\Desktop\\";
private string savefileString = "C:\\Users\\lenovo\\Desktop\\";
Pen p = new Pen(Color.Black, 1);
Pen p2 = new Pen(Color.Red, 2);
Graphics g ;
Bitmap b;
public Form1()
{
InitializeComponent();
if (g != null)
{
g.Dispose();
}
g = pictureBox1.CreateGraphics();
/* b = new Bitmap(pictureBox1.Width, pictureBox1.Height);
//g.Save();
pictureBox1.Image = b;
g = Graphics.FromImage(pictureBox1.Image);
g.Save();*/
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
DrawPoint(new Point(45,64));
DrawLine(new Point(e.X, e.Y), new Point(44, 64));
/* //点击一下 第一个点
if (pointNums == 0)
{
start = new Point(e.X, e.Y);
DrawPoint(start);
pointNums++;
return;
}
//点击两次 第二个点
else if (pointNums == 1)
{
end = new Point(e.X, e.Y);
DrawPoint(end);
pointNums++;
draw = true;
if (pointNums == 2&& draw == true)
{
if (choice == LINE)
{
DrawLine(start, end);
}
else if (choice == CIRCLE)
{
DrawCircle(start, end);
}
pointNums = 0;
draw = false;
choice = 0;
return;
}
}
//点击三次 画椭圆 第三个点
else if (pointNums == 2&&choice == ELLIPSE&&draw == true)
{
third = new Point(e.X,e.Y);
DrawPoint(third);
pointNums++;
draw = true;
if (pointNums == 3 && draw == true && choice == ELLIPSE)
{
DrawEllipse(start, end, third);
pointNums = 0;
draw = false;
choice = 0;
return;
}
}*/
} 展开
public partial class Form1 : Form
{
private bool openFile = false;
private bool draw = false;
private int pointNums = 0;
private Point start, end,third;
private int choice = 0;
const int LINE = 100001;
const int CIRCLE = 100002;
const int ELLIPSE = 100003;
private string fileString = "C:\\Users\\lenovo\\Desktop\\";
private string savefileString = "C:\\Users\\lenovo\\Desktop\\";
Pen p = new Pen(Color.Black, 1);
Pen p2 = new Pen(Color.Red, 2);
Graphics g ;
Bitmap b;
public Form1()
{
InitializeComponent();
if (g != null)
{
g.Dispose();
}
g = pictureBox1.CreateGraphics();
/* b = new Bitmap(pictureBox1.Width, pictureBox1.Height);
//g.Save();
pictureBox1.Image = b;
g = Graphics.FromImage(pictureBox1.Image);
g.Save();*/
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
DrawPoint(new Point(45,64));
DrawLine(new Point(e.X, e.Y), new Point(44, 64));
/* //点击一下 第一个点
if (pointNums == 0)
{
start = new Point(e.X, e.Y);
DrawPoint(start);
pointNums++;
return;
}
//点击两次 第二个点
else if (pointNums == 1)
{
end = new Point(e.X, e.Y);
DrawPoint(end);
pointNums++;
draw = true;
if (pointNums == 2&& draw == true)
{
if (choice == LINE)
{
DrawLine(start, end);
}
else if (choice == CIRCLE)
{
DrawCircle(start, end);
}
pointNums = 0;
draw = false;
choice = 0;
return;
}
}
//点击三次 画椭圆 第三个点
else if (pointNums == 2&&choice == ELLIPSE&&draw == true)
{
third = new Point(e.X,e.Y);
DrawPoint(third);
pointNums++;
draw = true;
if (pointNums == 3 && draw == true && choice == ELLIPSE)
{
DrawEllipse(start, end, third);
pointNums = 0;
draw = false;
choice = 0;
return;
}
}*/
} 展开
3个回答
展开全部
private int pointNums = 0;
private Point start, end;
private int choice = ELLIPSE; //主要修改这个改变两个点生成什么
const int LINE = 100001;
const int CIRCLE = 100002;
const int ELLIPSE 判含= 100003;
Pen p = new Pen(Color.Black, 1);
Bitmap b;
Graphics g;
public Form1()
{
InitializeComponent();
b = new Bitmap(pictureBox1.Width, pictureBox1.Height);
g = Graphics.FromImage(b);
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
//点击一下 第一个点
if (pointNums == 0)
{
start = new Point(e.X, e.Y);
//因为没有画点的方法,所以使用填充矩形的方式画点
g.FillRectangle(Brushes.Red, new Rectangle(start, new Size(1, 1)));
pointNums++;
}
//点击两次 第二个点
else if (pointNums == 1)
{
end = new Point(e.X, e.Y);
g.FillRectangle(Brushes.Red, new Rectangle(end, new Size(1, 1)));
if (choice == LINE) //画线
{
g.DrawLine(p, start, end);
}
else if (choice == CIRCLE) //画圆
{
g.DrawEllipse(p, GetRectangle(start, end, true));
}
else if (choice == ELLIPSE) //画椭圆
{
g.DrawEllipse(p, GetRectangle(start, end));
}
pointNums = 0; //重置状态
}
pictureBox1.Image = b; //图形覆盖,生效
}
//根据两个点,计算形成的左上角座标,以及形成的大小
//isCircle:是否为画圆,不传,默认为 false
private Rectangle GetRectangle(Point p1, Point p2, bool isCircle = false)
{
int 和冲穗x = Math.Min(p1.X, p2.X);
int y = Math.Min(p1.Y, p2.Y);
int w = Math.Abs(p1.X - p2.X);
int h = 唤卜Math.Abs(p1.Y - p2.Y);
if (isCircle) //如果为画圆,则把长宽都设为,长与宽,小的那个值
{
w = h = Math.Min(w, h);
}
//返回一个矩形
return new Rectangle(new Point(x, y), new Size(w, h));
}
精简了你的一些判断,和状态控制
如果需要的话,在这基础上修改吧
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询