New to C#can run first program

  • Thread starter Thread starter Gary Morris
  • Start date Start date
G

Gary Morris

//Show the open files dialog box.
if (ofdSelectPicture.ShowDialog()==DialogResult.OK)
{
//Load the Pitcure into the Picture box

This line:
picShowPicture.Image = Image.FromFile(ofdSelectPicture.FileName);
is stopping the show.

'Image' is a class from System.Drawing. If that namespace is referenced in
your
project, you can try fully-qualifying it such as:
picShowPicture.Image =
System.Drawing.Image.FromFile(ofdSelectPicture.FileName);
Which would have to be done anywhere you need to use the Image class, so the
better alternative would be just to add a 'using System.Drawing' at the
beginning
of the code. If, however, you are not referencing System.Drawing in the
project,
the code will not work at all, so the first step is to reference the
namespace if it is
not already.
 
Dive into the MSDN and find out in which namespace the "Image" class lives.

Add "using <namespace>;" to make sure C# compiler finds it...
Currently it isn't in your code...

- Joris
 
Chris said:
I would appreciate some help with a program I have written from the Sams
teach your self c# book.
I have checked my code several times and even rewritten it.
when I compile (Sharp develop) I get the following error :

<snip>

The Image class is in the System.Drawing namespace, so you need to
either specify System.Drawing.Image.FromFile or put using
System.Drawing; in the using statements.
 
hi,

i think it's this bit that causing the problem

//Load the Pitcure into the Picture box
picShowPicture.Image = Image.FromFile(ofdSelectPicture.FileName);

you need to add a reference to the System.drawing
namespace.(System.Drawing.dll)

HTH
Sam


Chris said:
Hi all
I would appreciate some help with a program I have written from the Sams
teach your self c# book.
I have checked my code several times and even rewritten it.

when I compile (Sharp develop) I get the following error :

------ Build started: Project: Pic view 2 Configuration: Debug ------
Performing main compilation...

c:\Documents and Settings\Chris\My Documents\SharpDevelop Projects\Pic view
2\fclsViewer.cs(106,28): error CS0246: The type or namespace name 'Image'
could not be found (are you missing a using directive or an assembly
reference?)
Build complete -- 1 errors, 0 warnings

The code I have written is below. Any help would be greatly appreciated.

Thanks

Chris

.............................................................................
.............................................................................
.............................................................................
..................
/*
* Created by Sharp.
* User: Chris
* Date: 16/08/2004
* Time: 15:50
*
* To change this template use Tools | Options | Coding | Edit Standard
Headers.
*/
using System;
using System.Windows.Forms;
namespace DefaultNamespace
{
/// <summary>
/// Description of fclsViewer.
/// </summary>
public class fclsViewer : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnSelectPicture;
private System.Windows.Forms.Button btnQuit;
private System.Windows.Forms.PictureBox picShowPicture;
private System.Windows.Forms.OpenFileDialog ofdSelectPicture;
public fclsViewer()
{
//
// The InitializeComponent() call is required for Windows Forms designer
support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
[STAThread]
public static void Main(string[] args)
{
Application.Run(new fclsViewer());
}
#region Windows Forms Designer generated code
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The
Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent() {
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(fclsViewer));
this.ofdSelectPicture = new System.Windows.Forms.OpenFileDialog();
this.picShowPicture = new System.Windows.Forms.PictureBox();
this.btnQuit = new System.Windows.Forms.Button();
this.btnSelectPicture = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// ofdSelectPicture
//
this.ofdSelectPicture.Filter = "Windows Bitmaps|*.Bmp|JPEG Files|*.jpg";
this.ofdSelectPicture.Title = "Select Picture";
//
// picShowPicture
//
this.picShowPicture.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.picShowPicture.Location = new System.Drawing.Point(8, 8);
this.picShowPicture.Name = "picShowPicture";
this.picShowPicture.Size = new System.Drawing.Size(282, 275);
this.picShowPicture.TabIndex = 2;
this.picShowPicture.TabStop = false;
//
// btnQuit
//
this.btnQuit.Location = new System.Drawing.Point(301, 40);
this.btnQuit.Name = "btnQuit";
this.btnQuit.Size = new System.Drawing.Size(85, 23);
this.btnQuit.TabIndex = 1;
this.btnQuit.Text = "Quit";
this.btnQuit.Click += new System.EventHandler(this.BtnQuitClick);
//
// btnSelectPicture
//
this.btnSelectPicture.Location = new System.Drawing.Point(301, 10);
this.btnSelectPicture.Name = "btnSelectPicture";
this.btnSelectPicture.Size = new System.Drawing.Size(85, 23);
this.btnSelectPicture.TabIndex = 0;
this.btnSelectPicture.Text = "Select Picture";
this.btnSelectPicture.Click += new
System.EventHandler(this.BtnSelectPictureClick);
//
// fclsViewer
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(392, 291);
this.Controls.Add(this.picShowPicture);
this.Controls.Add(this.btnQuit);
this.Controls.Add(this.btnSelectPicture);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "fclsViewer";
this.Text = "Picture Viewer";
this.ResumeLayout(false);
}
#endregion
void BtnSelectPictureClick(object sender, System.EventArgs e)
{
//Show the open files dialog box.
if (ofdSelectPicture.ShowDialog()==DialogResult.OK)
{
//Load the Pitcure into the Picture box
picShowPicture.Image = Image.FromFile(ofdSelectPicture.FileName);
this.Text=String.Concat("picture Viewer ("+ ofdSelectPicture.FileName
+")"); }
}
void BtnQuitClick(object sender, System.EventArgs e)
{
this.Close();
}
}
}
 
Hi all
I would appreciate some help with a program I have written from the Sams
teach your self c# book.
I have checked my code several times and even rewritten it.

when I compile (Sharp develop) I get the following error :

------ Build started: Project: Pic view 2 Configuration: Debug ------
Performing main compilation...

c:\Documents and Settings\Chris\My Documents\SharpDevelop Projects\Pic view
2\fclsViewer.cs(106,28): error CS0246: The type or namespace name 'Image'
could not be found (are you missing a using directive or an assembly
reference?)
Build complete -- 1 errors, 0 warnings

The code I have written is below. Any help would be greatly appreciated.

Thanks

Chris

......................................................................................................................................................................................................................................................
/*
* Created by Sharp.
* User: Chris
* Date: 16/08/2004
* Time: 15:50
*
* To change this template use Tools | Options | Coding | Edit Standard
Headers.
*/
using System;
using System.Windows.Forms;
namespace DefaultNamespace
{
/// <summary>
/// Description of fclsViewer.
/// </summary>
public class fclsViewer : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnSelectPicture;
private System.Windows.Forms.Button btnQuit;
private System.Windows.Forms.PictureBox picShowPicture;
private System.Windows.Forms.OpenFileDialog ofdSelectPicture;
public fclsViewer()
{
//
// The InitializeComponent() call is required for Windows Forms designer
support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
[STAThread]
public static void Main(string[] args)
{
Application.Run(new fclsViewer());
}
#region Windows Forms Designer generated code
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The
Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent() {
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(fclsViewer));
this.ofdSelectPicture = new System.Windows.Forms.OpenFileDialog();
this.picShowPicture = new System.Windows.Forms.PictureBox();
this.btnQuit = new System.Windows.Forms.Button();
this.btnSelectPicture = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// ofdSelectPicture
//
this.ofdSelectPicture.Filter = "Windows Bitmaps|*.Bmp|JPEG Files|*.jpg";
this.ofdSelectPicture.Title = "Select Picture";
//
// picShowPicture
//
this.picShowPicture.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.picShowPicture.Location = new System.Drawing.Point(8, 8);
this.picShowPicture.Name = "picShowPicture";
this.picShowPicture.Size = new System.Drawing.Size(282, 275);
this.picShowPicture.TabIndex = 2;
this.picShowPicture.TabStop = false;
//
// btnQuit
//
this.btnQuit.Location = new System.Drawing.Point(301, 40);
this.btnQuit.Name = "btnQuit";
this.btnQuit.Size = new System.Drawing.Size(85, 23);
this.btnQuit.TabIndex = 1;
this.btnQuit.Text = "Quit";
this.btnQuit.Click += new System.EventHandler(this.BtnQuitClick);
//
// btnSelectPicture
//
this.btnSelectPicture.Location = new System.Drawing.Point(301, 10);
this.btnSelectPicture.Name = "btnSelectPicture";
this.btnSelectPicture.Size = new System.Drawing.Size(85, 23);
this.btnSelectPicture.TabIndex = 0;
this.btnSelectPicture.Text = "Select Picture";
this.btnSelectPicture.Click += new
System.EventHandler(this.BtnSelectPictureClick);
//
// fclsViewer
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(392, 291);
this.Controls.Add(this.picShowPicture);
this.Controls.Add(this.btnQuit);
this.Controls.Add(this.btnSelectPicture);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "fclsViewer";
this.Text = "Picture Viewer";
this.ResumeLayout(false);
}
#endregion
void BtnSelectPictureClick(object sender, System.EventArgs e)
{
//Show the open files dialog box.
if (ofdSelectPicture.ShowDialog()==DialogResult.OK)
{
//Load the Pitcure into the Picture box
picShowPicture.Image = Image.FromFile(ofdSelectPicture.FileName);
this.Text=String.Concat("picture Viewer ("+ ofdSelectPicture.FileName
+")"); }
}
void BtnQuitClick(object sender, System.EventArgs e)
{
this.Close();
}
}
}
 
Thanks for the help I needed to add the 'using System.Drawing' line in the
file.
The book doesn't tell you that but when you look at the downloadable source
files its there.
 
Back
Top