|
-
Jun 7th, 2008, 02:31 PM
#1
Thread Starter
Addicted Member
Help converting C# class to VB.NET
I'm wasn't sure if I should have put this in C# or the VB.NET--but anyway, I decided either way would work.
I have this class--not my own code--but I want to make use of it. And since I don't have C# and also don't know it (would learn it if I had it) I need to convert it. Would anyone please help me convert it? I know some of it will convert without really even needing too much knowledge of C# but some of it I'm not sure what the equivalents are.
Help would be greatly appreciated.
Thanks
(I'm sure this is all I need, I actually have the whole project but can open it- But if more information is needed I can provide it--just ask.)
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace UPCA
{
class cUPCA
{
private Bitmap newBitmap;
private Graphics g;
private int barCodeHeight = 80;
private int placeMarker = 0;
private int imageWidth = 0;
private float imageScale = 1;
private string UPCABegin = "0000000000000101";
private string[] UPCALeft = { "0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011" };
private string UPCAMiddle = "01010";
private string[] UPCARight = { "1110010", "1100110", "1101100", "1000010", "1011100", "1001110", "1010000", "1000100", "1001000", "1110100" };
private string UPCAEnd = "1010000000000000";
public Image CreateBarCode(string txt, int scale)
{
Image img = null;
imageWidth = 120;
imageScale = scale;
imageWidth = System.Convert.ToInt32(imageWidth * imageScale);
int imageHeightHolder = System.Convert.ToInt32(barCodeHeight * imageScale);
string incomingString = txt.ToUpper();
if(incomingString.Length == 0)
{
return img;
}
int numberOfChars = incomingString.Length;
newBitmap = new Bitmap((imageWidth), imageHeightHolder, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
g = Graphics.FromImage(newBitmap);
g.ScaleTransform(imageScale, imageScale);
Rectangle newRec = new Rectangle(0, 0, (imageWidth), imageHeightHolder);
g.FillRectangle(new SolidBrush(Color.White), newRec);
placeMarker = 0;
txt = txt.Substring(0, 11) + GetCheckSum(txt).ToString();
int wholeSet = 0;
for(wholeSet = 1; wholeSet <= System.Convert.ToInt32(incomingString.Length); wholeSet++)
{
int currentASCII = (int)Convert.ToChar((incomingString.Substring(wholeSet - 1, 1))) - 48;
string currentLetter = "";
if(wholeSet == 1)
{
DrawSet(UPCABegin, placeMarker, 0);
DrawSet(UPCALeft[currentASCII], placeMarker, 0);
}
else if(wholeSet <= 5)
{
DrawSet(UPCALeft[currentASCII], placeMarker, 6);
}
else if(wholeSet == 6)
{
DrawSet(UPCALeft[currentASCII], placeMarker, 6);
DrawSet(UPCAMiddle, placeMarker, 0);
}
else if(wholeSet <= 11)
{
DrawSet(UPCARight[currentASCII], placeMarker, 6);
}
else if(wholeSet == 12)
{
DrawSet(UPCARight[currentASCII], placeMarker, 0);
DrawSet(UPCAEnd, placeMarker, 0);
}
}
System.Drawing.Font font = new System.Drawing.Font("Courier New, Bold", 9);
try
{
g.DrawString(txt.Substring(0, 1), font, Brushes.Black, new System.Drawing.PointF(0, 67));
g.DrawString(txt.Substring(1, 5), font, Brushes.Black, new System.Drawing.PointF(22, 67));
g.DrawString(txt.Substring(6, 5), font, Brushes.Black, new System.Drawing.PointF(60, 67));
g.DrawString(txt.Substring(11, 1), font, Brushes.Black, new System.Drawing.PointF(108, 67));
}
finally
{
font.Dispose();
}
img = Image.FromHbitmap(newBitmap.GetHbitmap());
return img;
}
public int GetCheckSum(string barCode)
{
string leftSideOfBarCode = barCode.Substring(0, 11);
int total = 0;
int currentDigit = 0;
int i = 0;
for( i = 0; i <= leftSideOfBarCode.Length - 1; i++)
{
currentDigit = Convert.ToInt32(leftSideOfBarCode.Substring(i, 1));
if(((i - 1) % 2 == 0))
{
total += currentDigit * 1;
}
else
{
total += currentDigit * 3;
}
}
int iCheckSum = (10 - (total % 10)) % 10;
return iCheckSum;
}
private void DrawSet(string upcCode, int drawLocation, int barHeight)
{
int[] currentLetterArray = new int[upcCode.Length];
placeMarker += upcCode.Length;
barHeight = barCodeHeight - barHeight;
int i = 0;
for(i = 0; i <= upcCode.Length - 1; i++)
{
currentLetterArray[i] = Convert.ToInt16(upcCode.Substring(i, 1));
}
for(i = 0; i <= upcCode.Length - 1; i++)
{
if(currentLetterArray[i] == 0)
{
g.DrawLine(Pens.White, i + (drawLocation), 0, i + (drawLocation), barHeight - 6);
}
else if(currentLetterArray[i] == 1)
{
g.DrawLine(Pens.Black, i + (drawLocation), 0, i + (drawLocation), barHeight - 6);
}
}
}
}
}
-
Jun 7th, 2008, 02:33 PM
#2
Re: Help converting C# class to VB.NET
there are online converters that will do this for you like
http://labs.developerfusion.co.uk/co...arp-to-vb.aspx
and here is the output
Code:
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Drawing
Namespace UPCA
Class cUPCA
Private newBitmap As Bitmap
Private g As Graphics
Private barCodeHeight As Integer = 80
Private placeMarker As Integer = 0
Private imageWidth As Integer = 0
Private imageScale As Single = 1
Private UPCABegin As String = "0000000000000101"
Private UPCALeft As String() = {"0001101", "0011001", "0010011", "0111101", "0100011", "0110001", _
"0101111", "0111011", "0110111", "0001011"}
Private UPCAMiddle As String = "01010"
Private UPCARight As String() = {"1110010", "1100110", "1101100", "1000010", "1011100", "1001110", _
"1010000", "1000100", "1001000", "1110100"}
Private UPCAEnd As String = "1010000000000000"
Public Function CreateBarCode(ByVal txt As String, ByVal scale As Integer) As Image
Dim img As Image = Nothing
imageWidth = 120
imageScale = scale
imageWidth = System.Convert.ToInt32(imageWidth * imageScale)
Dim imageHeightHolder As Integer = System.Convert.ToInt32(barCodeHeight * imageScale)
Dim incomingString As String = txt.ToUpper()
If incomingString.Length = 0 Then
Return img
End If
Dim numberOfChars As Integer = incomingString.Length
newBitmap = New Bitmap((imageWidth), imageHeightHolder, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
g = Graphics.FromImage(newBitmap)
g.ScaleTransform(imageScale, imageScale)
Dim newRec As New Rectangle(0, 0, (imageWidth), imageHeightHolder)
g.FillRectangle(New SolidBrush(Color.White), newRec)
placeMarker = 0
txt = txt.Substring(0, 11) + GetCheckSum(txt).ToString()
Dim wholeSet As Integer = 0
For wholeSet = 1 To System.Convert.ToInt32(incomingString.Length)
Dim currentASCII As Integer = CInt(Convert.ToChar((incomingString.Substring(wholeSet - 1, 1)))) - 48
Dim currentLetter As String = ""
If wholeSet = 1 Then
DrawSet(UPCABegin, placeMarker, 0)
DrawSet(UPCALeft(currentASCII), placeMarker, 0)
ElseIf wholeSet <= 5 Then
DrawSet(UPCALeft(currentASCII), placeMarker, 6)
ElseIf wholeSet = 6 Then
DrawSet(UPCALeft(currentASCII), placeMarker, 6)
DrawSet(UPCAMiddle, placeMarker, 0)
ElseIf wholeSet <= 11 Then
DrawSet(UPCARight(currentASCII), placeMarker, 6)
ElseIf wholeSet = 12 Then
DrawSet(UPCARight(currentASCII), placeMarker, 0)
DrawSet(UPCAEnd, placeMarker, 0)
End If
Next
Dim font As New System.Drawing.Font("Courier New, Bold", 9)
Try
g.DrawString(txt.Substring(0, 1), font, Brushes.Black, New System.Drawing.PointF(0, 67))
g.DrawString(txt.Substring(1, 5), font, Brushes.Black, New System.Drawing.PointF(22, 67))
g.DrawString(txt.Substring(6, 5), font, Brushes.Black, New System.Drawing.PointF(60, 67))
g.DrawString(txt.Substring(11, 1), font, Brushes.Black, New System.Drawing.PointF(108, 67))
Finally
font.Dispose()
End Try
img = Image.FromHbitmap(newBitmap.GetHbitmap())
Return img
End Function
Public Function GetCheckSum(ByVal barCode As String) As Integer
Dim leftSideOfBarCode As String = barCode.Substring(0, 11)
Dim total As Integer = 0
Dim currentDigit As Integer = 0
Dim i As Integer = 0
For i = 0 To leftSideOfBarCode.Length - 1
currentDigit = Convert.ToInt32(leftSideOfBarCode.Substring(i, 1))
If ((i - 1) Mod 2 = 0) Then
total += currentDigit * 1
Else
total += currentDigit * 3
End If
Next
Dim iCheckSum As Integer = (10 - (total Mod 10)) Mod 10
Return iCheckSum
End Function
Private Sub DrawSet(ByVal upcCode As String, ByVal drawLocation As Integer, ByVal barHeight As Integer)
Dim currentLetterArray As Integer() = New Integer(upcCode.Length - 1) {}
placeMarker += upcCode.Length
barHeight = barCodeHeight - barHeight
Dim i As Integer = 0
For i = 0 To upcCode.Length - 1
currentLetterArray(i) = Convert.ToInt16(upcCode.Substring(i, 1))
Next
For i = 0 To upcCode.Length - 1
If currentLetterArray(i) = 0 Then
g.DrawLine(Pens.White, i + (drawLocation), 0, i + (drawLocation), barHeight - 6)
ElseIf currentLetterArray(i) = 1 Then
g.DrawLine(Pens.Black, i + (drawLocation), 0, i + (drawLocation), barHeight - 6)
End If
Next
End Sub
End Class
End Namespace
-
Jun 7th, 2008, 07:06 PM
#3
Re: Help converting C# class to VB.NET
There's one big 'gotcha' that is missed by the online converters:
Dim currentASCII As Integer = CInt(Convert.ToChar((incomingString.Substring(wholeSet - 1, 1)))) - 48
should be:
Dim currentASCII As Integer = CInt(Fix(Convert.ToChar((incomingString.Substring(wholeSet - 1, 1))))) - 48
Note the use of the VB 'Fix' function - this is necessary to produce the same result as the original C# code. C# integer casts always truncate. The CInt macro rounds to the nearest integer. If you don't take this into account, the code will compile, but fail to produce the same results (which is the worst kind of conversion bug).
-
Jun 7th, 2008, 09:43 PM
#4
Re: Help converting C# class to VB.NET
Using reflector:
VB Code:
Friend Class cUPCA
' Methods
Public Function CreateBarCode(ByVal txt As String, ByVal scale As Integer) As Image
Dim img As Image = Nothing
Me.imageWidth = 120
Me.imageScale = scale
Me.imageWidth = Convert.ToInt32(CSng((Me.imageWidth * Me.imageScale)))
Dim imageHeightHolder As Integer = Convert.ToInt32(CSng((Me.barCodeHeight * Me.imageScale)))
Dim incomingString As String = txt.ToUpper
If (incomingString.Length = 0) Then
Return img
End If
Dim length As Integer = incomingString.Length
Me.newBitmap = New Bitmap(Me.imageWidth, imageHeightHolder, PixelFormat.Format32bppArgb)
Me.g = Graphics.FromImage(Me.newBitmap)
Me.g.ScaleTransform(Me.imageScale, Me.imageScale)
Dim newRec As New Rectangle(0, 0, Me.imageWidth, imageHeightHolder)
Me.g.FillRectangle(New SolidBrush(Color.White), newRec)
Me.placeMarker = 0
txt = (txt.Substring(0, 11) & Me.GetCheckSum(txt).ToString)
Dim wholeSet As Integer = 0
wholeSet = 1
Do While (wholeSet <= Convert.ToInt32(incomingString.Length))
Dim currentASCII As Integer = (Convert.ToChar(incomingString.Substring((wholeSet - 1), 1)) - "0"c)
If (wholeSet = 1) Then
Me.DrawSet(Me.UPCABegin, Me.placeMarker, 0)
Me.DrawSet(Me.UPCALeft(currentASCII), Me.placeMarker, 0)
ElseIf (wholeSet <= 5) Then
Me.DrawSet(Me.UPCALeft(currentASCII), Me.placeMarker, 6)
ElseIf (wholeSet = 6) Then
Me.DrawSet(Me.UPCALeft(currentASCII), Me.placeMarker, 6)
Me.DrawSet(Me.UPCAMiddle, Me.placeMarker, 0)
ElseIf (wholeSet <= 11) Then
Me.DrawSet(Me.UPCARight(currentASCII), Me.placeMarker, 6)
ElseIf (wholeSet = 12) Then
Me.DrawSet(Me.UPCARight(currentASCII), Me.placeMarker, 0)
Me.DrawSet(Me.UPCAEnd, Me.placeMarker, 0)
End If
wholeSet += 1
Loop
Using font As Font = New Font("Courier New, Bold", 9!)
Me.g.DrawString(txt.Substring(0, 1), font, Brushes.Black, New PointF(0!, 67!))
Me.g.DrawString(txt.Substring(1, 5), font, Brushes.Black, New PointF(22!, 67!))
Me.g.DrawString(txt.Substring(6, 5), font, Brushes.Black, New PointF(60!, 67!))
Me.g.DrawString(txt.Substring(11, 1), font, Brushes.Black, New PointF(108!, 67!))
End Using
Return Image.FromHbitmap(Me.newBitmap.GetHbitmap)
End Function
Private Sub DrawSet(ByVal upcCode As String, ByVal drawLocation As Integer, ByVal barHeight As Integer)
Dim currentLetterArray As Integer() = New Integer(upcCode.Length - 1) {}
Me.placeMarker = (Me.placeMarker + upcCode.Length)
barHeight = (Me.barCodeHeight - barHeight)
Dim i As Integer = 0
i = 0
Do While (i <= (upcCode.Length - 1))
currentLetterArray(i) = Convert.ToInt16(upcCode.Substring(i, 1))
i += 1
Loop
i = 0
Do While (i <= (upcCode.Length - 1))
If (currentLetterArray(i) = 0) Then
Me.g.DrawLine(Pens.White, (i + drawLocation), 0, (i + drawLocation), (barHeight - 6))
ElseIf (currentLetterArray(i) = 1) Then
Me.g.DrawLine(Pens.Black, (i + drawLocation), 0, (i + drawLocation), (barHeight - 6))
End If
i += 1
Loop
End Sub
Public Function GetCheckSum(ByVal barCode As String) As Integer
Dim leftSideOfBarCode As String = barCode.Substring(0, 11)
Dim total As Integer = 0
Dim currentDigit As Integer = 0
Dim i As Integer = 0
i = 0
Do While (i <= (leftSideOfBarCode.Length - 1))
currentDigit = Convert.ToInt32(leftSideOfBarCode.Substring(i, 1))
If (((i - 1) Mod 2) = 0) Then
total = (total + currentDigit)
Else
total = (total + (currentDigit * 3))
End If
i += 1
Loop
Return ((10 - (total Mod 10)) Mod 10)
End Function
' Fields
Private barCodeHeight As Integer = 80
Private g As Graphics
Private imageScale As Single = 1!
Private imageWidth As Integer
Private newBitmap As Bitmap
Private placeMarker As Integer
Private UPCABegin As String = "0000000000000101"
Private UPCAEnd As String = "1010000000000000"
Private UPCALeft As String() = New String() { "0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011" }
Private UPCAMiddle As String = "01010"
Private UPCARight As String() = New String() { "1110010", "1100110", "1101100", "1000010", "1011100", "1001110", "1010000", "1000100", "1001000", "1110100" }
End Class
-
Jun 8th, 2008, 10:36 AM
#5
Thread Starter
Addicted Member
Re: Help converting C# class to VB.NET
Hey, thanks all of you guys. I'll try it out.. Thank you so much..
I'll post more to let you know how its going.
Or if I have anymore problems.
Thanks again.
-
Jun 8th, 2008, 11:06 AM
#6
Thread Starter
Addicted Member
Re: Help converting C# class to VB.NET
Okay, So I used the Fix function and the syntax was all right but when I ran the program and got to that in code. It gave me this error:
Code:
An unhandled exception of type 'System.ArgumentException' occurred in microsoft.visualbasic.dll
Additional information: Type of argument 'Number' is 'System.Char', which is not numeric.
SO, I'm really confused on how that last part is worded... But I went ahead and used AscW() instead of the whole "Fix(convert(" section and it works perfectly.
Thank you so much everyone.
If anyone's interested its a barcode class (obviously) :] its pretty cool.
I could upload it if any of you wanted..
Thanks again.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|