Sign in to follow this  
flashinpan

C# Assign Graphics object to PictureBox?

Recommended Posts

flashinpan    100
Is it possible to assign a Graphics object to a PictureBox control in C#?
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace TestAI
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox textBoxWorld;
		private System.Windows.Forms.PictureBox pictureBoxWorld;

		private Robot[,] robo = new Robot[10,10];
		private System.Windows.Forms.Button button1;

		private const int MIN_ROW = 0;
		private const int MAX_ROW = 0;
		private const int MIN_COL = 9;
		private const int MAX_COL = 9;

		private World _new_world = new World();

		private bool donow = false;


		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.textBoxWorld = new System.Windows.Forms.TextBox();
			this.pictureBoxWorld = new System.Windows.Forms.PictureBox();
			this.button1 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// textBoxWorld
			// 
			this.textBoxWorld.Location = new System.Drawing.Point(16, 32);
			this.textBoxWorld.Multiline = true;
			this.textBoxWorld.Name = "textBoxWorld";
			this.textBoxWorld.Size = new System.Drawing.Size(384, 400);
			this.textBoxWorld.TabIndex = 0;
			this.textBoxWorld.Text = "";
			// 
			// pictureBoxWorld
			// 
			this.pictureBoxWorld.Borderstyle = System.Windows.Forms.Borderstyle.FixedSingle;
			this.pictureBoxWorld.Location = new System.Drawing.Point(472, 40);
			this.pictureBoxWorld.Name = "pictureBoxWorld";
			this.pictureBoxWorld.Size = new System.Drawing.Size(408, 392);
			this.pictureBoxWorld.TabIndex = 1;
			this.pictureBoxWorld.TabStop = false;
			this.pictureBoxWorld.Click += new System.EventHandler(this.pictureBoxWorld_Click);
			this.pictureBoxWorld.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBoxWorld_Paint);
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(720, 472);
			this.button1.Name = "button1";
			this.button1.TabIndex = 2;
			this.button1.Text = "button1";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(960, 606);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.pictureBoxWorld);
			this.Controls.Add(this.textBoxWorld);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{			
			Application.Run(new Form1());
		}

		private void pictureBoxWorld_Click(object sender, System.EventArgs e)
		{
		
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{			
//			for(int i=0;i<10;i++)
//			{
//				for(int j=0;j<10;j++)
//				{
//					robo[i,j] = new Robot();
//					robo[i,j].RobotStatus = Image.FromFile("images\\none.bmp");
//					robo[i,j].Controller = "";
//				}
//			}
//			
//			robo[0,0].Controller = "Tom";
//			robo[9,9].Controller = "Bob";
//
//			robo[9,9].RobotStatus = Image.FromFile("images\\attacker.bmp");

			_new_world.CreateRobots();
			donow = true;
		}

		private void pictureBoxWorld_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{
			if(donow == true)
			{
				try
				{
					Bitmap b = new Bitmap(300,300,_new_world.DisplayWorld());
					e.Graphics.DrawImage(b,0,0);
				}
				catch(Exception ee)
				{
					System.Diagnostics.Debug.WriteLine(ee.ToString());
				}
			}			
		}

		private void button1_Click(object sender, System.EventArgs e)
		{			
			this.pictureBoxWorld.Refresh();
		}
	}

	public class World
	{
		private const int _MIN_ROW = 0;
		private const int _MAX_ROW = 9;
		private const int _MIN_COL = 0;
		private const int _MAX_COL = 9;

		public Graphics world_graphics;

		public Image img;

		
		private Robot[,] robo = new Robot[10,10];

		public World()
		{
		}

		public void CreateRobots()
		{
			for(int i=0;i<10;i++)
			{
				for(int j=0;j<10;j++)
				{
					robo[i,j] = new Robot();
					robo[i,j].RobotStatus = Image.FromFile("images\\none.bmp");
					robo[i,j].Name = "";
				}
			}
			
			robo[0,0].Name = "Tom";
			robo[0,0].RobotStatus = Image.FromFile("images\\searcher.bmp");

			robo[9,9].Name = "Bob";
			robo[9,9].RobotStatus = Image.FromFile("images\\searcher.bmp");
		}


		public Graphics DisplayWorld()
		{
			for(int x=0;x<10;x++)
			{
				for(int y=0;y<10;y++)
				{
					world_graphics.DrawImage(robo[x,y].RobotStatus,x*32,y*32);					
				}
			}

			//img = world_graphics.im
			return world_graphics;
		}
		
	}
	public class Robot
	{
		private Image _robotStatus;
		private const string _IMAGE_DIR = "images";
		private const string _DIR_DELIMETER = "\\";
		private const string _FULL_IMAGE_DIR = _IMAGE_DIR + _DIR_DELIMETER;
		private const string _ATTACKER = "attacker.bmp";
		private const string _SEARCHER = "searcher.bmp";
		private const string _NONE = "none.bmp";

		private string _name;

		private int _row;
		private int _col;

		public Image RobotStatus
		{
			get{ return _robotStatus;}
			set{_robotStatus = value;}
		}

		public int Row
		{
			get{ return _row;}
			set{
				_row = value;
			}
		}
		
		public string Name
		{
			get{ return _name;}
			set{_name = value;}
		}
		

		public Robot()
		{
		}

	}

}






public Graphics DisplayWorld() { for(int x=0;x<10;x++) { for(int y=0;y<10;y++) { world_graphics.DrawImage(robo[x,y].RobotStatus,x*32,y*32); } } return world_graphics; } and then in the OnPaint event for the PictureBox: private void pictureBoxWorld_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { if(donow == true) { try { Bitmap b = new Bitmap(300,300,_new_world.DisplayWorld()); e.Graphics.DrawImage(b,0,0); } catch(Exception ee) { System.Diagnostics.Debug.WriteLine(ee.ToString()); } } } [Edited by - Tom Knowlton on December 6, 2005 12:30:19 PM]

Share this post


Link to post
Share on other sites
Xpyder    120
It works the other way from what i think you mean

Create a picture box, assign it an image and then create a graphics object and assign that to the image of the picture box.

Share this post


Link to post
Share on other sites
flashinpan    100
Quote:
Original post by Xpyder
It works the other way from what i think you mean

Create a picture box, assign it an image and then create a graphics object and assign that to the image of the picture box.


Can you provide a quick example, give some sample source code?

Share this post


Link to post
Share on other sites
flashinpan    100
Quote:
Original post by Xpyder
It works the other way from what i think you mean

Create a picture box, assign it an image and then create a graphics object and assign that to the image of the picture box.




It is working now, thanks!!




using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace TestAI
{
/// &lt;summary&gt;
/// Summary description for Form1.
/// &lt;/summary&gt;
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBoxWorld;
private System.Windows.Forms.PictureBox pictureBoxWorld;
private System.Windows.Forms.Button button1;

private World _new_world = new World();

/// &lt;summary&gt;
/// Required designer variable.
/// &lt;/summary&gt;
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// &lt;summary&gt;
/// Clean up any resources being used.
/// &lt;/summary&gt;
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// &lt;summary&gt;
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// &lt;/summary&gt;
private void InitializeComponent()
{
this.textBoxWorld = new System.Windows.Forms.TextBox();
this.pictureBoxWorld = new System.Windows.Forms.PictureBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBoxWorld
//
this.textBoxWorld.Location = new System.Drawing.Point(16, 32);
this.textBoxWorld.Multiline = true;
this.textBoxWorld.Name = "textBoxWorld";
this.textBoxWorld.Size = new System.Drawing.Size(384, 400);
this.textBoxWorld.TabIndex = 0;
this.textBoxWorld.Text = "";
//
// pictureBoxWorld
//
this.pictureBoxWorld.Borderstyle = System.Windows.Forms.Borderstyle.FixedSingle;
this.pictureBoxWorld.Location = new System.Drawing.Point(472, 40);
this.pictureBoxWorld.Name = "pictureBoxWorld";
this.pictureBoxWorld.Size = new System.Drawing.Size(408, 392);
this.pictureBoxWorld.TabIndex = 1;
this.pictureBoxWorld.TabStop = false;
this.pictureBoxWorld.Click += new System.EventHandler(this.pictureBoxWorld_Click);
this.pictureBoxWorld.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBoxWorld_Paint);
//
// button1
//
this.button1.Location = new System.Drawing.Point(720, 472);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(960, 606);
this.Controls.Add(this.button1);
this.Controls.Add(this.pictureBoxWorld);
this.Controls.Add(this.textBoxWorld);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// &lt;summary&gt;
/// The main entry point for the application.
/// &lt;/summary&gt;
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void pictureBoxWorld_Click(object sender, System.EventArgs e)
{

}

private void Form1_Load(object sender, System.EventArgs e)
{
_new_world.CreateRobots();
}

private void pictureBoxWorld_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
try
{
_new_world.DisplayWorld(e.Graphics);
}
catch(Exception ee)
{
System.Diagnostics.Debug.WriteLine(ee.ToString());
}

}

private void button1_Click(object sender, System.EventArgs e)
{
this.pictureBoxWorld.Refresh();
}
}

public class World
{
private const int _MIN_ROW = 0;
private const int _MAX_ROW = 9;
private const int _MIN_COL = 0;
private const int _MAX_COL = 9;

public Graphics world_graphics;

public Image img;


private Robot[,] robo = new Robot[10,10];

public World()
{
}

public void CreateRobots()
{
for(int i=0;i&lt;10;i++)
{
for(int j=0;j&lt;10;j++)
{
robo[i,j] = new Robot();
robo[i,j].RobotStatus = Image.FromFile("images\\none.bmp");
robo[i,j].Name = "";
}
}

robo[0,0].Name = "Tom";
robo[0,0].RobotStatus = Image.FromFile("images\\searcher.bmp");

robo[9,9].Name = "Bob";
robo[9,9].RobotStatus = Image.FromFile("images\\searcher.bmp");
}


public void DisplayWorld(Graphics gr)
{
for(int x=0;x&lt;10;x++)
{
for(int y=0;y&lt;10;y++)
{
gr.DrawImage(robo[x,y].RobotStatus,x*32,y*32);
}
}
}


}
public class Robot
{
private Image _robotStatus;
private const string _IMAGE_DIR = "images";
private const string _DIR_DELIMETER = "\\";
private const string _FULL_IMAGE_DIR = _IMAGE_DIR + _DIR_DELIMETER;
private const string _ATTACKER = "attacker.bmp";
private const string _SEARCHER = "searcher.bmp";
private const string _NONE = "none.bmp";

private string _name;

private int _row;
private int _col;

public Image RobotStatus
{
get{ return _robotStatus;}
set{_robotStatus = value;}
}

public int Row
{
get{ return _row;}
set{
_row = value;
}
}

public string Name
{
get{ return _name;}
set{_name = value;}
}


public Robot()
{
}

}

}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this