[code]using System;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
namespace ASCII_Generator
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Select input file";
ofd.Filter = "Bitmap|*.bmp|Graphics Interchange Format|*.gif|Joint Photo Experts Group|*.jpg|Portable Network Graphics|*.png|Icon|*.ico";
string shades = @".-=*~^|?5X%&@#";
string[,] strImage;
if (ofd.ShowDialog() == DialogResult.OK)
{
Bitmap image = new Bitmap(ofd.FileName);
// Iterate over each chunk
int ChunkSize = 4;
strImage = new string[image.Width/ChunkSize, image.Height/ChunkSize];
strImage.Initialize();
for (int x = 0; x < image.Width / ChunkSize; x++)
{
float avs = 0;
for (int y = 0; y < image.Height / ChunkSize; y++)
{
// Greyscale and save the pixel value
int[] values = new int[ChunkSize*ChunkSize];
for (int cx = 0; cx < ChunkSize; cx++)
{
for (int cy = 0; cy < ChunkSize; cy++)
{
int red = image.GetPixel(cx, cy).R;
int green = image.GetPixel(cx, cy).G;
int blue = image.GetPixel(cx, cy).B;
int average = (red + green + blue) / 3;
values[cx * ChunkSize + cy] = average;
}
}
// Get average shade
float avShade = 0.0f;
foreach (int shade in values)
{
avShade += shade;
}
avShade /= values.Length;
// Scale it down to fit
avShade = avShade / (255 / shades.Length);
avShade = shades.Length - avShade - 1;
avs = avShade;
// Put it into the array
strImage[x, y] = shades[Convert.ToInt32(avShade)].ToString();
}
}
// Now iterate through the strImage array and paste it into a file
string final = "";
for (int x = 0; x < strImage.GetLength(0); x++)
{
for (int y = 0; y < strImage.GetLength(1); y++)
{
final += strImage[x,y];
}
final += "\r\n";
}
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Text|*.txt";
if (sfd.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(sfd.FileName, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
sw.Write(final);
sw.Close();
}
}
}
}
}[/code]
Sorry about the really ugly code and redundant lines, I'm really tired and I can't work this out properly.
Any kind soul mind pointing out my mistakes? Thanks.
1. You're just accessing the pixels x to ChunkSize, y to ChunkSize; see your inner-most loop.
2. To scale the shade down do avShade /= 255 / (shades.Length - 1);
Edit: I actually didn't notice that you had to invert there, since your darkest character is on index 0 and your brightest on shades.Length - 1.
So do avShade = shades.Length - avShade * ((shades.Length - 1) / 255.0f) - 1; or flip your array around.
3. You'll notice that the resulting picture is flipped.
4. I'd use FileMode.Create instead of FileMode.OpenOrCreate; at least I'd wanted the file to be overridden.
Edit:
Buttscratcher?
[code]~~~~~~~~~~~~~~~~~~~~~|5?||||||?55????5?|||||||||||||||||?55???5?||||||||||||||?X5?????555|||||||~~~~~~~~~~~~~~~~^**************
~~~~~~~~~~~~~~~~~~~~~|5?||||||?55????5?|||||||||||||||||?55???5?||||||||||||||5X5???5?555||||||^~~~~~~~~~~~~~~~~^**************
~~~~~~~~~~~~~~~~~~~~~|5?||||||?55????5?|||||||||||||||||?55???5?||||||||||||||5X5???5?555||||||^~~~~~~~~~~~~~~~~^**************
~~~~~~~~~~~~~~~~~~~~~|5?||||||?55????5?|||||||||||||||||?55???5?||||||||||||||5X5???5?555||||||^~~~~~~~~~~~~~~~~^**************
~~~~~~~~~~~~~~~~~~~~~|5?||||||?55????5?|||||||||||||||||?55???5?||||||||||||||?X5???5?555|||||||~~~~~~~~~~~~~~~~^**************
~~~~~~~~~~~~~~~~~~~~~|5?|||||||55????5?|||||||||||||||||?5555?5?||||||||||||||?X5???5?555|||||||~~~~~~~~~~~~~~~~~**************
~~~~~~~~~~~~~~~~~~~~~|5?|||||||55????5?|||||||||||||||||?5555?5?||||||||||||||?X5???5?555|||||||~~~~~~~~~~~~~~~~~**************
~~~~~~~~~~~~~~~~~~~~~|5?|||||||55????5?|||||||||||||||||?555555?||||||||||||||?55???5?555|||||||~~~~~~~~~~~~~~~~~**************
~~~~~~~~~~~~~~~~~~~~~|5?|||||||55????5?|||||||||||||||||?55?555?||||||||||||||?55?????555||||||^~~~~~~~~~~~~~~~~~**************
~~~~~~~~~~~~~~~~~~~~~|5?|||||||55????5?|||||||||||||||||?55?555?||||||||||||||?55?????555|||||||~~~~~~~~~~~~~~~~~**************
~~~~~~~~~~~~~~~~~~~~~|5?|||||||55555?5?|||||||||||||||||?55?555?||||||||||||||?55?????555|?5555?^~~~~~~~~~~~~~~~~**************
~~~~~~~~~~~~~~~~~~~~~|5?|||||||55555?5?|||||||||||||||||?55?5?5?||||||||||||||?55?????5X%%%%%%%%%X5^~~~~~~~~~~~~^**************
~~~~~~~~~~~~~~~~~~~~~|5?||||||?55??555?|||||||||||||||||?55?5?5?||||||||||||||?55?555X%%%%%%%%%%%%%%5^~~~~~~~~~~^**************
~~~~~~~~~~~~~~~~~~~~~|5?||||||?X5555????||||||||||||||||?55?5?5?||||||||||||||?X5??X%%%%%%%%%%%%%%%%%X|~~~~~~~~~^**************
~~~~~~~~~~~~~~~~~~~~~|5?|||||???||??^^^|??||||||||||||||?55?5?5?||||||||||||||?X55X%%%%%%%%%%%%%%%%%%%X|~~~~~~~~^**************
~~~~~~~~~~~~~~~~~~~~~|5?||||??|^^^^|||^|^?5|||||||||||||?55?5?5?||||||||||||||?X5X%%%%%%%%%%%%%%X5%%%^~?^~~~~~~~^**************
~~~~~~~~~~~~~~~~~~~~~|5?|||?||^^^^^^|||^^|??||||||||||||?55?5?5?||||||||||||||?XX%%X555XXX%%%X5|~~%&5~~^|^~~~~~~^**************
~~~~~~~~~~~~~~~~~~~~~|5?||?|^^^^^^^^??||^|^??|||||||||||?55?5?5?||||||||||||||5X&X^~*****~^^^^***^&%?~~^*|~~~~~~^**************
~~~~~~~~~~~~~~~~~~~~~|5?|?|^^^^^^^||||||^^|^??||||||||||?55???5?||||||||||||||?X5^*~~****~=..-*~^^55~*~**~^~~~~~^**************
~~~~~~~~~~~~~~~~~~~~~|5???^^^^|^^^^|^*^||||||?||||||||||?55??55?||||||||||||||??=...=~**~-.....^~********=^^~~~~^**************
~~~~~~~~~~~~~~~~~~~~~|55?^|^^^|^^^^|****^^??^|?|||||||||?55??55?||||||||||||||?*.....*^~*......*~=*********|~~~~^**************
~~~~~~~~~~~~~~~~~~~~~|55|^^|^^^^|^|~***=*=*||^??||||||||?55???5?|||||||||||||^?-.....=~~*......=~**********~^~~~~**************
~~~~~~~~~~~~~~~~~~~~~|5|^|||^^^^||~=***=~^^|^||?||||||||?55???5?||||||||||||||?|-....=**%*-----*************|~~~~**************
~~~~~~~~~~~~~~~~~~~~~??^|^||||||^~****==~^***~?X?|||||||?55???5?||||||||||||||?X*=*~*^^*?^*~~*~^************^^~~~**************
~~~~~~~~~~~~~~~~~~~~^?|^^^^|?^~*~~*~^~=*~****^%??|||||||?55???5?|||||||||||||~*^^^^*****~^~***~**************|~~~**************
~~~~~~~~~~~~~~~~~~~~||^^^^^^|*=*~****|**^*-...|||?||||||?55???5?||||||||||?|****~|*******~~~~~***************^^~^**************
~~~~~~~~~~~~~~~~~~~~?|^^^^^|^**^*~~*=?~=~=...-|||5||||||?55??55?||||||||||?*****~~***************************~|~^**************
~~~~~~~~~~~~~~~~~~~^|^^^^^^|**|?*-...=~=***-=~*^||?|||||?55??55?|||||||||?~*****~^*****~**********************^^^**************
~~~~~~~~~~~~~~~~~~~|^^^^^^||***~-....****=*^~*=*||?|||||?55???5?||||||||||*******^^**~^^**********************~^^**************
~~~~~~~~~~~~~~~~~~^?^^^^^^|~*==*~=--=~*****~^****|?|||||?55???5?|||||||||^********^^^^~***********************~|~**************
~~~~~~~~~~~~~~~~~~||^|^^^|?**=***~~~*=***=*=~~=*=~??||||?55???5?|||||||||~******~~****~~~~^~~******************|^**************
~~~~~~~~~~~~~~~~~~||||^^^||******=*=*********^*=**??||||555???5?|||||||||*******^|~~~~~************************^|**************
~~~~~~~~~~~~~~~~~~?^|^^^^|^=*****************~~=**^5||||5555??5?|||||||||*******^******************************~|**************
~~~~~~~~~~~~~~~~~^|^^^^^^|~******************~*****??|||5555??5?|||||||||*******~~******************************^**************
~~~~~~~~~~~~~~~~~^|^^^^^^|***=***************^*=***|?|||5555??5?||||||||?********^~*****************************^**************
~~~~~~~~~~~~~~~~~||^^^^^||***=**==*******=**~~=****|?|||555???5?||||||||?*********^*****************************^~*************
~~~~~~~~~~~~~~~~~?|^^^^^||***=*===*******==*^**=***|?|||555???5?||||||||?*********^*****************************~~*************
~~~~~~~~~~~~~~~~~?|^^|^^|^*****===********=~*=*=***|?|||555???5?||||||||?~*******~~*****************************~^*************
^^~~~~~~~~~~~~~~~||^^^^^|^****************~~=****=*^?|||555???5?||||||||?~******~~***********~~******************^*************
~^^~~~~~~~~~~~~~~||^|^^^|~=*****==****===*~*==*=*=*|5|||555???5?||||||||?~******^*************~******************|~************
**^^~~~~~~~~~~~~~||^|||||~=*=****=*****=****==*=*=~??|||555??55?||||||||?^******^******~*****~~=*****************|^************
***^^~~~~~~~~~~~~||||^||?^===***==********=~^*****^??|||555?5555|||||||||^******^*=***~|*==**^*********************~***********
****^^~~~~~~~~~~~||^|^^|XX|***********=*^||X5==**~??||||5X55??55|||||||||^******~^***~^~^~~~^*******************~--^***=*******
****~|~~~~^^^^^^^^?^|^||XX?************?XXX5^=***|5?||||5X5?5?55||||^^~~~^*******~~^^~***~~~*******************~*..~|^~*****~~~
****~^|^^^~~~~**~*^|^|^^?X^=**********5XXXX^**=*|??|||||5X55??||^~~~*~*~~^*************************************~..-*-*^^~~*^^~~
***~^^~~********~~~||^||^?|*******=*=~XX55|****|?|~~~^??5X?|~~~**~***~~*~|************************************~-..*-..-~^^|^~**
***~^*~~***********~||^^^|||~*****==***~~~*=**|?|~****~^?^~~**********~~~************************************^=..-*.....*^^~***
***|~*~****~~~~~~~~~^??|^^|||^**====*****=**~??^~~~~~**^~~*********~~~*~*-~*********************************^=...*.......~^~***
**^^*~*~~~~~~~^^^^^^^^|??|^^^||^*====**=*=~|?|~*~***~*~^~~~*****~**~~~~^*.=~*******************************^=...=-........*~***
*~^~~~~~^^^^^^^^^^^^^^^^|???|^||||^*=***=*?|^~~*****~*~^~~~~~*~~~~~~^^|~*..~******************************~=...==..........~~~~
~^^~~^^^^^^^^^^^^^^^^^^^^^^||????5X^=====*?^^^~~~~****^~~*~~~~^^^^^^^^^.-=.-~***************************~~-...-*...........=^~^
~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|?^=====~||^^^^^^~~~~^~~~^^^^^^^^^^^^-..=-.-^*************************~*....-*.............~^^
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|??^=***~|^?|^^^^^^^~^^^^^^^^^^^^^^^|=...-=..-~**********************~~=....=*..............=|^
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^??|^==*^||||^||^^^^^^|^^^^^^^^^^^^^|~.....=-..-~~******************~~*-....==-..............-|^
|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|??||*~||||?|^^|||^^^^?^^^^^^^^^^^^^|-..-...*-...*~***************~~*-.....==.................~^
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|||??||??||??|||^^||^^||^^^^^^^^^^^^^~...=-..-*....=~~***********~*=-.....-*-..................*^
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^||^?5??|^||^??^|||||?|^|^^^^^^^^^^^^^|-...=-...-=.....=^*******~~*-.......=*-...................-|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^||^||?555|||^?5||^||||?||^^^^^^^^^^^^|~....=-....==.....=^***~~*-........-=~-.....................^
^^^^^^^^^^^^^^^^^^^^^^^^^^^^|^|?|?|||||||??5|?|||^|5?^^^^^^^^^^^^|=....*......==...==*~~*-==........-*-=-.....................~
^^^^^^^^^^^^^^^^^^^^^^^^^^^??|?||?55?^||???|????|||5?^^^^^^^^^^^^^....-=.......-=.-=.-^*...=-.....-==.-=----..................~
^^^^^^^^^^^^^^^^^^^^^^^^||^?|^~~|?X||??^?|5???||??|??||^^^^^^^^^|*....=-....-==-=**..-^....-*...-=*-..=*=---==--..............~
^^^^^^^^^^^^^^^^^^^^^^|?^*========^|~||55??|????|||^||???||^^^^^|-....=..-********=-=-=.....*-.==-..-=*=......-==-............*
^^^^^^^^^^^^^^^^^^^|?^**===========*=*=*^||^||||||????|||||||??|^....-=-***=*****~*--=-.....=*=-...-=*--........-=-...........*
^^^^^^^^^^^^^^^^^^|?~==================-*^??||||^|^||5?|???|||???*=-.*~************~*~*=-----=----=*----..........-=..........*
^^^^^^^^^^^^^^^^^|^*================-======~|||||||^^||^^|||??||||^^^?|~************~~*~~~*~*~~**~~~*~**~*-........-=.........*
^^^^^^^^^^^^^^^^?^===================--=====~?|^^|^^^||^^^~^^||?|^^||||||^|^***************==***************-.......==........~
^^^^^^^^^^^^^^^^?===~^****==================*^5|^|^^^|?|^^^^^^|*--=~??||||^~*********************************-.......=........~
^^^^^^^^^^^^^^^|*==~|****^===========*========|?||||||||^^^^^^=....~^**^|^***********************************~-......-=.......^
^^^^^^^^^^^^^^^~===^|====~*=========*^~*~^~====||^||||||^^^^|~=-..-~~******************************************.......*-.....-~
^^^^^^^^^^^^^^~=====......*=-=======X|****^*===~|^|||||?^^^^|*-...********************************************~=......-=.....=-
^^^^^^^^^^^^^|~====*......*========*~=...-=*===*5||||||||^^|~....==-~******************************************~.......=.....=.
^^^^^^^^^^^^^^|~===*-....-*==*=====*.......*====^???||???^^|---==*..********************************************-......=....--.
^^^^^^^^^^^^^^^|====*-..-*=-*~=====*-.....-~====~?||^^^|?|^|^~~*~-..-~******************************************=......--...=-.
^^^^^^^^^^^^^^^|~====****===**=====*=.....**=====~^||^^||?^|^**~=....-^~~~***************************************......--...=..
^^^^^^^^^^^^^^^^|=================-=*=..-=~======*^|||^||?||***~-.....=^~~**************************************~......-=..=-..
^^^^^^^^^^^^^^^^|^=====-====^*=======***~*======~|?|||?||??~**~=.......****************************************=~......-=..=...
^^^^^^^^^^^^^^^^^|~=========~***===============*??~****^|||=**~-.......-*~**************************************~......-=.==...
^^^^^^^^^^^^^^^^^^|~==========================*|?^=*****|?~**~*..........=~|~~**********************************~......=--=....
^^^^^^^^^^^^^^^^^^^|^========================~|||******=?^***~-............*************************************~......=-=-....
^^^^^^^^^^^^^^^^^^^^|^*===================-=^|||~*=*==*~?****~.............-=~~~*~~**~~**************************......==-.....
^^^^^^^^^^^^^^^^^^^^^|~*==================~|||||***===*?~*****.............-.*-----..-=~~***********************=.....-~=......
^^^^^^^^^^^^^^^^^^^^^~=*~~*=============~^|||||*=*====||****~=............-*-=.........-=*~~*******************~-.....**.......
^^^^^^^^^^^^^^^^^^^^^***=^|5|~*=******^|||^^^|~=*=***^?*****~-.............-=-...........--*~*******************..--.=~-.......
^^^^^^^^^^^^^^^^^^^^^=***~|X^***^55~**^?|||||~=*****^?~*****~-..............=...............-=*************=***-..=-=*.........
^^^^^^^^^^^^^^^^^^^|~=**~~?X~===^%?=***|~****==**==^?^******~-..............=..................-=*~~~~*******~=..===*-.........
^^^^^^^^^^^^^^^^^^^|**~^~*^^|~~~?5?=***~~*****=***|?|*******~-..............*......................-==****~^~=..=~==...........
^^^^^^^^^^^^^^^^^^^|*~~?^^||%X5X5~^***=~~=******~|5?********~-..............=............................--======..............
^^^^^^^^^^^^^^^^^^^?^^^?XXXXX55X5??~~*=*~*=**=*^||5~**********.............-=.................................-................
^^^^^^^^^^^^^^^^^^|?|||XXX5XX5XXXXX?~***~*=**~|||?^**********~.............-=..................................................
^^^^^^^^^^^^^^^^^^|**~^|XXX?|^^XX555^==*^*=*^?|||^***********~=............-=..................................................
^^^^^^^^^^^^^^^^^|~***^~XX|~*~~^XX5X|~~^^*~^^||||************^5-............*..................................................
^^^^^^^^^^^^^^^^^|~***~*55~***^*5XXX?~*~|^~~~~?|^********~**=^@|............=................................................=*
^^^^^^^^^^^^^^^^^|~***^~X?****~*555XX|^|~~~~~~|?*********^***^@@?-..=.....===................................................|&
^^^^^^^^^^^^^^^||?^^^^^~X?~~~*~*5X5XX?^~~~~~~*^|*^**^****^***|&@#%~--*....=-==..............................................*&#
^^^^^^^^^^^^^||~~^~*~~~^X?~^^^^*5XX?^~~~~~~~~~^^*^**^****|~*~X&&@#@5|^~-.....*.............................................~&##
^^^^^^^^^^^||^~*~~~**~~?5|***~~~X5|~~~~~~~~~~~^^*^**^****55|X&&&&@@%&@@5=....=-...............................-..........=|@##@
^^^^^^^^^|?|~~~~~~^~~^^|^|~**~~?|~~~~~~~~~~~~~^|*^**^***^%&&&&&&%&&&@###&^-..-*.............................-*-.......-*|X@##@&
^^^~~~~~~^^~~~~~~~~^^^^^^^^~~|^^~~~~~~~~~~~~~~|?*^**~~*~X&&&&&&&&&&&@####@%^*=**..........................-==....--=~?%@&&@@&&&[/code]
[QUOTE=ZeekyHBomb;22010247]1. You're just accessing the pixels x to ChunkSize, y to ChunkSize; see your inner-most loop.
2. To scale the shade down do avShade /= 255 / (shades.Length - 1);
Edit: I actually didn't notice that you had to invert there, since your darkest character is on index 0 and your brightest on shades.Length - 1.
So do avShade = shades.Length - avShade * ((shades.Length - 1) / 255.0f) - 1; or flip your array around.
3. You'll notice that the resulting picture is flipped.
4. I'd use FileMode.Create instead of FileMode.OpenOrCreate; at least I'd wanted the file to be overridden.
Edit:
Buttscratcher?[/QUOTE]
Thanks for all that; I'll add it all in now.
-Edit-
New code
[code]using System;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
namespace ASCII_Generator
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Select input file";
ofd.Filter = "Bitmap|*.bmp|Graphics Interchange Format|*.gif|Joint Photo Experts Group|*.jpg|Portable Network Graphics|*.png|Icon|*.ico";
string shades = @".-=*~^|?5X%&@#";
string[,] strImage;
if (ofd.ShowDialog() == DialogResult.OK)
{
Bitmap image = new Bitmap(ofd.FileName);
// Iterate over each chunk
int ChunkSize = 4;
strImage = new string[image.Width/ChunkSize + 1, image.Height/ChunkSize + 1];
strImage.Initialize();
for (int x = 0; x < image.Width; x+= ChunkSize)
{
float avs = 0;
for (int y = 0; y < image.Height; y += ChunkSize)
{
// Greyscale and save the pixel value
int[] values = new int[ChunkSize*ChunkSize];
for (int cx = 0; cx < ChunkSize; cx++)
{
for (int cy = 0; cy < ChunkSize; cy++)
{
int red = image.GetPixel(cx, cy).R;
int green = image.GetPixel(cx, cy).G;
int blue = image.GetPixel(cx, cy).B;
int average = (red + green + blue) / 3;
values[cx * ChunkSize + cy] = average;
}
}
// Get average shade
float avShade = 0.0f;
foreach (int shade in values)
{
avShade += shade;
}
avShade /= values.Length;
// Scale it down to fit
avShade = shades.Length - avShade * ((shades.Length - 1) / 255.0f) - 1;
avs = avShade;
// Put it into the array
strImage[x/ChunkSize, y/ChunkSize] = shades[Convert.ToInt32(avShade)].ToString();
}
}
// Now iterate through the strImage array and paste it into a file
string final = "";
for (int x = 0; x < strImage.GetLength(0); x++)
{
for (int y = 0; y < strImage.GetLength(1); y++)
{
final += strImage[x,y];
}
final += "\r\n";
}
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Text|*.txt";
if (sfd.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(sfd.FileName, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(final);
sw.Close();
}
}
}
}
}[/code]
Still isn't working. I haven't flipped the image back around yet; I'll do that when I get the rest of it working.
Not sure what's stopping it now, because stepping through this thing with a debugger is a nightmare
You inner-most loop still only accesses the pixels 0 to ChunkSize, 0 to ChunkSize :P
[QUOTE=ZeekyHBomb;22018969]You inner-most loop still only accesses the pixels 0 to ChunkSize, 0 to ChunkSize :P[/QUOTE]
Bugger, I see it now XD
No worries though, I re-wrote it so it's a lot more efficient (at least, memory wise).
What do you think of this new code?
[code]using System;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
namespace ASCII_Generator
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Select input file";
ofd.Filter = "Support image files|*.bmp;*.gif;*.jpg;;*.jpeg;*.png;*.ico";
string shades = @" .-=*~^|?5X%&@#";
if (ofd.ShowDialog() == DialogResult.OK)
{
// Choose a save location and setup the file stream
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Text files|*.txt|All files|*.*";
string file = "";
if(sfd.ShowDialog() == DialogResult.OK)
file = sfd.FileName;
else
return;
FileStream fs = new FileStream(file, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Flush();
Bitmap image = new Bitmap(ofd.FileName);
float scale = 0.3f;
// Iterate over each pixel
for (float fy = 0; fy < (float)image.Height; fy += (1.625f * (1/scale)))
{
int y = Convert.ToInt32(fy);
if(y >= image.Height) y = image.Height-1;
string line = "";
for (float fx = 0; fx < (float)image.Width; fx += 1.0f * (1/scale))
{
int x = Convert.ToInt32(fx);
if(x >= image.Width) x = image.Width-1;
float avShade = (image.GetPixel(x,y).R + image.GetPixel(x,y).G + image.GetPixel(x,y).B)/3;
// Scale it down to fit
avShade = shades.Length - avShade * ((shades.Length - 1) / 255.0f) - 1;
line += shades[Convert.ToInt32(avShade)];
}
sw.WriteLine(line);
}
sw.Close();
}
}
}
}[/code]
Working pretty awesome, I'm gonna add some more features, then release it :)
I'm not sure why you're using a float in the loops to cast it to an integer and use it to access the pixels. You're loosing some resolution here as you do not take neighbor pixels into account.
And I think you don't need to sw.Flush().
And maybe you've seen in my fork that Color actually has a member to access the brightness.
[editline]06:40PM[/editline]
And you were saying, in the WAYWO thread, that you want to add a config-file. I'd just make a small GUI you can set the scale 'n stuff in.
You could also add the option to output colored HTML as seem in the C# ascii thread ;)
Alright, GUI it is :)
Also, I used the float as a quick way of getting a (1.625/1.000) scale on the output. Otherwise the image gets stretched downwards a fair amount. I chose 1.625:1 because that's the ratio of height to width on the Courier font, as close as I could measure!
Alright, that makes sense. Scaling using integer is indeed limiting.
But you should still take neighbor pixels into account when calculating brightness, so when you had a scale of 0.5 that for the first character 50% of pixel 0 and 50% of pixel 1 is used, or for a scale of 0.3 70% of pixel 0 and 30% of pixel 1 (for the next character it'd be 40% of p0 and 60% of p1) and so on
So it smooths out the images somewhat? Interesting...
I'll look into it!
Sorry, you need to Log In to post a reply to this thread.