0

我一直在测试并寻求帮助,但无法解决此问题:

在 C# 和 emgu 中,我无法运行这个最简单的代码:

... //**after these 3 dots HERE I GOT THE FIRST WARNING :a namespace cannot directly contain members such as fields or methods**

//Create an image of 400x200 of Blue color
using (Image<Bgr, Byte> img = new Image<Bgr, byte>(400, 200, new Bgr(255, 0, 0))) 
{
   //Create the font
   MCvFont f = new MCvFont(CvEnum.FONT.CV_FONT_HERSHEY_COMPLEX, 1.0, 1.0);

   //Draw "Hello, world." on the image using the specific font
   img.Draw("Hello, world", ref f, new Point(10, 80), new Bgr(0, 255, 0)); 

   //Show the image using ImageViewer from Emgu.CV.UI
   ImageViewer.Show(img, "Test Window");
}

我收到这些错误:

error CS0116: A namespace cannot directly contain members such as fields or methods
error CS1518: Expected class, delegate, enum, interface, or struct

PS: emgu cv 已正确安装,在 C# 中此代码(更复杂)运行完美:

namespace CameraCapture
{
    public partial class CameraCapture : Form
    {
        //declaring global variables
        private Capture capture;        //takes images from camera as image frames
        private bool captureInProgress;

        public CameraCapture()
        {
            InitializeComponent();
        }
        //------------------------------------------------------------------------------//
        //Process Frame() below is our user defined function in which we will create an EmguCv 
        //type image called ImageFrame. capture a frame from camera and allocate it to our 
        //ImageFrame. then show this image in ourEmguCV imageBox
        //------------------------------------------------------------------------------//
        private void ProcessFrame(object sender, EventArgs arg)
        {
            Image<Bgr, Byte> ImageFrame = capture.QueryFrame();
            CamImageBox.Image = ImageFrame;
        }

        //btnStart_Click() function is the one that handles our "Start!" button' click 
        //event. it creates a new capture object if its not created already. e.g at first time
        //starting. once the capture is created, it checks if the capture is still in progress,
        //if so the
        private void btnStart_Click(object sender, EventArgs e)
        {
            #region if capture is not created, create it now
            if (capture == null)
            {
                try
                {
                    capture = new Capture();
                }
                catch (NullReferenceException excpt)
                {
                    MessageBox.Show(excpt.Message);
                }
            }
            #endregion

            if (capture != null)
            {
                if (captureInProgress)
                {  //if camera is getting frames then stop the capture and set button Text
                    // "Start" for resuming capture
                    btnStart.Text = "Start!"; //
                    Application.Idle -= ProcessFrame;
                }
                else
                {
                    //if camera is NOT getting frames then start the capture and set button
                    // Text to "Stop" for pausing capture
                    btnStart.Text = "Stop";
                    Application.Idle += ProcessFrame;
                }

                captureInProgress = !captureInProgress;
            }
        }

        private void ReleaseData()
        {
            if (capture != null)
                capture.Dispose();
        }

        private void CameraCapture_Load(object sender, EventArgs e)
        {

        }
    }
}

感谢您的帮助!

4

1 回答 1

0

也许您的命名空间名称与某些类或不同的结构相同,因此与命名空间发生冲突

使用 * 注释所有内容,并使用全名限定符(例如 System.Console.WriteLine()...),然后它应该可以正常运行(如果这是一个问题)。

于 2014-04-08T10:46:08.770 回答