Hi vijay9471,
You can not convert the console output to image.
You have to convert the output to text file and then convert the text file to image and send as attachment.
Check the below code.
Code
C#
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
namespace Bar_Graph_Console
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("Name"),
new DataColumn("Mark")
});
dt.Rows.Add("Math", 50);
dt.Rows.Add("Science", 46);
dt.Rows.Add("English", 48);
dt.Rows.Add("Chemistry", 35);
DataSet ds = new DataSet();
ds.Tables.Add(dt);
List<KeyValuePair<string, int>> list = new List<KeyValuePair<string, int>>();
foreach (DataRow dr in ds.Tables[0].Rows)
{
list.Add(new KeyValuePair<string, int>(Convert.ToString(dr["Name"]), Convert.ToInt32(dr["Mark"])));
}
// Draws greph.
Console.WriteLine();
Console.WriteLine();
var max = list.Max(x => x.Key.Length);
foreach (var item in list)
{
Console.Write(item.Key.PadLeft(max));
Console.Write(" ");
Console.BackgroundColor = ConsoleColor.DarkBlue;
for (var i = 0; i < item.Value; i++)
{
Console.Write(" ");
}
Console.BackgroundColor = ConsoleColor.Black;
Console.Write(" ");
Console.WriteLine(item.Value);
Console.WriteLine();
}
// Save as text file.
string filePath = Path.Combine(Directory.GetCurrentDirectory(), "output.txt");
using (var sw = new StreamWriter(new FileStream(filePath, FileMode.Create)))
{
Console.SetOut(sw);
max = list.Max(x => x.Key.Length);
foreach (var item in list)
{
Console.Write(item.Key.PadLeft(max));
Console.Write(" ");
Console.BackgroundColor = ConsoleColor.DarkBlue;
for (var i = 0; i < item.Value; i++)
{
Console.Write(" ");
}
Console.BackgroundColor = ConsoleColor.Black;
Console.Write(" ");
Console.WriteLine(item.Value);
Console.WriteLine();
}
sw.Flush();
}
// Convert the text to image and send in email.
string text = File.ReadAllText(filePath);
Bitmap bitmap = new Bitmap(1, 1);
Font font = new Font("Arial", 25, FontStyle.Regular, GraphicsUnit.Pixel);
Graphics graphics = Graphics.FromImage(bitmap);
int width = (int)graphics.MeasureString(text, font).Width;
int height = (int)graphics.MeasureString(text, font).Height;
bitmap = new Bitmap(bitmap, new Size(width, height));
graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.White);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
graphics.DrawString(text, font, new SolidBrush(Color.FromArgb(255, 0, 0)), 0, 0);
graphics.Flush();
graphics.Dispose();
using (MemoryStream memoryStream = new MemoryStream())
{
bitmap.Save(memoryStream, ImageFormat.Jpeg);
// Send email.
MailMessage mm = new MailMessage("sender@gmail.com", "receiver@gmail.com");
mm.Subject = "Bar graph";
mm.Body = "Bar graph Attachment";
mm.Attachments.Add(new Attachment(memoryStream, "iTextSharpPDF.pdf"));
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential();
NetworkCred.UserName = "sender@gmail.com";
NetworkCred.Password = "<password>";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
}
Console.Read();
}
}
}
VB.Net
Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.Drawing.Text
Imports System.IO
Imports System.Linq
Imports System.Net
Imports System.Net.Mail
Imports System.Text
Namespace Bar_Graph_Console
Class Program
Private Shared Sub Main(ByVal args As String())
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("Name"), New DataColumn("Mark")})
dt.Rows.Add("Math", 50)
dt.Rows.Add("Science", 46)
dt.Rows.Add("English", 48)
dt.Rows.Add("Chemistry", 35)
Dim ds As DataSet = New DataSet()
ds.Tables.Add(dt)
Dim list As List(Of KeyValuePair(Of String, Integer)) = New List(Of KeyValuePair(Of String, Integer))()
For Each dr As DataRow In ds.Tables(0).Rows
list.Add(New KeyValuePair(Of String, Integer)(Convert.ToString(dr("Name")), Convert.ToInt32(dr("Mark"))))
Next
Console.WriteLine()
Console.WriteLine()
Dim max = list.Max(Function(x) x.Key.Length)
For Each item In list
Console.Write(item.Key.PadLeft(max))
Console.Write(" ")
Console.BackgroundColor = ConsoleColor.DarkBlue
For i = 0 To item.Value - 1
Console.Write(" ")
Next
Console.BackgroundColor = ConsoleColor.Black
Console.Write(" ")
Console.WriteLine(item.Value)
Console.WriteLine()
Next
Dim filePath As String = Path.Combine(Directory.GetCurrentDirectory(), "output.txt")
Using sw = New StreamWriter(New FileStream(filePath, FileMode.Create))
Console.SetOut(sw)
max = list.Max(Function(x) x.Key.Length)
For Each item In list
Console.Write(item.Key.PadLeft(max))
Console.Write(" ")
Console.BackgroundColor = ConsoleColor.DarkBlue
For i = 0 To item.Value - 1
Console.Write(" ")
Next
Console.BackgroundColor = ConsoleColor.Black
Console.Write(" ")
Console.WriteLine(item.Value)
Console.WriteLine()
Next
sw.Flush()
End Using
Dim text As String = File.ReadAllText(filePath)
Dim bitmap As Bitmap = New Bitmap(1, 1)
Dim font As Font = New Font("Arial", 25, FontStyle.Regular, GraphicsUnit.Pixel)
Dim graphics As Graphics = Graphics.FromImage(bitmap)
Dim width As Integer = CInt(graphics.MeasureString(text, font).Width)
Dim height As Integer = CInt(graphics.MeasureString(text, font).Height)
bitmap = New Bitmap(bitmap, New Size(width, height))
graphics = Graphics.FromImage(bitmap)
graphics.Clear(Color.White)
graphics.SmoothingMode = SmoothingMode.AntiAlias
graphics.TextRenderingHint = TextRenderingHint.AntiAlias
graphics.DrawString(text, font, New SolidBrush(Color.FromArgb(255, 0, 0)), 0, 0)
graphics.Flush()
graphics.Dispose()
Using memoryStream As MemoryStream = New MemoryStream()
bitmap.Save(memoryStream, ImageFormat.Jpeg)
Dim mm As MailMessage = New MailMessage("sender@gmail.com", "receiver@gmail.com")
mm.Subject = "Bar graph"
mm.Body = "Bar graph Attachment"
mm.Attachments.Add(New Attachment(memoryStream, "iTextSharpPDF.pdf"))
mm.IsBodyHtml = True
Dim smtp As SmtpClient = New SmtpClient()
smtp.Host = "smtp.gmail.com"
smtp.EnableSsl = True
Dim NetworkCred As NetworkCredential = New NetworkCredential()
NetworkCred.UserName = "sender@gmail.com"
NetworkCred.Password = "<password>"
smtp.UseDefaultCredentials = True
smtp.Credentials = NetworkCred
smtp.Port = 587
smtp.Send(mm)
End Using
Console.Read()
End Sub
End Class
End Namespace
Screenshot