我想知道其实正常picturebox的绘图使用是将程序放在什么部分的?
我都是放在timer上面通过定时重绘的方式来处理的,
我想有没有一种方式是可以不用timer的?
个人感觉用timer重绘比较卡,很不爽啊,我觉得应该是本身有其它自动触发重绘功能的,但是不知道是什么诶,求指教啦!!!
大神快来解救下我咧,,,谢谢啦哈哈
我都是放在timer上面通过定时重绘的方式来处理的,
我想有没有一种方式是可以不用timer的?
个人感觉用timer重绘比较卡,很不爽啊,我觉得应该是本身有其它自动触发重绘功能的,但是不知道是什么诶,求指教啦!!!
大神快来解救下我咧,,,谢谢啦哈哈
“绘什么图”在程序里控制,比如定义一个bitmap,在bitmap上绘,如果是矢量图,就定义一个集合,把顶点信息等保存进去,绘图如果需要马上显示出来,就调用Invalidate"怎么绘图"就在OnPaint事件里,读取数据显示出来。这样即使窗口挡住后又可见了,都是会自动重绘的,要手工重绘就调用Invalidate
{
int Length01;
OpenFilePath01 = OpenFilePath01.Replace("jmms", "den");
FileStream str01 = new FileStream(OpenFilePath01, FileMode.Open);
BinaryReader bread01 = new BinaryReader(str01);
Length01 = Convert.ToInt32(str01.Length);
byte[] rdata01 = new byte[Length01];
for (int i = 0; i < Length01; i++)
{
rdata01[i] = bread01.ReadByte(); }
str01.Close(); string YY;
YY = System.Text.Encoding.ASCII.GetString(rdata01); //将转成的Y点放入字符串中
textBox1.Text = YY;
string[] Prdata = Regex.Split(YY, @"\s+"); //以分割空格的方式将Y点放入数组中 pictureBox1.Width = (int)((Prdata.Length / 2) * BiLi + 10); if (flag == true)
{
PicHeight = (int)(PicHeight * BiLi);
BiLi1 = BiLi1 * BiLi;
flag = !flag;
scroll = !scroll;
}
pictureBox1.Height = PicHeight;
//初始化画板 Bitmap image = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height);
//获取背景层
Bitmap BeiJing = (Bitmap)pictureBox1.BackgroundImage; //初始化画布
Bitmap HuaBu = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height); //初始化图形
Graphics g = Graphics.FromImage(image);
Graphics gb = Graphics.FromImage(HuaBu);
PointF[] pL0 = new PointF[Prdata.Length - 1]; //出现多余的斜线是因为这里的长度大于数组的长度
PointF[] pY = new PointF[Prdata.Length - 1];
PointF ptY;
//Prdata.Length-2才是最后一个点
for (ii = 0; ii < Prdata.Length - 1; ii++)
{
pL0[ii] = new PointF(ii / 2, PicHeight / 2);
fx = (float)(ii / 2) * BiLi1;
fy = float.Parse(Prdata[ii]) * 100 * BiLi1;
ptY = new PointF(fx, (PicHeight / 2) - fy);
pY[ii] = ptY;
}
Pen penL = new Pen(Color.Green);
Pen penY = new Pen(Color.Red); g.DrawLines(penY, pY);
g.DrawLines(penL, pL0);
g.Dispose();
gb.DrawImage(BeiJing, 0, 0); //先绘制背景层
gb.DrawImage(image, 0, 0); //绘制画层
pictureBox1.BackgroundImage = BeiJing; //设置背景层
pictureBox1.CreateGraphics().DrawImage(HuaBu, 0, 0);
}
这是我画图的代码,然后你说的“怎么绘图“是下面这些吧?//初始化画板 Bitmap image = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height);
//获取背景层
Bitmap BeiJing = (Bitmap)pictureBox1.BackgroundImage; //初始化画布
Bitmap HuaBu = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height); //初始化图形
Graphics g = Graphics.FromImage(image);
Graphics gb = Graphics.FromImage(HuaBu);
然后其它的就直接放入OnPaint里面?
我写了个简单的例子,窗体的autoscroll不知道是不是你说的滚动条,可以设置为true,拖一个图片框和按钮。熟悉vb就用vb写了,没几行代码,应该看得懂。
Public Class Form1 Dim image As Bitmap Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load image = New Bitmap(PictureBox1.ClientSize.Width, PictureBox1.ClientSize.Height) End Sub Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Using g = Graphics.FromImage(image) g.DrawEllipse(Pens.Red, 10, 10, 200, 200) End Using PictureBox1.Invalidate() End Sub Private Sub PictureBox1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint e.Graphics.DrawImage(image, 0, 0) End SubEnd Class
然后我看网上都是在picturebox_paint中画图,然后再OnPaint中再画一遍,但是我发现OnPaint用了跟没用一样诶
我那个代码会不断创建新窗口?都没有new 窗体的语句,怎么会创建新窗口?你是怎么操作的?picturebox_paint和OnPaint是一回事,paint是事件,OnPaint是受保护方法,继承picturebox才可以重写OnPaint,不知道你的“网上都是在picturebox_paint中画图,然后再OnPaint中再画一遍”是什么概念。
我在paint里面放的是
//初始化画板 image = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height);
//获取背景层
BeiJing = (Bitmap)pictureBox1.BackgroundImage; //初始化画布
HuaBu = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height); //初始化图形
Graphics g = Graphics.FromImage(image);
Graphics gb = Graphics.FromImage(HuaBu);
后面的就是画线啊什么的
然后OnPaint中放入
e.Graphics.DrawImage(image,0,0);
e.Graphics.DrawImage(HuaBu, 0, 0);
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
Bitmap image;
Random rand= new Random(); public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
image = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height);
} private void button1_Click(object sender, EventArgs e)
{
using (Graphics g = Graphics.FromImage(image))
{
g.DrawEllipse(Pens.Red, rand.Next(200), rand.Next(200), rand.Next(200), rand.Next(200));
} pictureBox1.Invalidate();
} private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(image, 0, 0);
}
}
}
==========paint是什么?是一个只执行一次的方法吗?还是不停的执行?只执行一次的话,应该不会出现滚动就没有 的事情呀
不能在按钮中加pictureBox1.Invalidate()吧,这样会把图刷掉的,然后我在Paint里面读取了较大的数据量进行画图操作,是因为数据量过大而无法自动重绘吗?