谁能帮我把VB代码转成C#谢谢!!

CraxyMouse 2010-01-21 11:37:09
Imports System.Text
Imports System.Drawing
Imports System.Drawing.Imaging
Public Class FontConvertBmp
Private m_FontName As String '字体名字
Private m_FontSize As Integer '字体大小
Private m_String As String '需要打印的文本
Private m_FontBold As Boolean '字体样式
Private m_X As Integer, m_Y As Integer '打印时候的X,Y坐标
Private m_XZoom As Integer, m_YZoom As Integer '打印时候的X,Y方向的放大倍数最大只能为10倍
Private List1 As List(Of Char), List2 As List(Of Integer) '压缩代码集合
Private S As String
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As IntPtr) As Integer
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As IntPtr) As IntPtr
Public Property XZoom() As Integer
Get
Return m_XZoom
End Get
Set(ByVal value As Integer)
If value > 0 AndAlso value <= 10 Then
m_XZoom = value
End If
End Set
End Property
Public Property YZoom() As Integer
Get
Return m_YZoom
End Get
Set(ByVal value As Integer)
If value > 0 AndAlso value <= 10 Then
m_YZoom = value
End If
End Set
End Property
Public Property X() As Integer
Get
Return m_X
End Get
Set(ByVal value As Integer)
If value >= 0 Then
m_X = value
End If
End Set
End Property
Public Property Y() As Integer
Get
Return m_Y
End Get
Set(ByVal value As Integer)
If value >= 0 Then
m_Y = value
End If
End Set
End Property
Public Property FontName() As String
Get
Return m_FontName
End Get
Set(ByVal value As String)
m_FontName = value
End Set
End Property
Public Property FontSize() As Integer
Get
Return m_FontSize
End Get
Set(ByVal value As Integer)
If value > 7 AndAlso value <= 500 Then
m_FontSize = value
End If
End Set
End Property
Public Property Text() As String
Get
Return m_String
End Get
Set(ByVal value As String)
m_String = value
End Set
End Property
Public Property FontBold() As Boolean
Get
Return m_FontBold
End Get
Set(ByVal value As Boolean)
m_FontBold = value
End Set
End Property
Public Sub New()
m_FontName = "宋体"
m_FontSize = 12
m_FontBold = False
m_X = 1
m_Y = 1
m_XZoom = 1
m_YZoom = 1
InitDictionary()
End Sub
Public Function Convert() As String
Dim Hdc As IntPtr
Hdc = CreateCompatibleDC(IntPtr.Zero)
Dim Graphic As Graphics = Graphics.FromHdc(Hdc)
Dim Width As Integer, Height As Integer
Dim G As Graphics, Image As Bitmap
Dim Font As Font
If m_FontBold = True Then
Font = New Font(m_FontName, m_FontSize, Drawing.FontStyle.Bold, GraphicsUnit.Pixel)
Else
Font = New Font(m_FontName, m_FontSize, Drawing.FontStyle.Regular, GraphicsUnit.Pixel)
End If
Width = (CInt(Graphic.MeasureString(m_String, Font).Width) \ 8 + 1) * 8 '将行规格话便于计算字节
Height = Font.Height '列高
Image = New Bitmap(Width, Height, PixelFormat.Format32bppArgb)
G = Graphics.FromImage(Image)
G.Clear(Color.White)
G.DrawString(m_String, Font, Brushes.Black, 0, 2) '将文本写入图片
Dim Value As Integer = Width * Height
Dim TempString As New StringBuilder(Value)
Dim DesString As New StringBuilder(Value)
DesString.Append("~DGOUTSTR01," & Width * Height \ 8 & "," & Width \ 8 & ",")
Dim i As Integer, j As Integer
Dim Sum As Integer
For j = 0 To Height - 1 '由于内存中的图片是倒置,所以要反取数据
For i = 0 To Width \ 4 - 1
Sum = 0
For m As Integer = 0 To 3 '由于4个点可用1个十六进制数表示因此一次取4个点进行转换
If Image.GetPixel(i * 4 + m, j).B = 0 Then '由于只打印黑白点所以根据RGB分量判断是否需要打印该点
Sum += 1 << (3 - m) '通过移位操作将该点的信息与一个BIT相对应
End If
Next
TempString.Append(Hex(Sum)) '将取出的点转换成16进制数
Next
Next
Dim Count As Integer = 1 '将转成16进制的文本进行压缩
For i = 1 To TempString.Length - 1
If TempString.Chars(i - 1) = TempString.Chars(i) Then
Count += 1
If i = TempString.Length - 1 Then
DesString.Append(CompressCode(Count) & TempString.Chars(i))
End If
Else
DesString.Append(CompressCode(Count) & TempString.Chars(i - 1))
S = String.Empty
Count = 1
End If
Next
DesString.Append(vbCrLf & "^FO" & m_X.ToString & "," & m_Y.ToString & "^XGOUTSTR01," & m_XZoom & "," & m_YZoom & ",^FS")
DeleteDC(Hdc)
G.Dispose()
Image.Dispose()
Graphic.Dispose()
Return DesString.ToString
End Function
Private Function CompressCode(ByRef Input As Integer) As String
If Input > 1 Then
For i As Integer = List1.Count - 1 To 0 Step -1 '使用递归和贪婪算法将连续的数字转换成ZPL压缩代码,如000可用I0表示
If Input >= List2.Item(i) Then
S &= List1.Item(i)
Input -= List2.Item(i)
CompressCode(Input)
End If
Next
End If
Return S
End Function
Private Sub InitDictionary()
'将ZPL定义的压缩代码写入集合
List1 = New List(Of Char)
List2 = New List(Of Integer)
For i As Integer = 0 To 18
List1.Add(ChrW(71 + i))
List2.Add(i + 1)
Next
For i As Integer = 0 To 19
List1.Add(ChrW(103 + i))
List2.Add(20 * (i + 1))
Next
End Sub
End Class
...全文
175 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
z415353144 2010-01-25
  • 打赏
  • 举报
回复
顶二楼的,不过有的转过来有点小毛病,得改改。
messi_yang 2010-01-25
  • 打赏
  • 举报
回复
有網站可以直接轉化的···
24K純帥 2010-01-25
  • 打赏
  • 举报
回复
up..
longhair9711 2010-01-25
  • 打赏
  • 举报
回复
up
LoveMango 2010-01-25
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 psyke99 的回复:]
不用Reflector.exe?.net 必备工具
推荐一下

[/Quote]
up
psyke99 2010-01-25
  • 打赏
  • 举报
回复
不用Reflector.exe?.net 必备工具
推荐一下
yilanwuyu123 2010-01-21
  • 打赏
  • 举报
回复
mark
满衣兄 2010-01-21
  • 打赏
  • 举报
回复
http://www.developerfusion.com/tools/convert/vb-to-csharp/
这个网站挺不错的,在线转。
满衣兄 2010-01-21
  • 打赏
  • 举报
回复

using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
public class FontConvertBmp
{
private string m_FontName;
//字体名字
private int m_FontSize;
//字体大小
private string m_String;
//需要打印的文本
private bool m_FontBold;
//字体样式
private int m_X;
private int m_Y;
//打印时候的X,Y坐标
private int m_XZoom;
private int m_YZoom;
//打印时候的X,Y方向的放大倍数最大只能为10倍
private List<char> List1;
private List<int> List2;
//压缩代码集合
private string S;
[DllImport("gdi32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern int DeleteDC(IntPtr hdc);
[DllImport("gdi32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern IntPtr CreateCompatibleDC(IntPtr hdc);
public int XZoom {
get { return m_XZoom; }
set {
if (value > 0 && value <= 10) {
m_XZoom = value;
}
}
}
public int YZoom {
get { return m_YZoom; }
set {
if (value > 0 && value <= 10) {
m_YZoom = value;
}
}
}
public int X {
get { return m_X; }
set {
if (value >= 0) {
m_X = value;
}
}
}
public int Y {
get { return m_Y; }
set {
if (value >= 0) {
m_Y = value;
}
}
}
public string FontName {
get { return m_FontName; }
set { m_FontName = value; }
}
public int FontSize {
get { return m_FontSize; }
set {
if (value > 7 && value <= 500) {
m_FontSize = value;
}
}
}
public string Text {
get { return m_String; }
set { m_String = value; }
}
public bool FontBold {
get { return m_FontBold; }
set { m_FontBold = value; }
}
public FontConvertBmp()
{
m_FontName = "宋体";
m_FontSize = 12;
m_FontBold = false;
m_X = 1;
m_Y = 1;
m_XZoom = 1;
m_YZoom = 1;
InitDictionary();
}
public string Convert()
{
IntPtr Hdc = default(IntPtr);
Hdc = CreateCompatibleDC(IntPtr.Zero);
Graphics Graphic = Graphics.FromHdc(Hdc);
int Width = 0;
int Height = 0;
Graphics G = default(Graphics);
Bitmap Image = default(Bitmap);
Font Font = default(Font);
if (m_FontBold == true) {
Font = new Font(m_FontName, m_FontSize, Drawing.FontStyle.Bold, GraphicsUnit.Pixel);
}
else {
Font = new Font(m_FontName, m_FontSize, Drawing.FontStyle.Regular, GraphicsUnit.Pixel);
}
Width = ((int)Graphic.MeasureString(m_String, Font).Width / 8 + 1) * 8;
//将行规格话便于计算字节
Height = Font.Height;
//列高
Image = new Bitmap(Width, Height, PixelFormat.Format32bppArgb);
G = Graphics.FromImage(Image);
G.Clear(Color.White);
G.DrawString(m_String, Font, Brushes.Black, 0, 2);
//将文本写入图片
int Value = Width * Height;
StringBuilder TempString = new StringBuilder(Value);
StringBuilder DesString = new StringBuilder(Value);
DesString.Append("~DGOUTSTR01," + Width * Height / 8 + "," + Width / 8 + ",");
int i = 0;
int j = 0;
int Sum = 0;
for (j = 0; j <= Height - 1; j++) {
//由于内存中的图片是倒置,所以要反取数据
for (i = 0; i <= Width / 4 - 1; i++) {
Sum = 0;
for (int m = 0; m <= 3; m++) {
//由于4个点可用1个十六进制数表示因此一次取4个点进行转换
if (Image.GetPixel(i * 4 + m, j).B == 0) {
//由于只打印黑白点所以根据RGB分量判断是否需要打印该点
//通过移位操作将该点的信息与一个BIT相对应
Sum += 1 < (3 - m);
}
}
//将取出的点转换成16进制数
TempString.Append(Conversion.Hex(Sum));
}
}
int Count = 1;
//将转成16进制的文本进行压缩
for (i = 1; i <= TempString.Length - 1; i++) {
if (TempString.Chars(i - 1) == TempString.Chars(i)) {
Count += 1;
if (i == TempString.Length - 1) {
DesString.Append(CompressCode(Count) + TempString.Chars(i));
}
}
else {
DesString.Append(CompressCode(Count) + TempString.Chars(i - 1));
S = string.Empty;
Count = 1;
}
}
DesString.Append(Constants.vbCrLf + "^FO" + m_X.ToString + "," + m_Y.ToString + "^XGOUTSTR01," + m_XZoom + "," + m_YZoom + ",^FS");
DeleteDC(Hdc);
G.Dispose();
Image.Dispose();
Graphic.Dispose();
return DesString.ToString;
}
private string CompressCode(ref int Input)
{
if (Input > 1) {
for (int i = List1.Count - 1; i >= 0; i += -1) {
//使用递归和贪婪算法将连续的数字转换成ZPL压缩代码,如000可用I0表示
if (Input >= List2.Item(i)) {
S += List1.Item(i);
Input -= List2.Item(i);
CompressCode(Input);
}
}
}
return S;
}
private void InitDictionary()
{
//将ZPL定义的压缩代码写入集合
List1 = new List<char>();
List2 = new List<int>();
for (int i = 0; i <= 18; i++) {
List1.Add(Strings.ChrW(71 + i));
List2.Add(i + 1);
}
for (int i = 0; i <= 19; i++) {
List1.Add(Strings.ChrW(103 + i));
List2.Add(20 * (i + 1));
}
}
}
http://www.developerfusion.com/tools/convert/vb-to-csharp/

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧