[译文]ASCII art with C#
2005-07-29 19:48
393 查看
ASCII art with C#[/b]
翻译说明:原文版权归作者所有
原文作者:Daniel Fisher (lennybacon)
原文地址:http://www.codeproject.com/aspnet/ascii_art_with_c_.asp
译 者:zitiger http://zitiger.cnblogs.com/
Download source files - 6.06 Kb
Download VB.NET port - 5.98 Kb [by Paul Farry]
Download demo project - 23.4 Kb
Image _img = Image.FromStream(stream);
Bitmap _image =
new Bitmap(_img, new Size(_img.Width, _img.Height));
_img.Dispose();
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();
_attributes.SetColorMatrix(_matrix);
Graphics gphGrey = Graphics.FromImage(_image);
gphGrey.DrawImage(_image,
bounds,
0,
0,
_image.Width,
_image.Height,
GraphicsUnit.Pixel,
_attributes);
gphGrey.Dispose();
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.
我对图片的像素进行循环,我不想一个ASCII字符代表一个像素,而是代表10/5个(像素).为了让每个
像素效果都能在最终结果的ASCII字符里有所体现,我对代表一个ASCII字符的一组像素进行循环,然后计算它们的亮度和.
(译者:把图片上的所有像素按一定的大小分成N小块,再求出每一小块内所有像素的亮度和.)
for(int y=0; y<10; y++)
{
for(int x=0; x<10; x++)
{
int _cY = y + _startY;
int _cX = x + _startX;
try
{
Color _c = _image.GetPixel(_cX, _cY);
int _b = (int)(_c.GetBrightness() * 10);
_allBrightness = (_allBrightness + _b);
}
catch
{
_allBrightness = (_allBrightness + 10);
}
Finally, I append different ASCII characters based on the calculated amount:
最后,我根据计算出来的和把不同的ASCII字符组合起来.
int _sb = (_allBrightness/10);
if(_sb<25)
{
_asciiart.Append("#");
}
else if(_sb<30)
{
[b]That's all
Thanks to The Code Project and Chris Maunder, newtelligence and greetings to all C# coders out there.
感谢Code Project , Chris Maunder 和 newtelligence,并向所有C#爱好者问好.
译者的理解:
1. 通过Web上传一张图片;
2. 把图片进行灰度化(变成黑白);
3. 把图片划分成N个小块,对每个小块的内像素的亮度进行求和;
4. 根据每个小块的亮度平均值来选择合适的ASCII字符进行组合;
5. 输出.
欢迎杭州的朋友加入杭州.net俱乐部http://zitiger.cnblogs.com/archive/2005/07/28/201584.html.
另:马上大四了,找个实习单位,个人简历http://zitiger.cnblogs.com/archive/2005/07/26/200648.html,限杭州.
翻译说明:原文版权归作者所有
原文作者:Daniel Fisher (lennybacon)
原文地址:http://www.codeproject.com/aspnet/ascii_art_with_c_.asp
译 者:zitiger http://zitiger.cnblogs.com/
Download source files - 6.06 Kb
Download VB.NET port - 5.98 Kb [by Paul Farry]
Download demo project - 23.4 Kb
Image _img = Image.FromStream(stream);
Bitmap _image =
new Bitmap(_img, new Size(_img.Width, _img.Height));
_img.Dispose();
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();
_attributes.SetColorMatrix(_matrix);
Graphics gphGrey = Graphics.FromImage(_image);
gphGrey.DrawImage(_image,
bounds,
0,
0,
_image.Width,
_image.Height,
GraphicsUnit.Pixel,
_attributes);
gphGrey.Dispose();
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.
我对图片的像素进行循环,我不想一个ASCII字符代表一个像素,而是代表10/5个(像素).为了让每个
像素效果都能在最终结果的ASCII字符里有所体现,我对代表一个ASCII字符的一组像素进行循环,然后计算它们的亮度和.
(译者:把图片上的所有像素按一定的大小分成N小块,再求出每一小块内所有像素的亮度和.)
for(int y=0; y<10; y++)
{
for(int x=0; x<10; x++)
{
int _cY = y + _startY;
int _cX = x + _startX;
try
{
Color _c = _image.GetPixel(_cX, _cY);
int _b = (int)(_c.GetBrightness() * 10);
_allBrightness = (_allBrightness + _b);
}
catch
{
_allBrightness = (_allBrightness + 10);
}
Finally, I append different ASCII characters based on the calculated amount:
最后,我根据计算出来的和把不同的ASCII字符组合起来.
int _sb = (_allBrightness/10);
if(_sb<25)
{
_asciiart.Append("#");
}
else if(_sb<30)
{
[b]That's all
Thanks to The Code Project and Chris Maunder, newtelligence and greetings to all C# coders out there.
感谢Code Project , Chris Maunder 和 newtelligence,并向所有C#爱好者问好.
译者的理解:
1. 通过Web上传一张图片;
2. 把图片进行灰度化(变成黑白);
3. 把图片划分成N个小块,对每个小块的内像素的亮度进行求和;
4. 根据每个小块的亮度平均值来选择合适的ASCII字符进行组合;
5. 输出.
欢迎杭州的朋友加入杭州.net俱乐部http://zitiger.cnblogs.com/archive/2005/07/28/201584.html.
另:马上大四了,找个实习单位,个人简历http://zitiger.cnblogs.com/archive/2005/07/26/200648.html,限杭州.
相关文章推荐
- Drawing with ASCII art
- Using Weka and Libsvm with C# in Visual Studio
- BC#53 1001 Rikka with Graph
- ASCII Art—使用纯文本来显示图像:综述
- Setting NTFS Permissions with C#
- LeetCode #11 Container With Most Water C# Solution
- Essential ASP.NET With Examples in C#
- Timer Objects in Windows Services with C#.NET
- C# mysql 链接 遇到 异常 Authentication with old password no longer supported, use 4.1 style passwords.
- SHA1 Encoding with C#
- C#: Create a WebRequest with HTTP Basic Authentication
- C#3.5技术探讨(7) :Linq To Object with Lambda Expressions
- C# ASCII与字符串间相互转换
- Send Meeting Request With C#
- Computer Talker with C# z
- Effective C# Item13: Initialize Static Class Members with Static Constructiors
- C#热图生成(一)——with .NET 2.0
- Deep Learning for Computer Vision with MATLAB and cuDNN(译文)
- XmlRpc with C#/Java【转】
- C#: Unsafe code may only appear if compiling with /unsafe