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

将vb.Net项目转换为C#.Net项目,c#,vb.net,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; us

我曾尝试将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);
    }

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

已编辑:我已将reference(System.Windows.Forms.dll)添加到项目中,并且我正在使用所有其他引用。

塔克斯 Nurcky在汇编System.Windows.Forms.dll中定义。确保有对该程序集的引用

另外,你可能想要

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

另外,你可能想要

using System.Windows.Forms;

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

要添加引用,请执行以下步骤:

在解决方案资源管理器中,在项目节点上单击鼠标右键,然后单击“添加引用”


在“添加引用”对话框中,选择.NET选项卡并选择System.Windows.Forms,然后单击“确定”。

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

要添加引用,请执行以下步骤:

在解决方案资源管理器中,在项目节点上单击鼠标右键,然后单击“添加引用”


在“添加引用”对话框中,选择.NET选项卡并选择System.Windows.Forms,然后单击“确定”。

列表框和PictureBox控件都位于System.Windows.Forms命名空间中。在代码顶部添加以下语句:

使用System.Windows.Forms


然后添加对
System.Windows.Forms.dll的引用

列表框和PictureBox控件都位于System.Windows.Forms命名空间中。在代码顶部添加以下语句:

使用System.Windows.Forms


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

的引用,只需右键单击出现错误的类并选择“解决”。June Roslyn CTP附带了一个VB到C的转换器,该转换器工作得相当好。有相当多的实用程序可以在两者之间进行词汇和语法转换。以防万一你不知道:vb.net的一个很好用。Habib并用“usingsystem.Windows.Forms;”修复了它。但准确地说,它给了我更多的其他错误。thanx Habibj只需右键单击出现错误的类并选择“解决”。June Roslyn CTP附带了一个VB到C#的转换器,该转换器工作得相当好。有相当多的实用程序可以在两者之间进行词汇和语法转换。以防万一你不知道:vb.net的一个很好用。Habib并用“usingsystem.Windows.Forms;”修复了它。但准确地说,它给了我更多的其他错误。thanx HabibI两个都做了,没用。该项目由主窗体和上面发布的图像实用程序类组成。如果有帮助的话,也许我应该发布这两个vb版本。我两个都做了,但都没用。该项目由主窗体和上面发布的图像实用程序类组成。如果有帮助的话,也许我应该发布这两个vb版本。这是其中之一:“错误12当前上下文中不存在名称‘Strings’”。“如果(Strings.InStr(1,文件名,VTFile[i].fileExt)=Strings.Len(文件名)-Strings.Len(VTFile[i].fileExt)+1)”Strings.InStr感觉像文件名。Contains,Strings.Len感觉像文件名。Length。VB.Net很少使用,所以不确定.StringsInStr和StringsLen是否可以在C#.Net中使用,只要添加“使用Microsoft.VisualBasic;”。所以不是这样!Thanx Eric这是其中之一:“错误12当前上下文中不存在名称‘Strings’”。“如果(Strings.InStr(1,文件名,VTFile[i].fileExt)=Strings.Len(文件名)-Strings.Len(VTFile[i].fileExt)+1)”Strings.InStr感觉像文件名。Contains,Strings.Len感觉像文件名。Length。VB.Net很少使用,所以不确定.StringsInStr和StringsLen是否可以在C#.Net中使用,只要添加“使用Microsoft.VisualBasic;”。所以不是这样!Thanx Eric没有帮助…我已经将“Dim myUtil As Util”转换为“Util myUtil=default(Util)”;“但是当我在mainForm.cs中的“if(myUtil.LoadImage(ofdLoadImage.FileName,pbMainImage))”中运行get error时”“使用new关键字创建一个新的对象instace”。我该怎么做呢?我尝试过“Util myUtil=new Util();但是我得到的错误没有帮助…我已经将“Dim myUtil As Util”转换为“Util myUtil=default(Util);“但是当我运行get error”“时,请使用new关键字在mainForm.cs中的”if(myUtil.LoadImage(ofdLoadImage.FileName,pbMainImage))”处创建一个新的对象instace”。我该怎么做?我尝试过“Util myUtil=new Util();”,但出现了错误