C++ C++ C# C# ASP.NET Security ASP.NET Security ASM ASM Скачать Скачать Поиск Поиск Хостинг Хостинг  
  Программа для работы с LPT портом...
Язык: .NET — ©Alexey...
  "ASP.NET Atlas" – AJAX в исполнении Micro...
Язык: .NET — ©legigor@mail.ru...
  "Невытесняющая" Многопоточность...
Язык: C/C++ — ©...
  Update World C++: Сборник GPL QT исходников
  Весь сайт целиком можно загрузить по ссылкам из раздела Скачать
Дебетовая карта Home Credit [CPS] RU

 ASCII art with C# / Images and multimedia / ASP. NET

Sample Image - ascii_art_with_c_.jpg


Behind the scenes: I like C# and I like ASCII art. So I asked myself if somebody has written some image to ASCII application in C#. I Googled but found nothing. I did image manipulation stuff for my company and so I decided to build a basic image-to-ASCII conversion library in C#.

You can see a running sample here.

Step 1: The Upload Form.

To keep it quite simple, I just check the content type and let my library do the rest.

if(File1.PostedFile.ContentType=="image/gif" ||
    File1.PostedFile.ContentType=="image/jpg" ||
    File1.PostedFile.ContentType=="image/jpeg" ||
    File1.PostedFile.ContentType=="image/pjpeg" ||
        Output.Text = "<xmp>" +
         File1.PostedFile.InputStream) +

Step 2: The Library

The first thing I do is of course load the image:

Image _img = Image.FromStream(stream);
Bitmap _image = 
 new Bitmap(_img, new Size(_img.Width, _img.Height));

Next I grayscale the image - you'll see later why.

Rectangle bounds = 
 new Rectangle(0, 0, _image.Width, _image.Height);

ColorMatrix _matrix = new ColorMatrix();
_matrix[0,0] = 1/3f;
_matrix[0,1] = 1/3f;
_matrix[0,2] = 1/3f;
_matrix[1,0] = 1/3f;
_matrix[1,1] = 1/3f;
_matrix[1,2] = 1/3f;
_matrix[2,0] = 1/3f;
_matrix[2,1] = 1/3f;
_matrix[2,2] = 1/3f;

ImageAttributes _attributes = 
 new ImageAttributes();

Graphics gphGrey = Graphics.FromImage(_image);


O.K. Now, we get to the interesting part.

for(int h=0; h<_image.Height/10; h++)
    int _startY = (h*10);

    for(int w=0; w<_image.Width/5; w++)
         int _startX = (w*5);
         int _allBrightness = 0;

I loop through the image's pixels and because I don't want one ASCII character per pixel, I take one per 10/5. To let every pixel influence the resulting ASCII char, I loop them and calculate the brightness of the amount.

for(int y=0; y<10; y++)
    for(int x=0; x<10; x++)
        int _cY = y + _startY;
        int _cX = x + _startX;
            Color _c = _image.GetPixel(_cX, _cY);
            int _b = (int)(_c.GetBrightness() * 10);
            _allBrightness = (_allBrightness + _b);
            _allBrightness = (_allBrightness + 10);

Finally, I append different ASCII characters based on the calculated amount:

int _sb = (_allBrightness/10);
else if(_sb<30)

That's all

Thanks to The Code Project and Chris Maunder, newtelligence and greetings to all C# coders out there.

Дебетовая карта Home Credit [CPS] RU