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.
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.