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

CraxyMouse CraxyMouse 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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
...全文

相关推荐

C#代码项目转换VB.NET项目代码(可整个项目转换)

C#代码项目转换VB.NET项目代码(可整个项目转换)C#代码项目转换VB.NET项目代码(可整个项目转换)

hu0336

VB.NET到C#的代码转换工具指南

枚举(Enum)是用于定义一组命名常量的类型。尽管VB.NET和C#中的枚举在基本概念上是一致的,但它们的声明方式略有不同。例如,在VB.NET中定义枚举类型如下:FirstValueEnd Enum而在C#中,枚举的定义则稍显简洁:在进行代码转换时,需要注意枚举值在两种语言中的相同点和不同点,确保枚举的命名和值都能够正确地映射。除了通用的异常处理语法外,自定义异常类的转换也是代码迁移中的一项重要工作。

一人一猫浪迹天涯

VB.NET转C#代码转换工具实操指南

本文还有配套的精品资源,点击获取 简介:本文介绍了"VB.NET to C# Converter"工具的重要性和应用,该工具能够高效地将VB.NET代码自动转换为C#代码,保留变量名和注释,减少开发者的转换工作量。VB.NET和C#作为.NET平台上的两种语言,各有特点,其中C#在现代编程领域具有广泛应用。工具支持试用版和完整版,试用版可永久使用但处理代码行数有限。详细操作...

郁林成森

VB与C#互转代码的实用工具包

本文还有配套的精品资源,点击获取 简介:VB和C#作为微软的编程语言,各具特色,其中VB适合初学者,而C#则面向对象且性能高效。本转码工具旨在为开发者提供VB与C#代码转换的功能,以帮助理解语言间相似性与差异性,支持语法解析与代码重构,实现高效多项目的转换,并保持代码格式,提供错误检查与修复建议,确保兼容性与更新。工具还包括安装程序、文档及链接资源,使得开发者能够灵活运用不...

周不宅

VB.Net to C# Converter将VB代码转换成C#代码

本资源为【VB.Net to C# Converter】软件,它可以将VB代码转换成C#代码,效果很好

huangwenhua5000
评论 9
收藏 已收藏
分享
返回顶部
CraxyMouse
C# 111,129 成员 642,538 内容