类传递bitmap问题~在线等待~~

xinxin1982 2008-07-12 08:24:51
小弟初学C#,用函数写的画图小程序可以运行,现转换成类模式后看不到图。。请教各位~~


//form2中:

Color mycolor = Color.Red;
draw_single_qx ceshi = new draw_single_qx(pictureBox1.Height, pictureBox1.Width, mydata,mycolor);
ceshi.draw_start();

pictureBox1.Image = ceshi.Pic_quxian; //通过测试,可以获取Pic_quxian的宽,高,但是看不到图,前面用函数时候一切正常
//------------------------------------------------------------------------------------------------
//class draw_single_qx中:

Bitmap pic_quxian;
Graphics pic_gpr;

public draw_single_qx(double height,double length,string[,] mydata,Color mycolor)
{
//。。。。省略
}

public Bitmap Pic_quxian
{
get { return pic_quxian; }
set {}
}
public Graphics Pic_grp
{
get { return pic_gpr; }
set { Pic_grp = value; }
}

public void draw_start()
{
pic_quxian = new Bitmap((int)pic_length, (int)pic_height);
pic_gpr = Graphics.FromImage(pic_quxian);

pic_gpr.Clear(Color.Black);

ltx = lex = (float)(pic_length * 0.05);
rtx = rex = (float)(pic_length * 0.95);

lty = rty = (float)(pic_height * 0.05);
ley = rey = (float)(pic_height * 0.95);

//Debug.Print(ltx + "," + lty);

pic_gpr.DrawLine(xuxian_pen, ltx, lty, rtx, rty);
pic_gpr.DrawLine(xuxian_pen, lex, ley, rex, rey);

pic_gpr.DrawLine(shixian_pen, ltx, lty, lex, ley);
pic_gpr.DrawLine(shixian_pen, rtx, rty, rex, rey);
}


是不是少了什么,,为什么form2中看不到图呢。。。bitmap在类中画好,交给Pic_quxian.....麻烦指教~~分不够可以继续说嘛~~
...全文
66 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
xinxin1982 2008-07-12
  • 打赏
  • 举报
回复
不好意思。。。低级错误。。呵呵。。。

public draw_single_qx(double height,double length,string[,] mydata,Color mycolor)
{
//。。。。省略
}

中颜色最后给值,笔刷都没有颜色。。。嘿嘿。。第一个进来的给分吧~~不好意思。。。今天送分好了~~
初吻给了烟 2008-07-12
  • 打赏
  • 举报
回复
你没有写完吧,我有比较完整的代码你看下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing;//导入画图命名空间
using System.Reflection;

namespace MyDrawing
{
public partial class Form1 : Form
{
Pen pe=new Pen(Color.Red);
//全局颜色
Color col=Color.Red;
bool flag = false;//鼠标是否被按下
//确定起始点
Point pot = new Point();
//声明画图对象
Graphics gh =null;
//Stroke对象的数组集合
List<Stroke> strokes = new List<Stroke>();
//点的集合的轨迹的对象,stroke的一个实例
Stroke stroke;
//点的集合的数组列表
List<Point> s_points;

public Form1()
{
InitializeComponent();
//获取panel上的绘图对象
this.gh = this.CreateGraphics();
}

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
this.flag = true;
pot.X = e.X;
pot.Y = e.Y;
s_points = new List<Point>();
s_points.Add(pot);
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (this.flag)
{
//绘直线
gh.DrawLine(pe, pot, new Point(e.X, e.Y));
pot.X = e.X;
pot.Y = e.Y;
s_points.Add(pot);
}
}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
this.flag = false;
stroke = new Stroke();
stroke.C = this.col;
stroke.W = 1.0f;
stroke.Points = s_points;
strokes.Add(stroke);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
for (int i = 0; i < strokes.Count; i++)
{
strokes[i].DrawStroke(gh);
}
}

private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
pe.Width = int.Parse(this.toolStripComboBox1.SelectedItem.ToString());
}

private void Form1_Load(object sender, EventArgs e)
{
int i = 0;
Type t = typeof(Color);
//用循环输出里面的值 只读公开且是静态的属性
foreach (PropertyInfo p in t.GetProperties(BindingFlags.Public | BindingFlags.Static))
{
ToolStripButton bolb = new ToolStripButton();
bolb.Click += new EventHandler(bolb_Click);
bolb.BackColor = Color.FromName(p.Name);
bolb.CheckOnClick = true;
if (i <= 75)
{
this.toolStrip2.Items.Add(bolb);
}
else
{
this.toolStrip3.Items.Add(bolb);
}
i++;
}
}
void bolb_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.toolStrip2.Items.Count; i++)
{
(this.toolStrip2.Items[i] as ToolStripButton).CheckState = CheckState.Unchecked;
}
for (int i = 0; i < this.toolStrip3.Items.Count; i++)
{
(this.toolStrip3.Items[i] as ToolStripButton).CheckState = CheckState.Unchecked;
}
ToolStripButton tb = (sender as ToolStripButton);
tb.CheckState = CheckState.Checked;
this.col = tb.BackColor;
float wht = pe.Width;
pe = new Pen(tb.BackColor);
pe.Width = wht;
}
}
class Stroke
{
//颜色
Color c;

public Color C
{
get { return c; }
set { c = value; }
}
float w;
//宽度
public float W
{
get { return w; }
set { w = value; }
}
List<Point> points;
//点的集合
public List<Point> Points
{
get { return points; }
set { points = value; }
}
//定义一个绘轨迹的方法
public void DrawStroke(Graphics g)
{
for(int i = 0;i <points.Count-1;i++)
{
g.DrawLine(new Pen(c, w), points[i], points[i + 1]);
}
}
}
}

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧