Displaying Emoji on Console Program - VB.Net

VKSB 176 Reputation points
2020-12-19T17:14:30.45+00:00

Dear Friends, I want to display an Emoji on my console program - Not the one like " :-) " but a real one like ![49629-smiling-face.png][1] [1]: /api/attachments/49629-smiling-face.png?platform=QnA

Can you please let me know how to do it?

Thanks
Kind regards

VKSBK

.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
322 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,568 questions
0 comments No comments
{count} votes

Accepted answer
  1. Xingyu Zhao-MSFT 5,356 Reputation points
    2020-12-21T08:05:28.137+00:00

    Hi @VKSB ,

    In order to display an emoji on console program, you need to create a 'Graphics' object and use 'Graphics.DrawImage' method to draw the specified Image.
    Here's an example you can refer to.

    Imports System.Runtime.InteropServices  
    Imports System.Drawing  
      
    Module Module1  
      
        <DllImport("kernel32.dll", EntryPoint:="GetConsoleWindow", SetLastError:=True)>  
        Private Function GetConsoleHandle() As IntPtr  
        End Function  
      
        Sub Main()  
            Dim handler = GetConsoleHandle()  
            ' Need to add System.Drawing reference.  
            Using g = Graphics.FromHwnd(handler)  
                Using img = Image.FromFile("D:\Images\1.png")  
                    g.DrawImage(img, 10, 10, 50, 50)  
                End Using  
            End Using  
            Console.ReadLine()  
        End Sub  
      
    End Module  
    

    Result of my test.
    49846-2.png

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


3 additional answers

Sort by: Most helpful
  1. VKSB 176 Reputation points
    2020-12-21T13:22:42.127+00:00

    Dear XingyuZhao,

    Thanks for the help.

    I don't understand the following;

    "<DllImport("kernel32.dll", EntryPoint:="GetConsoleWindow", SetLastError:=True)>
    

    Where should I write this?

    When I copy all code to my "Trial Project" it shows error.

    I did the following to "System.Drawing reference";

    Menu --> Project --> Add Reference -->.Net ---> System.Drawing.

    Please help.

    Kind regards
    VKSBK


  2. Xingyu Zhao-MSFT 5,356 Reputation points
    2020-12-22T06:47:30.14+00:00

    Hi @VKSB ,
    I provide the whole code in my above reply, and you can take a look.
    Besides, if the above example is not helpful, the following example might be a better solution. ( code from Display a Image in a console application ).

    Imports System.Runtime.InteropServices  
    Imports System.Drawing  
    Imports System.IO  
      
    Module Module1  
      
        <DllImport("kernel32.dll", SetLastError:=True)>  
        Private Function GetConsoleWindow() As IntPtr  
        End Function  
      
        <DllImport("kernel32.dll", SetLastError:=True)>  
        Private Function CreateFile(ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByVal lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As IntPtr) As IntPtr  
        End Function  
      
        <DllImport("kernel32.dll", SetLastError:=True)>  
        Private Function GetCurrentConsoleFont(ByVal hConsoleOutput As IntPtr, ByVal bMaximumWindow As Boolean,  
        <Out>  
        <MarshalAs(UnmanagedType.LPStruct)> ByVal lpConsoleCurrentFont As ConsoleFontInfo) As Boolean  
        End Function  
      
        <StructLayout(LayoutKind.Sequential)>  
        Friend Class ConsoleFontInfo  
            Friend nFont As Integer  
            Friend dwFontSize As Coord  
        End Class  
      
        <StructLayout(LayoutKind.Explicit)>  
        Friend Structure Coord  
            <FieldOffset(0)>  
            Friend X As Short  
            <FieldOffset(2)>  
            Friend Y As Short  
        End Structure  
      
        Private Const GENERIC_READ As Integer = &H80000000  
        Private Const GENERIC_WRITE As Integer = &H40000000  
        Private Const FILE_SHARE_READ As Integer = 1  
        Private Const FILE_SHARE_WRITE As Integer = 2  
        Private Const INVALID_HANDLE_VALUE As Integer = -1  
        Private Const OPEN_EXISTING As Integer = 3  
      
        Sub Main()  
            Dim location As Point = New Point(1, 1)  
            ' Desired image size in characters.'  
            Dim imageSize As Size = New Size(10, 5)  
      
            Dim path As String = "your image path"  
      
            Using g As Graphics = Graphics.FromHwnd(GetConsoleWindow())      
                Using image As Image = Image.FromFile(path)  
                    Dim fontSize As Size = GetConsoleFontSize()  
                    ' Translating the character positions to pixels.'  
                    ' You can change image's location by adjust 'location' and 'imageSize'.  
                    Dim imageRect As Rectangle = New Rectangle(location.X * fontSize.Width, location.Y * fontSize.Height, imageSize.Width * fontSize.Width, imageSize.Height * fontSize.Height)  
                    g.DrawImage(image, imageRect)  
                End Using  
            End Using  
            Console.ReadLine()  
        End Sub  
    
        Private Function GetConsoleFontSize() As Size  
            ' Getting the console out buffer handle.'  
            Dim outHandle As IntPtr =  
            CreateFile("CONOUT$", GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)  
            Dim errorCode As Integer = Marshal.GetLastWin32Error()  
      
            If outHandle.ToInt32() = INVALID_HANDLE_VALUE Then  
                Throw New IOException("Unable to open CONOUT$", errorCode)  
            End If  
      
            Dim cfi As ConsoleFontInfo = New ConsoleFontInfo()  
      
            If Not GetCurrentConsoleFont(outHandle, False, cfi) Then  
                Throw New InvalidOperationException("Unable to get font information.")  
            End If  
      
            Return New Size(cfi.dwFontSize.X, cfi.dwFontSize.Y)  
        End Function  
    End Module  
    

    Result.
    50249-3.png

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. VKSB 176 Reputation points
    2020-12-23T12:38:50.463+00:00

    Dear XingyuZhao & WayneAKing,

    Thanks for the replies. Sorry, I somehow missed the "Imports" parts when copying the code.

    Now the 1st code & the 2nd code work fine.

    The Emoji disappears on scroll the console Window.

    Kind regards
    VKSBK