question

VKSB-0116 avatar image
0 Votes"
VKSB-0116 asked ·

Displaying Emoji on Console Program - VB.Net

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]: /answers/storage/attachments/49629-smiling-face.png

Can you please let me know how to do it?

Thanks
Kind regards

VKSBK

dotnet-visual-basicdotnet-console
smiling-face.png (54.7 KiB)
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

XingyuZhao-MSFT avatar image
0 Votes"
XingyuZhao-MSFT answered ·

Hi @VKSB-0116 ,

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.




2.png (69.5 KiB)
· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Note that a problem with doing this is that the emoji will not be redrawn
if focus is taken away from the console window and then returned. In that
case the emoji will disappear from the console window.

  • Wayne

0 Votes 0 ·
VKSB-0116 avatar image
0 Votes"
VKSB-0116 answered ·

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





· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

What error does it show? Don't make us guess.

Note that you may need to put Imports directives at the start
of your code:

 Imports System.Drawing
 Imports System.Runtime.InteropServices
    
 Module Module1
     <DllImport("kernel32.dll", EntryPoint:="GetConsoleWindow", SetLastError:=True)>
     Private Function GetConsoleHandle() As IntPtr
     End Function
     Sub Main()
 ...

  • Wayne

0 Votes 0 ·
XingyuZhao-MSFT avatar image
0 Votes"
XingyuZhao-MSFT answered ·

Hi @VKSB-0116 ,
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.





3.png (108.3 KiB)
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

VKSB-0116 avatar image
0 Votes"
VKSB-0116 answered ·

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

· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

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

No need to be sorry. You didn't miss them, they weren't in the code when
Xingyu Zhao originally posted it and before I posted my reply. Xingyu Zhao
edited the original answer later to add them.

  • Wayne

0 Votes 0 ·