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;
}
}*/
}
展开
 我来答
雨月蓝
推荐于2016-09-19 · TA获得超过765个赞
知道小有建树答主
回答量:194
采纳率:100%
帮助的人:249万
展开全部
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));
}

精简了你的一些判断,和状态控制

如果需要的话,在这基础上修改吧

金苹果3101221
2014-12-15
知道答主
回答量:38
采纳率:0%
帮助的人:19.1万
展开全部
Graphics.DrawString可以画图
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
s不知是错s
2014-12-15 · 超过19用户采纳过TA的回答
知道答主
回答量:59
采纳率:0%
帮助的人:40.2万
展开全部
同学,你哪个学校的?GIS吗?是周蕙教吗?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式