日期:2014-05-18  浏览次数:20951 次

类传递bitmap问题~在线等待~~
小弟初学C#,用函数写的画图小程序可以运行,现转换成类模式后看不到图。。请教各位~~

C# code

//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.....麻烦指教~~分不够可以继续说嘛~~

------解决方案--------------------
你没有写完吧,我有比较完整的代码你看下
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++)
{