日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

[译文]ASCII art with C#

發布時間:2025/7/25 C# 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [译文]ASCII art with C# 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ASCII art with C#<?xml:namespace prefix = o />

??? 翻譯說明:原文版權歸作者所有

??? 原文作者:Daniel Fisher (lennybacon)

??? 原文地址:http://www.codeproject.com/aspnet/ascii_art_with_c_.asp

??? ?????? :zitiger http://zitiger.cnblogs.com/

[by Paul Farry]

<?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 藝術,所以試圖知道是不是已經有人使用C#寫了把圖像轉換成ASCII的應用.Google了一下,沒有發現.在公司我從事圖像處理素材,所以我決定寫一個把圖像轉換成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.

簡單起見,我只檢查了上傳文件的類型,其他的就交給我的類庫去完成了.

if(File1.PostedFile.ContentType=="image/gif"?||
????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:

首先,當然是加載圖片.

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)
{
????



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,限杭州.

轉載于:https://www.cnblogs.com/zitiger/archive/2005/07/29/203083.html

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的[译文]ASCII art with C#的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。