Is there any way to attribute a code for every color, and be able to read them? Programatically I mean. My target is to convert an image into code, then convert it back to image.
问问题
116 次
2 回答
0
每种颜色实际上是一个 ARGB 颜色代码只需获取整数值
Dim myColor As Color = Color.Red
Dim Code As Integer = myColor.ToArgb()
Dim myColorBack As Color = Color.FromArgb(Code)
于 2013-03-23T09:16:53.270 回答
0
每种颜色有 4 个字段(对于某些图像,您只有 3 个包含有意义信息的字段)
这些字段包括:
- Alpha (
A
)(这表示像素的不透明度) - 红色 (
R
)(红色强度) - 绿色 (
G
)(绿色强度) - 蓝色 (
B
)(蓝色强度)
您要做的是读取它们并通过将这些值相互连接来为每个生成一个代码字符串。
这很可能被认为是伪代码,因为我没有检查它是否编译,但你应该按照这个做一些事情。
Dim pixelColor As Color
Dim image As BitMap = New BitMap("your_image.png")
Dim a As String
Dim r As String
Dim b As String
Dim g As String
Dim fileString As New StringBuilder()
fileString.AppendLine(image.Size.Width.ToString())
fileString.AppendLine(image.Size.Height.ToString())
' Loop over all pixels
For y As Integer = 0 To image.Size.Height - 1
For x As Integer = 0 To image.Size.Width - 1
pixelColor = image.GetPixel(x, y)
' get ARGB values as strings
a = pixelColor.A.ToString()
r = pixelColor.R.ToString()
g = pixelColor.G.ToString()
b = pixelColor.B.ToString()
' Append the colors, one pixel per line
fileString.AppendLine(a & " " & r & " " & g & " " & b)
Next
Next
Using file As New StreamWriter("image_data.txt")
outfile.Write(fileString.ToString())
End Using
同样,这可能无法编译。(我现在没有编译器)
编辑: 我意识到宽度和高度也需要存储。
至于读取文件:
Dim file As System.IO.StreamReader
file = File.OpenText("text_file.txt")
Dim width As Integer = Convert.ToInt32(file.ReadLine)
Dim height As Integer = Convert.ToInt32(file.ReadLine)
Dim image As BitMap = New BitMap(width, height)
Dim currentX As Integer = 0
Dim currentY As Integer = 0
Do Until file.EndOfStream
Dim line As String = file.ReadLine
Dim valueArray(4) As String = line.Split(" ")
Dim a As Integer = Convert.ToInt16(valueArray(0))
Dim r As Integer = Convert.ToInt16(valueArray(1))
Dim g As Integer = Convert.ToInt16(valueArray(2))
Dim b As Integer = Convert.ToInt16(valueArray(3))
image.SetPixel(currentX, currentY, Color.FromArgb(a, r, g, b))
currentX = currentX + 1
If currentX == width Then
currentX = 0
currentY = currentY + 1
If currentY == height Then
Exit Do ' We're done here.
End If
End If
Loop
' At this point, you'll have a BitMap with all the pixels set.
再次考虑这个伪代码。
于 2013-03-23T09:09:15.643 回答