由于vb.net转换问题引用c#assembuly

时间:2014-09-24 08:48:01

标签: c# vb.net visual-studio-2010

将代码从c#转换为vb.net后,指针和不安全代码存在问题,并且由于应用程序我需要使用它们,因此我已经阅读了有关创建组合并引用它的信息。一个vb.net应用程序。到目前为止我已经到了,但我现在还不确定如何去做。指针(字节)问题

Public Function Recognize(image As UnmanagedImage, rect As Rectangle, ByRef confidence As Single) As Byte(,)
        Dim glyphStartX As Integer = rect.Left
        Dim glyphStartY As Integer = rect.Top
        Dim glyphWidth As Integer = rect.Width
        Dim glyphHeight As Integer = rect.Height
        Dim cellWidth As Integer = glyphWidth \ glyphSize
        Dim cellHeight As Integer = glyphHeight \ glyphSize
        Dim cellOffsetX As Integer = CInt(cellWidth * 0.2)
        Dim cellOffsetY As Integer = CInt(cellHeight * 0.2)
        Dim cellScanX As Integer = CInt(cellWidth * 0.6)
        Dim cellScanY As Integer = CInt(cellHeight * 0.6)
        Dim cellScanArea As Integer = cellScanX * cellScanY
        Dim cellIntensity As Integer(,) = New Integer(glyphSize - 1, glyphSize - 1) {}
        Dim stride As Integer = image.Stride
        Dim srcBase As Pointer(Of Byte) = CType(image.ImageData.ToPointer(), Pointer(Of Byte)) + (glyphStartY + cellOffsetY) * stride + glyphStartX + cellOffsetX
        Dim srcLine As Pointer(Of Byte)
        Dim src As Pointer(Of Byte)
        For gi As Integer = 0 To glyphSize - 1
            srcLine = srcBase + cellHeight * gi * stride
            For y As Integer = 0 To cellScanY - 1
                For gj As Integer = 0 To glyphSize - 1
                    src = srcLine + cellWidth * gj
                    Dim x As Integer = 0
                    While x < cellScanX
                        cellIntensity(gi, gj) += src.Target
                        x += 1
                        src += 1
                    End While
                Next
                srcLine += stride
            Next
        Next

        ' calculate value of each glyph's cell and set
        ' glyphs' confidence to minim value of cell's confidence
        Dim glyphValues As Byte(,) = New Byte(glyphSize - 1, glyphSize - 1) {}
        confidence = 1.0F
        For gi As Integer = 0 To glyphSize - 1
            For gj As Integer = 0 To glyphSize - 1
                Dim fullness As Single = CSng(cellIntensity(gi, gj) / 255) / cellScanArea
                Dim conf As Single = CSng(System.Math.Abs(fullness - 0.5)) + 0.5F
                glyphValues(gi, gj) = CByte(If((fullness > 0.5F), 1, 0))
                If conf < confidence Then
                    confidence = conf
                End If
            Next
        Next
        Return glyphValues
    End Function

所以我创建了一个c#项目并将代码原始c#部分放在其命名空间CTCAM中:

public interface interfaceCTCAM
{
int Recognize(UnmanagedImage image, Rectangle rect, out float confidence);

}
public class Class1
{

public byte[,] Recognize(UnmanagedImage image, Rectangle rect, out float confidence)
{

int glyphSize = 5;
int glyphStartX = rect.Left;
int glyphStartY = rect.Top;
int glyphWidth = rect.Width;
int glyphHeight = rect.Height;
int cellWidth = glyphWidth / glyphSize;
int cellHeight = glyphHeight / glyphSize;
int cellOffsetX = (int)(cellWidth * 0.2);
int cellOffsetY = (int)(cellHeight * 0.2);
int cellScanX = (int)(cellWidth * 0.6);
int cellScanY = (int)(cellHeight * 0.6);
int cellScanArea = cellScanX * cellScanY;
int[,] cellIntensity = new int[glyphSize, glyphSize];
unsafe
{
int stride = image.Stride;
byte* srcBase = (byte*)image.ImageData.ToPointer() +
(glyphStartY + cellOffsetY) * stride +
glyphStartX + cellOffsetX;
byte* srcLine;
byte* src;
for (int gi = 0; gi < glyphSize; gi++)
{
srcLine = srcBase + cellHeight * gi * stride;
for (int y = 0; y < cellScanY; y++)
{
for (int gj = 0; gj < glyphSize; gj++)
{
src = srcLine + cellWidth * gj;
for (int x = 0; x < cellScanX; x++, src++)
{
cellIntensity[gi, gj] += *src;
}
}
srcLine += stride;
}
}
}

// calculate value of each glyph's cell and set
// glyphs' confidence to minim value of cell's confidence
byte[,] glyphValues = new byte[glyphSize, glyphSize];
confidence = 1f;
for (int gi = 0; gi < glyphSize; gi++)
{
for (int gj = 0; gj < glyphSize; gj++)
{
float fullness = (float)
(cellIntensity[gi, gj] / 255) / cellScanArea;
float conf = (float)System.Math.Abs(fullness - 0.5) + 0.5f;
glyphValues[gi, gj] = (byte)((fullness > 0.5f) ? 1 : 0);
if (conf < confidence)
confidence = conf;
}
}
return glyphValues;
}

}

一旦我这样做,我删除了&#34;认可的&#34;函数来自vb代码并导入了创建的libary&#34; Imports CTCAM.Class1&#34;。

所以在VB应用程序中,稍后我的代码和错误显示,这就是我不知道现在要去哪里。

Dim glyphValues As Byte(,) = Recognize(glyphImage, New Rectangle(0, 0, glyphImage.Width, glyphImage.Height), confidence)

单词&#34;识别&#34;突出显示以下错误信息:&#34;对非共享成员的引用需要对象引用。&#34;

任何帮助都会很棒。

非常感谢, 皮特

1 个答案:

答案 0 :(得分:1)

你需要创建一个'Class1'的实例

Dim class1instance As New Class1()
Dim glyphValues As Byte(,) = class1instance.Recognize(glyphImage, New Rectangle(0, 0, glyphImage.Width, glyphImage.Height), confidence)

另一种选择是使功能共享。