-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Zixu_Wang
authored and
Zixu_Wang
committed
May 27, 2020
1 parent
fb4d377
commit 8f9c331
Showing
11 changed files
with
279 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio 15 | ||
VisualStudioVersion = 15.0.28307.329 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Img2ColorfulChars", "Img2ColorfulChars\Img2ColorfulChars.csproj", "{D81C52AC-CA7A-4C10-BC4E-6508677619AB}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{D81C52AC-CA7A-4C10-BC4E-6508677619AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{D81C52AC-CA7A-4C10-BC4E-6508677619AB}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{D81C52AC-CA7A-4C10-BC4E-6508677619AB}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{D81C52AC-CA7A-4C10-BC4E-6508677619AB}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {023768DD-FD19-4DF5-98B7-D1688A2035B4} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<configuration> | ||
<startup> | ||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /> | ||
</startup> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{D81C52AC-CA7A-4C10-BC4E-6508677619AB}</ProjectGuid> | ||
<OutputType>Exe</OutputType> | ||
<RootNamespace>Img2ColorfulChars</RootNamespace> | ||
<AssemblyName>Img2ColorfulChars</AssemblyName> | ||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> | ||
<Deterministic>true</Deterministic> | ||
<PublishUrl>publish\</PublishUrl> | ||
<Install>true</Install> | ||
<InstallFrom>Disk</InstallFrom> | ||
<UpdateEnabled>false</UpdateEnabled> | ||
<UpdateMode>Foreground</UpdateMode> | ||
<UpdateInterval>7</UpdateInterval> | ||
<UpdateIntervalUnits>Days</UpdateIntervalUnits> | ||
<UpdatePeriodically>false</UpdatePeriodically> | ||
<UpdateRequired>false</UpdateRequired> | ||
<MapFileExtensions>true</MapFileExtensions> | ||
<ApplicationRevision>0</ApplicationRevision> | ||
<ApplicationVersion>1.0.0.%2a</ApplicationVersion> | ||
<IsWebBootstrapper>false</IsWebBootstrapper> | ||
<UseApplicationTrust>false</UseApplicationTrust> | ||
<BootstrapperEnabled>true</BootstrapperEnabled> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Drawing" /> | ||
<Reference Include="System.Drawing.Design" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="System.Data.DataSetExtensions" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Net.Http" /> | ||
<Reference Include="System.Xml" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="NativeMethods.cs" /> | ||
<Compile Include="Program.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="App.config" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<BootstrapperPackage Include=".NETFramework,Version=v4.6.1"> | ||
<Visible>False</Visible> | ||
<ProductName>Microsoft .NET Framework 4.6.1 %28x86 和 x64%29</ProductName> | ||
<Install>true</Install> | ||
</BootstrapperPackage> | ||
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1"> | ||
<Visible>False</Visible> | ||
<ProductName>.NET Framework 3.5 SP1</ProductName> | ||
<Install>false</Install> | ||
</BootstrapperPackage> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Img2ColorfulChars | ||
{ | ||
internal class NativeMethods | ||
{ | ||
// Source: visual studio - Custom text color in C# console application? - Stack Overflow | ||
// Answered by Alexei Shcherbakov & Olivier Jacot-Descombes | ||
// https://stackoverflow.com/questions/7937256/custom-text-color-in-c-sharp-console-application | ||
[DllImport("kernel32.dll", SetLastError = true)] | ||
public static extern bool SetConsoleMode(IntPtr hConsoleHandle, int mode); | ||
[DllImport("kernel32.dll", SetLastError = true)] | ||
public static extern bool GetConsoleMode(IntPtr handle, out int mode); | ||
|
||
[DllImport("kernel32.dll", SetLastError = true)] | ||
public static extern IntPtr GetStdHandle(int handle); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Drawing; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace Img2ColorfulChars | ||
{ | ||
internal class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
// Usage | ||
if (args.Length == 0) | ||
{ | ||
Console.WriteLine( | ||
"\nImg2ColorfulChar usage:\n" + | ||
" Img2ColorfulChar.exe example.jpg(Image path) 20([Int]Scale | Larger scale gets smaller image)\n" + | ||
"Thanks~\n"); | ||
return; | ||
} | ||
|
||
// Arguments | ||
string filename = args[0]; | ||
if (!File.Exists(filename)) | ||
{ | ||
Console.WriteLine("Failed: File not found."); | ||
Environment.Exit(-1); | ||
} | ||
bool validScale = int.TryParse(args[1], out int hScale); | ||
if (!validScale) | ||
{ | ||
Console.WriteLine("Failed: Scale should be positive integar."); | ||
Environment.Exit(-2); | ||
} | ||
int vScale = hScale * 2; // Good for console output | ||
|
||
// Set flag ENABLE_VIRTUAL_TERMINAL_PROCESSING(0x4) for colorful output | ||
var handle = NativeMethods.GetStdHandle(-11); | ||
NativeMethods.GetConsoleMode(handle, out int mode); | ||
NativeMethods.SetConsoleMode(handle, mode | 0x4); | ||
|
||
// Draw image | ||
try | ||
{ | ||
using (Bitmap bmp = new Bitmap(filename)) | ||
{ | ||
GetChars(bmp, hScale, vScale, true); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine(e); | ||
} | ||
|
||
Console.ReadKey(); | ||
} | ||
|
||
private static string GetChars(Bitmap bmp, int hScale, int vScale, bool shouldDraw) | ||
{ | ||
StringBuilder sb = new StringBuilder(); | ||
for (int h = 0; h < bmp.Height; h += vScale) | ||
{ | ||
for (int w = 0; w < bmp.Width; w += hScale) | ||
{ | ||
Color color = bmp.GetPixel(w, h); | ||
float brightness = color.GetBrightness(); | ||
char ch = GetChar(brightness); | ||
if (shouldDraw) | ||
{ | ||
// Refer to NativeMethods class | ||
Console.Write($"\x1b[38;2;{color.R};{color.G};{color.B}m{ch}"); | ||
} | ||
sb.Append(ch); | ||
} | ||
if (shouldDraw) { Console.WriteLine(); } | ||
sb.AppendLine(); | ||
} | ||
return sb.ToString(); | ||
} | ||
|
||
private static readonly List<char> listChar = | ||
new List<char>() { ' ', '^', '+', '!', '$', '#', '*', '%', '@' }; | ||
private static char GetChar(float brightness) | ||
{ | ||
int index = (int)(brightness * 0.99 * listChar.Count); | ||
return listChar[index]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// 有关程序集的一般信息由以下 | ||
// 控制。更改这些特性值可修改 | ||
// 与程序集关联的信息。 | ||
[assembly: AssemblyTitle("Img2ColorfulChars")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("Img2ColorfulChars")] | ||
[assembly: AssemblyCopyright("Copyright © 2020")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// 将 ComVisible 设置为 false 会使此程序集中的类型 | ||
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 | ||
//请将此类型的 ComVisible 特性设置为 true。 | ||
[assembly: ComVisible(false)] | ||
|
||
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID | ||
[assembly: Guid("d81c52ac-ca7a-4c10-bc4e-6508677619ab")] | ||
|
||
// 程序集的版本信息由下列四个值组成: | ||
// | ||
// 主版本 | ||
// 次版本 | ||
// 生成号 | ||
// 修订号 | ||
// | ||
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 | ||
// 方法是按如下所示使用“*”: : | ||
// [assembly: AssemblyVersion("1.0.*")] | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,22 @@ | ||
# Img2ColorfulChars | ||
Load image and print colorful chars in C# console. | ||
|
||
### Usage | ||
1. Build this project or use Img2ColorfulChars.exe in _Output. | ||
2. Run command ```Img2ColorfulChars.exe ImagePath Scale``` and enjoy your colorful chars. | ||
eg. | ||
``` cmd | ||
cd _Output | ||
Img2ColorfulChars.exe np.png 4 | ||
``` | ||
> Notice: Scale should be positive integar, and larger scale gets smaller image. | ||
|
||
## Output | ||
Original image: | ||
![Original image](Images/original.png) | ||
|
||
Output: | ||
![Output](Images/output.png) | ||
|
||
|
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.