读书人

为什么图像绘制不出来

发布时间: 2013-07-09 09:50:48 作者: rapoo

为何图像绘制不出来
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace texun
{
public partial class Form1 : Form
{

private bool isStart = false;
Thread pt = null;
public int i = 0;
private Graphics newDM = null;
private Bitmap bufferimg = null;



public Form1()
{
InitializeComponent();
LaunchForm();

}

private void LaunchForm()
{
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true);
isStart = true;

newDM = Graphics.FromImage(bufferimg);
}

private void PaintThread()
{
while (isStart)
{
DMMove(newDM);
this.Invalidate();
Thread.Sleep(100);
}


}

private void Form1_Load(object sender, EventArgs e)
{
pt = new Thread(new ThreadStart(PaintThread));
pt.Start();
}

private void DMMove(Graphics g)
{
int i = 0;
SolidBrush sb = new SolidBrush(Color.Red);
g.FillEllipse(sb, i += 3, i += 3, 10, 10);
}

private void Form1_Paint(object sender, PaintEventArgs e)
{

}
}
}

本来想画一个移动的点的,但是DMMove()方法貌似出了问题。我是新手……麻烦浅显点…… GDI+ Bitmap Graphics
[解决办法]
CreateGraphics 是属于Control类的,那么只要继承Control的都可以,

首先,你要看到图片,那么展示图片的控件可以是picturebox,picturebox需存在于窗体上,然后调用picturebox的CreateGraphics 创建画板
[解决办法]

引用:
Quote: 引用:

Quote: 引用:

Quote: 引用:

Quote: 引用:

Quote: 引用:

放哪里不重要,重要的是你要显示在哪。你都没地方显示,画了也看不到。


private void Form1_Paint(object sender, PaintEventArgs e)
{


SolidBrush sb = new SolidBrush(Color.Red);
i += 3;
e.Graphics.FillEllipse(sb, 200 - i, i, 10, 10);
}
我这样的话倒是有图,但是我想把e.Graphics.FillEllipse(sb, 200 - i, i, 10, 10);全部装在一个叫做DM1的类的DrawDM()方法里面。这个是我DM1的类的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace texun
{
public class DM1
{
private int x = 0;
private int y = 0;
public int X
{
set { x = value; }
get { return x; }
}

public int Y
{
set { y = value; }
get { return y; }
}
public DM1()
{

}
public void DrawDM(Graphics g)
{
int i = 0;
SolidBrush sb = new SolidBrush(Color.Red);
g.FillEllipse(sb, i += 3, i += 3, 10, 10);

}
}
}



你没明白我的意思,你画一张图后,在哪显示。你都没容器给他显示,你当然看不到拉。


所以容器必须是picturebox么?还是说有别的方法?



不用PictureBox也可以呀用Form的BackgroundImage也行,反正只要有地方让他显示就行




bufferImg = new Bitmap(WIDTH, HEIGHT);这样新建一张位图,这张图就算是会显示的那种吧?


这个实列化之后,把bufferImg对像赋给某个容器就行了。

读书人网 >C#

热点推荐