将vb.Net项目转换为C#.Net项目

时间:2012-07-18 04:58:41

标签: c# vb.net

我已经尝试将vb.net转换为C#,但在编译时我一直收到错误。我是.NET的新手。 这是我转换后的图像实用程序类的版本。 Util.cs

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using Com.Griaule.IcaoFace;
using System.Windows.Forms;

namespace IcaoWF
{
public class Util
{

    //Set if the mouth is important

    public const bool USES_MOUTH = true;
    //The number of supported pictures

    const int SFile = 3;
    //
    public const int FEATURES_COLOR = (255 << 8);

    public const int FEATURES_SIZE = 8;
    //File Vector with spec ID

    TFile[] VTFile = new TFile[SFile + 1];
    //Pointers to the .NET classes
    public FaceImage GrFaceImage = null;
    public IcaoImage GrIcaoImage = null;
    public CbeffImage GrCbeffImage = null;

    public Cbeff GrCbeff = null;

    ListBox log;

    // raw image data type.
    public struct TRawImage
    {
        // Image data.
        public object img;
        // Image width.
        public int width;
        // Image height.
        public int height;
        //Reduction Factor because stretch
        public float frX;
        public float frY;
        //Eyes an mouth positions
        public int lx;
        public int ly;
        public int rx;
        public int ry;
        public int mx;
        public int my;
    }

    // File Enum Type
    public enum EFile
    {
        BMP = 1,
        JPEG2000 = 2,
        CBEFF = 3,
        NOTDEF = 0
    }

    //File Type
    private struct TFile
    {
        //File Extension
        public string fileExt;
        //File Type
        public EFile fileID;
    }

    //Class constructor

    public Util(ListBox ltBox)
    {
        //Adding Supportted files
        VTFile[1].fileExt = ".bmp";
        VTFile[1].fileID = EFile.BMP;
        VTFile[2].fileExt = ".jp2";
        VTFile[2].fileID = EFile.JPEG2000;
        VTFile[3].fileExt = ".cbeff";
        VTFile[3].fileID = EFile.CBEFF;

        log = ltBox;
    }

    public void WriteError(GriauleIcaoFaceException err)
    {
        WriteLog("Error: " + err.ToString());
    }

    // Write a message in box.
    public void WriteLog(string message)
    {
        log.Items.Add(message);
        log.SelectedIndex = log.Items.Count - 1;
        log.ClearSelected();
    }

    //Get the ID File Type from file path name
    public EFile GetFileType(string fileName)
    {
        EFile functionReturnValue = default(EFile);
        int i = 0;
        for (i = 0; i <= SFile; i++)
        {
            if (Strings.InStr(1, fileName, VTFile[i].fileExt) == Strings.Len(fileName) - Strings.Len(VTFile[i].fileExt) + 1)
            {
                functionReturnValue = VTFile[i].fileID;
                return functionReturnValue;
            }
        }
        functionReturnValue = EFile.NOTDEF;
        return functionReturnValue;
    }

    //Loading an Image
    public bool LoadImage(string fileName, PictureBox img)
    {

        // create face image from file        
        GrFaceImage = new FaceImage(fileName);

        // display face image
        DisplayFaceImage(img, false);

        WriteLog("Image loaded successfully.");

        return true;
    }

    //Process the raw Image to FaceImage Type and paint the points on pBox
    public bool ProcessFaceImage(PictureBox pBox)
    {

        //Set mouth to be relevant to generate the ICAO
        GrFaceImage.MouthDetectionEnabled = USES_MOUTH;

        WriteLog("Finding the eyes and mouth positions. Please, wait...");

        //Get the positions from mouth and eyes
        if (GetPositionsFromFaceImage())
        {
            WriteLog("Eyes and mouth founded. Drawing their positions on the image.");
            //Display Face Image with eyes and mouth drawn
            DisplayFaceImage(pBox, true);
            return true;
        }
        else
        {
            //Display Face Image
            DisplayFaceImage(pBox, false);
            return false;
        }
    }

    //Display the ICAO Image

    public void DisplayIcaoImg(PictureBox imgIcao)
    {
        if (GrFaceImage.LeftEye.X <= 0 | GrFaceImage.LeftEye.Y <= 0 | GrFaceImage.LeftEye.X > GrFaceImage.Width | GrFaceImage.LeftEye.Y > GrFaceImage.Height)
        {
            WriteLog("Left eye is out of bounds.");
            return;
        }

        if (GrFaceImage.RightEye.X <= 0 | GrFaceImage.RightEye.Y <= 0 | GrFaceImage.RightEye.X > GrFaceImage.Width | GrFaceImage.RightEye.Y > GrFaceImage.Height)
        {
            WriteLog("Right eye is out of bounds.");
            return;
        }

        if (GrFaceImage.Mouth.X <= 0 | GrFaceImage.Mouth.Y <= 0 | GrFaceImage.Mouth.X > GrFaceImage.Width | GrFaceImage.Mouth.Y > GrFaceImage.Height)
        {
            WriteLog("Mouth is out of bounds.");
            return;
        }

        //Get the GrIcaoImage
        try
        {
            GrIcaoImage = GrFaceImage.FullFrontalImage(imgIcao.Width, 3.0 / 4.0, IcaoImage.IcaoFullFrontalMode.FullFrontal);
        }
        catch (GriauleIcaoFaceException ex)
        {
            WriteError(ex);
            return;
        }

        //Getting the eyes positons from icao
        if (GetPositionsFromIcaoImage())
        {
            //Displaying the icao image
            DisplayIcaoImage(imgIcao);
        }

        WriteLog("ICAO image generated.");

    }

    //Display Face Image

    public void DisplayFaceImage(PictureBox pBox, bool withFeatures)
    {
        if (withFeatures)
        {
            pBox.Image = GrFaceImage.ImageWithFeatures(8, Color.Green);
        }
        else
        {
            pBox.Image = GrFaceImage.Image;
        }

        pBox.Update();

    }

    //Display Cbeff Image

    public void DisplayCbeffImage(PictureBox pBox)
    {
        pBox.Image = GrCbeffImage.Image;
        pBox.Update();

    }

    //Display Icao Image

    public void DisplayIcaoImage(PictureBox pBox)
    {
        pBox.Image = GrIcaoImage.Image;
        pBox.Update();

    }

    //Save ICAO in CBEFF file format

    public void SaveIcaoIntoCBEFFImage(string fileName)
    {
        // Create a CBEFF from Icao        

        if (GetCbeffFromIcao())
        {
            //Get the CBEFF buffer
            try
            {
                SaveBuffer(fileName, ref GrCbeff.CBEFF);
            }
            catch (GriauleIcaoFaceException ex)
            {
                WriteError(ex);
            }

        }

    }

    //Load an ICAO image from CBEFF file format
    public void LoadIcaoFromCBEFFImage(string fileName, PictureBox pBox)
    {
        //Creating the cbeff from the buffer
        try
        {
            GrCbeff = new Cbeff(LoadBuffer(fileName));
            GrCbeffImage = GrCbeff.Image(0);
        }
        catch (GriauleIcaoFaceException ex)
        {
            WriteError(ex);
        }

        // Display icao image        
        DisplayCbeffImage(pBox);

    }

    //Save ICAO image in JPEG2000 file format
    public void SaveIcaoIntoJP2Image(string fileName)
    {
        // Create a CBEFF from Icao
        if (!GetCbeffFromIcao())
        {
            return;
        }

        //Get Jpeg2000 buffer from CBEFF and save it in a file        
        SaveBuffer(fileName, ref GrCbeffImage.BufferJPEG);
    }

    //Save Byte Buffer into a file
    private void SaveBuffer(string fileName, ref byte[] buffer)
    {
        System.IO.FileStream oFileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write);
        System.IO.BinaryWriter swb = new System.IO.BinaryWriter(oFileStream);
        swb.Write(buffer);
        swb.Close();
    }

    //Load stream from file
    private byte[] LoadBuffer(string fileName)
    {
        // Open a file that is to be loaded into a byte array
        FileInfo oFile = null;
        oFile = new FileInfo(fileName);

        System.IO.FileStream oFileStream = oFile.OpenRead();
        long lBytes = oFileStream.Length;
        byte[] fileData = new byte[lBytes + 1];

        // Read the file into a byte array
        oFileStream.Read(fileData, 0, lBytes);
        oFileStream.Close();

        return fileData;
    }

    //Get CBEFF image from an Icao image
    private bool GetCbeffFromIcao()
    {
        //Create Cbeff Image Data pointer
        GrCbeff = new Cbeff();
        GrCbeffImage = GrCbeff.AddImage(GrIcaoImage, false, 0);

        GrCbeffImage.Gender = CbeffImage.CbeffGender.Unknown;
        GrCbeffImage.Eyes = CbeffImage.CbeffEyes.Unspecified;
        GrCbeffImage.Hair = CbeffImage.CbeffHair.Unspecified;
        GrCbeffImage.FeatureMask = 0;
        GrCbeffImage.Expression = CbeffImage.CbeffExpression.Unspecified;

        return true;
    }

    //Get eyes and mouth position from Face Image
    public bool GetPositionsFromFaceImage()
    {
        float prob = 0;
        //Get the eyes detection probabilty        
        prob = GrFaceImage.DetectionProbability;
        if (prob == 0)
        {
            Interaction.MsgBox("There isn't any probability to find the eyes position.", Constants.vbCritical, "No probability");
            return false;
        }
        return true;
    }

    //Get eyes and mouth position from ICAO Image
    public bool GetPositionsFromIcaoImage()
    {
        //get the position from an icao image.
        float prob = 0;
        prob = GrIcaoImage.DetectionProbability;
        if (prob <= 0)
        {
            WriteLog("There isn't any probability to find the eyes position.");
            return false;
        }
        return true;
    }

    //Set left eye position on library
    public void SetLeftEyePos(int x, int y)
    {
        GrFaceImage.LeftEye = new Point(x, y);
    }

    //Set right eye position on library
    public void SetRightEyePos(int x, int y)
    {
        GrFaceImage.RightEye = new Point(x, y);
    }

    //Set mouth position on library
    public void SetMouthPos(int x, int y)
    {
        if (x > 0 & x < GrFaceImage.Width & y > 0 & y < GrFaceImage.Height)
        {
            Point p = new Point(x, y);
            GrFaceImage.Mouth = p;
        }
    }

    //Marshal between library and VB .NET. Copy an Variant Array to Byte() vector
    public byte[] ConvertArrayToVByte(Array buffer)
    {
        GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
        IntPtr ptr = handle.AddrOfPinnedObject();
        byte[] bytes = new byte[buffer.Length + 1];
        Marshal.Copy(ptr, bytes, 0, bytes.Length);
        return bytes;
    }

    // Show GriauleAfis version and type
    public void MessageVersion()
    {
        int majorVersion = 0;
        int minorVersion = 0;

        GriauleIcaoFace.GetVersion(majorVersion, minorVersion);
        MessageBox.Show("The GrIcaoFace DLL version is " + majorVersion + "." + minorVersion + ".", "GrIcaoFace Version", MessageBoxButtons.OK);
    }

}
}

我一直收到错误:

  

无法找到类型或命名空间名称“ListBox”(是   你错过了使用指令或汇编引用吗?)   找不到类型或命名空间名称'PictureBox'(是吗?   缺少using指令或程序集引用?)。

这是formMain.cs

using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace IcaoWF
{
public partial class formMain : Form
{
    public formMain(): base()
    {
        Load += formMain_Load;
        InitializeComponent();
    }

    // raw image data type.
    private struct TSetting
    {
        // Image data.
        public Button button;
        // Image width.
        public Label x;
        public Label y;
        public bool setting;
    }
    TSetting CSetting = new TSetting();

    Util myUtil = default(Util);
    private void formMain_Load(Object sender, EventArgs e)
    {
        InitializeInterface();
        //Setting file filters
        ldImg.Filter = "JPEG Images (*.jpg,*.jpeg)|*.jpg;*.jpeg|Gif Images (*.gif)|*.gif|Bitmaps (*.bmp)|*.bmp";
        ldIcaoImg.Filter = "CBEFF (*.cbeff)|*.cbeff";
        svIcaoImg.Filter = "JPEG2000 (*.jp2)|*.jp2|CBEFF (*.cbeff)|*.cbeff";
        myUtil = new Util(logBox);

        //Verifieing if the mouth is important
        gbMouth.Enabled = myUtil.USES_MOUTH;
    }

    //Unlock the interface and update the clicked iten
    private void interfaceSetStop(int x, int y)
    {
        if (CSetting.setting) {
            //Set the CSetting to false
            CSetting.setting = false;
            //Set positions from mouse to CSetting text selected
            CSetting.x.Text = x.ToString();
            CSetting.y.Text = y.ToString();
            //Enable Set button again
            CSetting.button.Enabled = true;
            //Set the normal cursor above image
            imgFace.Cursor = Cursors.Arrow;
            //Enable all butons, disabled before
            EnableButtons();

            //Sets new position from image
            myUtil.SetLeftEyePos(lbLeftEyeXPos.Text, lbLeftEyeYPos.Text);
            myUtil.SetRightEyePos(lbRightEyeXPos.Text, lbRightEyeYPos.Text);
            if (myUtil.USES_MOUTH) {
                myUtil.SetMouthPos(lbMouthXPos.Text, lbMouthYPos.Text);
            }

            //Redraw img
            myUtil.DisplayFaceImage(imgFace, true);
        }
    }

    //Initialize the program interface
    private void InitializeInterface()
    {
        //Disable butons
        DisableButtons();
        //Disbable image picture box
        imgFace.Enabled = false;
        //Current setting eye or mouth to false
        CSetting.setting = false;
        //Disable Save ICAO image
        mnFileSaveIcaoImg.Enabled = false;
        //Reset the logBox 
        logBox.ResetText();
    }

    //Enable all butons from interface
    private void EnableButtons()
    {
        btGenIcaoImage.Enabled = true;
        btLeftEyeSet.Enabled = true;
        btMouthSet.Enabled = true;
        btRightEyeSet.Enabled = true;
        btProcess.Enabled = true;
        imgFace.Enabled = true;
    }

    //Set the inteface to click on the image
    private void btLeftEyeSet_Click(Object sender, EventArgs e)
    {
        interfaceSetStart(btLeftEyeSet, lbLeftEyeXPos, lbLeftEyeYPos);
    }

    //Set the inteface to click on the image
    private void lbRightEyeSet_Click(Object sender, EventArgs e)
    {
        interfaceSetStart(btRightEyeSet, lbRightEyeXPos, lbRightEyeYPos);
    }

    //Set the inteface to click on the image
    private void btMouthSet_Click(Object sender, EventArgs e)
    {
        interfaceSetStart(btMouthSet, lbMouthXPos, lbMouthYPos);
    }

    //Lock the interface to click on image
    private void interfaceSetStart(Button button, Label x, Label y)
    {
        //Set the clicked button set
        CSetting.button = button;
        //set the label to update the position
        CSetting.x = x;
        CSetting.y = y;
        //Enable set mode
        CSetting.setting = true;
        //Disable the button
        button.Enabled = false;
        //Enable Cross cursor on image
        imgFace.Cursor = Cursors.Cross;
        //Disable button to avoid user to click in another area
        DisableButtons();
    }

    //Disable all buttons from interface
    private void DisableButtons()
    {
        btGenIcaoImage.Enabled = false;
        btLeftEyeSet.Enabled = false;
        btMouthSet.Enabled = false;
        btRightEyeSet.Enabled = false;
        btProcess.Enabled = false;
    }


    //On click on the image, stop the interface and set the right position
    private void imgFace_MouseDown(object sender, MouseEventArgs e)
    {
        interfaceSetStop(e.X / (imgFace.Width / myUtil.GrFaceImage.Width), e.Y / (imgFace.Height / myUtil.GrFaceImage.Height));
    }

    //Gen the ICAO image from FaceImage
    private void btGenIcaoImage_Click(Object sender, EventArgs e)
    {
        //Display ICAO image captured
        myUtil.DisplayIcaoImg(imgIcaoImg);
        //Enabled 
        mnFileSaveIcaoImg.Enabled = true;
    }

    //Load Icao IMAGE From CBEFF or JPEG2000
    private void mnFileLoadIcaoImg_Click(Object sender, EventArgs e)
    {
        Util.EFile fileType = default(Util.EFile);
        ldIcaoImg.FileName = "";

        //save the ICAO image
        if (ldIcaoImg.ShowDialog == DialogResult.OK & !string.IsNullOrEmpty(ldIcaoImg.FileName))
        {
            fileType = myUtil.GetFileType(ldIcaoImg.FileName);
            switch (fileType)
            {

                case Util.EFile.CBEFF:
                    //Save CBEFF image
                    myUtil.LoadIcaoFromCBEFFImage(ldIcaoImg.FileName, imgIcaoImg);
                    break;
                //
                default:
                    //Image type not found
                    myUtil.WriteLog("File type not supported.");
                    return;
            }
        }
    }

    //Save ICAO Image
    private void mnFileSaveIcaoImg_Click(Object sender, EventArgs e)
    {
        Util.EFile fileType = default(Util.EFile);
        svIcaoImg.FileName = "";

        //save the ICAO image
        if (svIcaoImg.ShowDialog == DialogResult.OK & !string.IsNullOrEmpty(svIcaoImg.FileName))
        {
            fileType = myUtil.GetFileType(svIcaoImg.FileName);
            switch (fileType)
            {

                case Util.EFile.CBEFF:
                    //Save CBEFF image
                    myUtil.SaveIcaoIntoCBEFFImage(svIcaoImg.FileName);

                    break;
                case Util.EFile.JPEG2000:
                    //Save JPEG200 image
                    myUtil.SaveIcaoIntoJP2Image(svIcaoImg.FileName);

                    break;
                default:
                    //Image type not found
                    myUtil.WriteLog("File type not supported.");
                    break;
            }
        }
    }

    //Load Image

    private void mnFileLoadImg_Click(Object sender, EventArgs e)
    {
        lbLeftEyeXPos.Text = "0";
        lbLeftEyeYPos.Text = "0";
        lbRightEyeXPos.Text = "0";
        lbRightEyeYPos.Text = "0";
        lbMouthXPos.Text = "0";
        lbMouthYPos.Text = "0";

        //Disable buttons
        DisableButtons();

        //Enable image
        imgFace.Enabled = true;
        //Set file name image to null
        ldImg.FileName = "";
        if (ldImg.ShowDialog == DialogResult.OK & !string.IsNullOrEmpty(ldImg.FileName))
        {
            //load image from FileName into imgFace Picture Box

            if (myUtil.LoadImage(ldImg.FileName, imgFace))
            {
                //Set the icaoImage to null
                imgIcaoImg.Image = null;
                imgIcaoImg.Refresh();

                //Disble mnFileSaveIcaoImg to save
                mnFileSaveIcaoImg.Enabled = false;

                //Disable buttons
                DisableButtons();

                //Enable find eyes and mouth button
                btProcess.Enabled = true;
            }
        }
    }

    //Close the program
    private void MenuItem5_Click(Object sender, EventArgs e)
    {
        this.Close();
    }

    //Process the Face Image
    private void btProcess_Click(Object sender, EventArgs e)
    {
        //Enable buttons to set eyes and mouth
        EnableButtons();

        //Process face image

        if (myUtil.ProcessFaceImage(imgFace))
        {
            //Get positions from face image
            lbLeftEyeXPos.Text = myUtil.GrFaceImage.LeftEye.X.ToString();
            lbLeftEyeYPos.Text = myUtil.GrFaceImage.LeftEye.Y.ToString();
            lbRightEyeXPos.Text = myUtil.GrFaceImage.RightEye.X.ToString();
            lbRightEyeYPos.Text = myUtil.GrFaceImage.RightEye.Y.ToString();
            lbMouthXPos.Text = myUtil.GrFaceImage.Mouth.X.ToString();
            lbMouthYPos.Text = myUtil.GrFaceImage.Mouth.Y.ToString();

        }

    }

    //Print the DLL version
    private void mnVersion_Click(Object sender, EventArgs e)
    {
        myUtil.MessageVersion();
    }


}
}

如果需要,我可以发布vb.net版本。

已编辑:我已将refference(System.Windows.Forms.dll)添加到项目中,而其他所有人都在使用。

感谢名单 Nurcky

3 个答案:

答案 0 :(得分:2)

ListBox在程序集System.Windows.Forms.dll中定义。确保您有对该程序集的引用。

另外你可能想要

using System.Windows.Forms;

答案 1 :(得分:1)

使用System.Windows.Forms命名空间。要使用System.Windows.Forms命名空间,您必须添加System.Windows.Forms.dll作为参考。

要添加参考,请按照以下步骤操作:

在Solution Explorer中,右键单击项目节点,然后单击Add Reference。

在Add Reference对话框中,选择.NET选项卡并选择System.Windows.Forms并单击OK。

答案 2 :(得分:1)

ListBox和PictureBox控件都可以在System.Windows.Forms命名空间中找到。将以下语句添加到代码顶部:

  

使用System.Windows.Forms;

然后添加对System.Windows.Forms.dll

的引用