[译文]ASCII art with C#
ASCII art with C#<?xml:namespace prefix = o />
??? 翻譯說明:原文版權(quán)歸作者所有
??? 原文作者:Daniel Fisher (lennybacon)
??? 原文地址:http://www.codeproject.com/aspnet/ascii_art_with_c_.asp
??? 譯?????? 者:zitiger http://zitiger.cnblogs.com/
<?xml:namespace prefix = v />
Introduction (介紹)
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#.
背景:我喜歡C#和ASCII 藝術(shù),所以試圖知道是不是已經(jīng)有人使用C#寫了把圖像轉(zhuǎn)換成ASCII的應(yīng)用.我Google了一下,沒有發(fā)現(xiàn).在公司我從事圖像處理素材,所以我決定寫一個把圖像轉(zhuǎn)換成ASCII的基本類庫.
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.
簡單起見,我只檢查了上傳文件的類型,其他的就交給我的類庫去完成了.
????File1.PostedFile.ContentType=="image/jpg"?||
????File1.PostedFile.ContentType=="image/jpeg"?||
????File1.PostedFile.ContentType=="image/pjpeg"?||
????File1.PostedFile.ContentType=="image/bmp")
???{
????????Output.Text?=?"<xmp>"?+
????????StaticDust.AsciiArt.ConvertImage(
?????????File1.PostedFile.InputStream)?+
????????"</xmp>";
???}
???else
???{
????????
Step 2: The Library(第二步 類庫)
The first thing I do is of course load the image:
首先,當然是加載圖片.
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.
好了,現(xiàn)在我們就要進入最有意思的部分了.
{
????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.
我對圖片的像素進行循環(huán),我不想一個ASCII字符代表一個像素,而是代表10/5個(像素).為了讓每個
像素效果都能在最終結(jié)果的ASCII字符里有所體現(xiàn),我對代表一個ASCII字符的一組像素進行循環(huán),然后計算它們的亮度和.
(譯者:把圖片上的所有像素按一定的大小分成N小塊,再求出每一小塊內(nèi)所有像素的亮度和.)
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:
最后,我根據(jù)計算出來的和把不同的ASCII字符組合起來.
if(_sb<25)
{
????_asciiart.Append("#");
}
else?if(_sb<30)
{
????
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個小塊,對每個小塊的內(nèi)像素的亮度進行求和;
4.???????? 根據(jù)每個小塊的亮度平均值來選擇合適的ASCII字符進行組合;
5.???????? 輸出.
?
?
歡迎杭州的朋友加入杭州.net俱樂部http://zitiger.cnblogs.com/archive/2005/07/28/201584.html.
另:馬上大四了,找個實習單位,個人簡歷http://zitiger.cnblogs.com/archive/2005/07/26/200648.html,限杭州.
轉(zhuǎn)載于:https://www.cnblogs.com/zitiger/archive/2005/07/29/203083.html
《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的[译文]ASCII art with C#的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 想吃混沌
- 下一篇: 用滚轮实现图片缩放〔zz〕