转ASCII图

Posted VB.Net

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了转ASCII图相关的知识,希望对你有一定的参考价值。

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
由于某些原因,很久没有写点什么。
这里把以前写的图片转ASII的代码放出。

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim opf As New OpenFileDialog()
        opf.Filter = "图片文件|*.jpg;*.png"
        opf.Multiselect = False
        If opf.ShowDialog <> DialogResult.OK Then
            Exit Sub
        End If
        Dim imgFile As String = opf.FileName
        PictureBox1.Image = Image.FromFile(imgFile)

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        Dim sfd As New SaveFileDialog()
        sfd.Filter = "文本文件|*.txt"
        If sfd.ShowDialog <> DialogResult.OK Then
            Exit Sub
        End If
        Dim txtFile As String = sfd.FileName


        Dim bmp As New Bitmap(100, 50)
        Dim g As Graphics = Graphics.FromImage(bmp)
        g.DrawImage(PictureBox1.Image, New Rectangle(0, 0, bmp.Width, bmp.Height), New Rectangle(0, 0, PictureBox1.Image.Width, PictureBox1.Image.Height), GraphicsUnit.Pixel)
        g.Dispose()


        Dim outputText As New System.Text.StringBuilder

        For i As Integer = 0 To bmp.Height - 1
            For j As Integer = 0 To bmp.Width - 1
                Dim currentColor As Color = bmp.GetPixel(j, i)
                outputText.Append(getAscii(currentColor))
            Next
            outputText.Append(ControlChars.CrLf)
        Next

        Dim fs As New IO.FileStream(txtFile, IO.FileMode.CreateNew, IO.FileAccess.Write)
        Dim sw As New IO.StreamWriter(fs)
        sw.Write(outputText.ToString)
        sw.Close()
        fs.Close()


    End Sub

    ' Dim ascChars() As String = {" ", "-", "+", "=", "%", "*", "$", "#", "&", "@", "@"}
    Dim ascChars() As String = {"@", "&", "#", "$", "*", "%", "=", "+", "-", " ", " "}

    'Dim ascChars() As String = {"M", "N", "H", "Q", "&", "O", "C", ">", "—", "  ", "  "}
    'Dim ascChars() As String = {"m", "n", "h", "Q", "&", "O", "C", "?", "-", " ", " "}


    '均值法获得灰度,并返回对应的字符
    Private Function getAscii(ByVal inputColor As Color) As String
        Dim R, G, B As Integer
        R = inputColor.R
        G = inputColor.G
        B = inputColor.B
        '灰度
        Dim gray As Integer
        gray = (R + G + B) / 3

        Dim grayCharValue As Integer = gray \\ 25

        Return ascChars(grayCharValue)
    End Function

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click


        Dim bmp As New Bitmap(100, 100)
        Dim g As Graphics = Graphics.FromImage(bmp)
        g.DrawImage(PictureBox1.Image, New Rectangle(0, 0, bmp.Width, bmp.Height), New Rectangle(0, 0, PictureBox1.Image.Width, PictureBox1.Image.Height), GraphicsUnit.Pixel)
        g.Dispose()

        Dim bmpDest As New Bitmap(100, 100)

        For i As Integer = 0 To bmp.Height - 1
            For j As Integer = 0 To bmp.Width - 1
                Dim currentColor As Color = getgray(bmp.GetPixel(j, i))

                bmpDest.SetPixel(j, i, currentColor)
            Next
        Next

        PictureBox2.Image = bmpDest
    End Sub

    '均值法获得灰度,并返回对应的字符
    Private Function getgray(ByVal inputColor As Color) As Color
        Dim R, G, B As Integer
        R = inputColor.R
        G = inputColor.G
        B = inputColor.B
        '灰度
        Dim gray As Integer
        gray = (R + G + B) / 3
        Return Color.FromArgb(gray, gray, gray)
    End Function

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Dim sfd As New SaveFileDialog()
        sfd.Filter = "图片|*.jpg"
        If sfd.ShowDialog <> DialogResult.OK Then
            Exit Sub
        End If
        Dim bmpFile As String = sfd.FileName


        Dim bmpWidth As Integer = 150
        Dim bmpHeight As Integer
        '缩放倍数
        Dim multiple As Integer
        '缩放系数
        Dim coefficient As Integer = 2
        multiple = PictureBox1.Image.Width \\ bmpWidth
        If PictureBox1.Image.Width Mod bmpWidth > 0 Then multiple += 1
        bmpHeight = PictureBox1.Image.Height \\ (multiple * coefficient)

        Dim bmp As New Bitmap(bmpWidth, bmpHeight)
        Dim g As Graphics = Graphics.FromImage(bmp)
        g.DrawImage(PictureBox1.Image, New Rectangle(0, 0, bmp.Width, bmp.Height), New Rectangle(0, 0, PictureBox1.Image.Width, PictureBox1.Image.Height), GraphicsUnit.Pixel)

        Dim outputText As New System.Text.StringBuilder

        For i As Integer = 0 To bmp.Height - 1
            For j As Integer = 0 To bmp.Width - 1
                Dim currentColor As Color = bmp.GetPixel(j, i)
                outputText.Append(getAscii(currentColor))
            Next
            outputText.Append(ControlChars.CrLf)
        Next


        Dim f As New Font(New FontFamily("宋体"), 12)

        Dim asciiSize As SizeF
        asciiSize = g.MeasureString(outputText.ToString, f)
        g.Dispose()


        Dim newWidth As Integer = CInt(asciiSize.Width)
        Dim newHeight As Integer = CInt(asciiSize.Height)
        Dim newBmp As New Bitmap(newWidth, newHeight)

        Dim newg As Graphics = Graphics.FromImage(newBmp)
        newg.Clear(Color.White)
        newg.DrawString(outputText.ToString, f, New SolidBrush(Color.Black), New Point(0, 0))
        newg.Dispose()
        newBmp.Save(bmpFile)


    End Sub

其中 ascChars 是生成使用的Ascii字符。这个只有慢慢测试哪些用的上了。
最后的结果:

由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供的参考。

学习更多vb.net知识,请参看 vb.net 教程 目录

以上是关于转ASCII图的主要内容,如果未能解决你的问题,请参考以下文章

图片转ascii网站(图像转字符图片转字符字符画字符图图像转ascii)

ASCII数字对应表

[转]perf + 火焰图分析程序性能

ASCII 码转音频

很实用的JQuery代码片段(转)

C语言编程取字符ASCII码的表示方法