2012-07-18 72 views
0

我已經嘗試將vb.net轉換爲C#,但編譯時不斷收到錯誤。我是.NET新手。 這是我轉換的圖像實用程序類的版本。 Util.cs將vb.Net項目轉換爲C#.Net項目

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); 
    } 

} 
} 

我一直得到錯誤,出現此錯誤:

The type or namespace name 'ListBox' could not be found (are you missing a using directive or an assembly reference?).
The type or namespace name 'PictureBox' could not be found (are you missing a using directive or an assembly reference?).

這裏是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

+0

只需右鍵點擊添加上你在哪裏得到的錯誤和選擇的決心類。 – Habib 2012-07-18 05:02:47

+0

June Roslyn CTP附帶了一個VB轉換爲C#的轉換器,效果相當不錯。有相當多的實用程序可以在兩者之間進行詞法和語法轉換。以防萬一你不知道:http://www.developerfusion.com/tools/convert/vb-to-csharp/ – 2012-07-18 05:22:57

+0

vb.net一個正常工作。 Habib和它使用「使用System.Windows.Forms;」修復它。但它確實給了我更多的其他52錯誤。 thanx Habib – Nurcky 2012-07-18 05:56:13

回答

2

ListBox在裝配System.Windows.Forms.dll中被定義。確保你有一個參考該組件。

另外,你可能想

using System.Windows.Forms; 
+0

我已經試過了,但是我的鋼鐵得到了更多的錯誤。 thanx Eric – Nurcky 2012-07-18 05:10:43

+0

你現在得到什麼錯誤? – 2012-07-18 05:47:38

+0

這是其中之一:「錯誤名稱'字符串'在當前上下文中不存在」at。 「if(Strings.InStr(1,fileName,VTFile [i] .fileExt)== Strings.Len(fileName) - Strings.Len(VTFile [i] .fileExt)+ 1)」 – Nurcky 2012-07-18 05:59:16

1

使用System.Windows.Forms命名空間。要使用System.Windows.Forms命名空間,您必須添加System.Windows.Forms.dll作爲參考。

要添加引用,請按照下列步驟:

在Solution Explorer中,在項目節點上單擊鼠標右鍵,單擊添加引用。

在添加引用對話框中,選擇.NET選項卡並選擇System.Windows.Forms,然後單擊確定。

+0

我做了兩個,它沒有幫助。該項目由主要形式和上面張貼的圖像實用程序類組成。也許我應該發佈這兩個版本,如果它會幫助。 – Nurcky 2012-07-18 05:42:09

1

ListBox和PictureBox控件都可以在System.Windows.Forms命名空間中找到。將以下語句添加到您的代碼的頂部:

using System.Windows.Forms;

然後,參照System.Windows.Forms.dll

+0

沒有幫助...我已將「Dim myUtil As Util」轉換爲「Util myUtil = default(Util);」但是當我運行得到錯誤「」使用新的關鍵字來創建一個新的對象實例「在」if(myUtil.LoadImage(ofdLoadImage.FileName,pbMainImage))「在mainForm.cs。」我怎麼做?我已經嘗試「Util myUtil = new Util();」但我得到錯誤 – Nurcky 2012-07-19 10:59:07