65.9K
CodeProject 正在变化。 阅读更多。
Home

如何创建彩色代码生成器(受条形码启发)

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2014年7月13日

CPOL

3分钟阅读

viewsIcon

8685

downloadIcon

4

你将学习如何创建一个颜色代码生成器。颜色代码使用颜色将整数写入图像

引言

在本文中,你将了解颜色代码的工作原理以及如何制作你自己的。它非常适合存储小数字,并且可以生成非常小的文件大小。我创建颜色代码是因为我一直想创建某种条形码。如果你也一直想这样做,那么现在就是你的机会!

创建生成器

创建生成器和解码器所需的工具如下:

  • Microsoft Visual Studio(年份无关紧要。我使用的是2010版)
  • .NET Framework

让我们从创建一个新项目开始。你可以随意命名它。以下是我用来使程序看起来不错的窗体设置:

  • 尺寸为 438 x 165 像素
  • StartPosition: CenterScreen
  • MaximizeButton: False(因为我们不需要它)
  • FormBorderStyle: FixedDialog

现在让我们开始编程

 

首先,让我们创建函数 ReturnRGBValue。此函数的作用是将给定的整数(从 0 到 9)转换为 RGB 值

'It is a function because we want it to return a value, in this case, a clor
Public Function ReturnRGBValue(ByVal Number As Integer) As Color
        'Create our color
        Dim Color As New Color
        'If it is true, we set the color's color 
        If Number = 1 Then
            Color = Color.FromArgb(255, 255, 255)
        ElseIf Number = 2 Then
            Color = Color.FromArgb(245, 245, 245)
        ElseIf Number = 3 Then
            Color = Color.FromArgb(235, 235, 235)
        ElseIf Number = 4 Then
            Color = Color.FromArgb(225, 225, 225)
        ElseIf Number = 5 Then
            Color = Color.FromArgb(215, 215, 215)
        ElseIf Number = 6 Then
            Color = Color.FromArgb(205, 205, 205)
        ElseIf Number = 7 Then
            Color = Color.FromArgb(195, 195, 195)
        ElseIf Number = 8 Then
            Color = Color.FromArgb(185, 185, 185)
        ElseIf Number = 9 Then
            Color = Color.FromArgb(175, 175, 175)
        ElseIf Number = 0 Then
            Color = Color.FromArgb(165, 165, 165)
        End If
        'We return the color that we just set the rgb value
        Return Color
    End Function

现在我们能够从一个简单的数字获取颜色,我们需要创建我们的位图!

这是创建函数。同样,它是一个函数,因为它返回一个位图图像

Public Function Create(ByVal Number As Integer) As Bitmap
        'I convert it to a string and add one to remove a bug
        Dim One As String = "1"
        Dim TempNumber = Number.ToString
        TempNumber = TempNumber + One
        'We convert the number to a string in order to get its length
        Dim StringNumber = TempNumber.ToString
        'We get its length
        NumberLength = StringNumber.Length
        'We create a new bitmap and as you can see, its width is the same as the number of integers
        Dim ColorCode As New Drawing.Bitmap((NumberLength), 1)
        Dim GFX As Graphics = Graphics.FromImage(ColorCode)
        'The for paints the colors to the bitmap
        For i As Integer = 1 To NumberLength
            'To get the number we need to paint, I used Substring(). It starts at i-1 and is long 1 character
            Dim CurrentNumber As Integer = StringNumber.Substring(i - 1, 1)
            'We fill the rectangle, so I created a new solid brush and its color is returned by ReturnRGBValue() Function.
            'The rectangles location is i. Its Y location is always gonna be 0 because the color codes width is always 1
            GFX.FillRectangle(New SolidBrush(ReturnRGBValue(CurrentNumber)), i, 0, 1, 1)
            'Exit the loop when we reach NumberLength, because there are no more numbers to paint
            If i = (NumberLength + 1) Then
                Exit For
            End If
        Next
        'We return the completed color code
        Return ColorCode
    End Function

现在我们已经组装好我们的颜色代码,是时候保存它并创建一个 GUI 了。让我们从创建 GUI 开始。我制作了一张图片,显示你需要添加的内容。请为文本框和按钮使用图片中提供的名称。

全部添加完成后,返回 Form1.vb 选项卡

 

在 Public Class Form1 之后,在页面顶部添加以下内容:

Dim NumberLength As Integer

它没有添加到 Create() 函数中,因为我们需要它用于下一个 Sub

这个 Sub 是将位图保存到用户选择的位置的 Sub

'Sub generated by Visual Studio  
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        'If the user didn't choose a path yet        
        If Button2.Text = "..." Then
            'Tell the user he has to choose a path
            MsgBox("Place select where you want your Color Code to be saved")
        Else    
                'We need to create a temporary bitmap before creating the final one because we need the Create() function
                'to fill the NumberLength variable
                Dim BMP As New Drawing.Bitmap(19, 10)
                'Create bitmap
                BMP = Create(TextBox1.Text)
                'Create final bitmap with the length provided by the Create() function
                Dim NewBMP As New Drawing.Bitmap(NumberLength, 1)
                NewBMP = BMP
                'Save the NewBMP where the user choose to
                NewBMP.Save(Button2.Text)
            End If
    End Sub

我们现在可以保存位图了,但我们需要让用户选择保存的位置。

'Sub generated sub by Visual Studio
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        'Create our new SaveFileDialog
        Dim Save As New SaveFileDialog
        'We allow the PNG, JPG, GIF file types
        Save.Filter = "PNG |*.png |JPG |*.jpg |GIF |*.gif"
        'Display it
        Save.ShowDialog()
        'We set our text to the selected file
        Button2.Text = Save.FileName
    End Sub

我们快完成了。为了防止出现错误,因为 ColorCode 只能处理 9 个数字,并且为了确保用户只能添加数字,我们需要添加以下代码(请注意,第一个 Sub 不是我编写的,而是我之前在网上找到的代码)

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

        '97 - 122 = Ascii codes for simple letters
        '65 - 90  = Ascii codes for capital letters
        '48 - 57  = Ascii codes for numbers

        If Asc(e.KeyChar) <> 8 Then
            If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
                e.Handled = True
            End If
        End If

    End Sub

我们需要做的最后一件事是防止文本框包含超过 9 个字符

  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        TextBox1.MaxLength = 9
  End Sub

完成!...或者说,完成了一半。我们需要创建我们的解码器来读取颜色代码。

创建解码器

首先,创建一个新项目。为了使其看起来不错,请使用我在顶部编写的设置。

这一次,我们需要从颜色获取一个整数,所以添加以下代码:

'It is a function because it returns a value
Public Function ReturnIntValue(ByVal GivenColor As Color) As Integer
        'We get the RGB value of the color
        Dim R As Integer = GivenColor.R
        Dim G As Integer = GivenColor.G
        Dim B As Integer = GivenColor.B

        Dim Int As Integer
        'Check if the RGB values match. If yes, set int value
        If R = 255 And G = 255 And B = 255 Then
            Int = 1
        ElseIf R = 245 And G = 245 And B = 245 Then
            Int = 2
        ElseIf R = 235 And G = 235 And B = 235 Then
            Int = 3
        ElseIf R = 225 And G = 225 And B = 225 Then
            Int = 4
        ElseIf R = 215 And G = 215 And B = 215 Then
            Int = 5
        ElseIf R = 205 And G = 205 And B = 205 Then
            Int = 6
        ElseIf R = 195 And G = 195 And B = 195 Then
            Int = 7
        ElseIf R = 185 And G = 185 And B = 185 Then
            Int = 8
        ElseIf R = 175 And G = 175 And B = 175 Then
            Int = 9
        ElseIf R = 165 And G = 165 And B = 165 Then
            Int = 0
        End If
        'Return int
        Return Int
    End Function

我们快完成了!现在,让我们创建将解码我们的颜色代码的函数:

Public Function Decode(ByVal Image As String) As Integer
        'Create our new bitmap from the image location
        Dim BMP As New Drawing.Bitmap(Image)
        'Get the length of the bitmap, which is used to count the number of ints
        Dim Length As Integer = BMP.Width
        Dim AssembledIntegers As Integer

        
        For i As Integer = 1 To Length
            'If i reaches Length, then exit loop
            If i = Length Then
                Exit For
            End If
            'We assemble the integers, not by adding them.
            'We get the int value by using ReturnIntValue
            'We obtain the color by using GetPixel. Its x location is i and y is always at 0 because a color code only has 1 width
            AssembledIntegers = AssembledIntegers & ReturnIntValue(BMP.GetPixel(i, 0))
            If i = Length Then
                Exit For
            End If
        Next
        'Return the ints
        Return AssembledIntegers
    End Function

是时候构建我们的 GUI 了! 再次,我制作了一张图片,以便更容易可视化

返回 Form1 选项卡,并添加以下代码:

  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        'Create new OpenFileDialog
        Dim Open As New OpenFileDialog
        'Only allow 1 file to be opened
        Open.Multiselect = False
        Open.Title = "Select the color code file"
        'Show the window
        Open.ShowDialog()
        'Set the buttons text to the filename
        Button1.Text = Open.FileName
    End Sub

并且...

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        'If no file has been chosen
        If Button1.Text = "..." Then
            'Tell the user to select a path
            MsgBox("Select a path first")
        Else
            'Write and decode the image
            TextBox1.Text = Decode(Button1.Text)
        End If
End Sub

完成。如果你操作正确,一切都应该正常工作。

关注点

我在遇到错误时确实有点沮丧。没有发生什么有趣的事情 :(

历史

  • 修复了标题中的一个错误
© . All rights reserved.