由于vb.net转换问题引用c#assembuly [英] Referencing c# assembuly due to vb.net conversion problems

查看:78
本文介绍了由于vb.net转换问题引用c#assembuly的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将代码从c#转换为vb.net,指针和不安全的代码出现了问题,由于我需要使用它们,所以我已经阅读了关于创建一个装配并在vb.net应用程序中引用它的内容。我到目前为止,但我不知道如何去做。指针(字节)问题



公共函数识别(图像为UnmanagedImage,矩形为矩形,ByRef置信为单)作为字节(,)

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(,)= ñ ew 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作为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

当x< cellScanX

cellIntensity(gi,gj)+ = src.Target

x + = 1

src + = 1

结束时

下一步

srcLine + = stride

下一页

下一页



'计算每个字形单元格的值并设置

'字形'c> 0.5F),1,0))

如果conf<信心然后

置信度= conf

结束如果

下一步

下一页

返回glyphValues

结束函数



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



公共接口interfaceCTCAM

{

int识别(UnmanagedImage图像,Rectangle rect,out float confidence);



}

公共类Class1

{



public byte [,]识别(UnmanagedImage图像,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];

不安全

{

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;

} < br $>
}

}



//计算每个字形单元格的值并设置

//字形对细胞信心最小值的信心

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)((丰满度> 0.5f)?1:0);

if(conf<信心)

置信度= conf;

}

}

返回glyphValues;

}



}





一旦我完成了我删除了从vb代码识别函数并导入创建的库Imports CTCAM.Class1。



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



Dim glyphValues As Byte(,)=识别(glyphImage,New Rectangle(0,0,glyphImage.Width,glyphImage.Height),置信度)



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



任何帮助都会很棒。



非常感谢,

Pete

After converting code from c# to vb.net, pointers and unsafe code there's a problem, and due to the application i need to use them, so i've read about creating an assembuly and referencing it in a vb.net application. i've got so far but im not sure how to go about it now. Pointer (Of Byte) Problem

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' c> 0.5F), 1, 0))
If conf < confidence Then
confidence = conf
End If
Next
Next
Return glyphValues
End Function

So i created a c# project and placed the code original c# part of the code within it namespace 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;
}

}


Once i done that i deleted the "recognized" function from the vb code and imported the libary created "Imports CTCAM.Class1".

So in the VB application, Later in my code and error is showing, and this is where im not sure where to go now.

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

The word "Recognize" is highlighted with the following error information: "Reference to a non-shared member requires an object reference."

Any help would be great.

Many Thanks,
Pete

推荐答案

VB中的共享意味着您只需调用该函数而无需拥有该类的实例,这就是您通过调用Recognize来尝试做的...所以,错误是告诉你,因为识别不是共享的,所以你不能这样称呼。



你需要声明一个类的实例识别是第一个。



类似于:



"Shared" in VB means you can just call the function without having to have an instance of the class, which is what you are trying to do by just calling Recognize... So, the error is telling you that since Recognize is NOT shared, so you can't call it that way.

You need to declare an instance of the class Recognize is in first.

Something like:

Dim cls AS New Class1
cls.Recognize...


这篇关于由于vb.net转换问题引用c#assembuly的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆