Txhua lub pixel ntawm ib daim duab bmp nqa cov ntaub ntawv hais txog nws cov xim los ntawm RGB qauv (cov qauv xim ntawm daim duab, uas muaj peb ntu R - liab, G - ntsuab, B - xiav). Nws yooj yim dua yog khaws RGB xim tus nqi nyob rau hauv hex hom (hexadecimal), qhov twg tus nqi ntawm txhua qhov sib txawv yog nyob rau hauv thaj tsam 00… FF. Qhov sib xyaw 000000 sib raug rau xim dub, FFFFFF - kom dawb.
Txhawm rau pib, cia qhib Lub Npe namespace:
siv System. Drawing;
Cia peb tsim ib qho piv txwv tshiab ntawm chav kawm Bitmap:
Bitmap bmp = Bitmap tshiab ("c: / 1.bmp") / / c: / 1.bmp - duab chaw nyob
Txhawm rau khaws cov xim pixel tus nqi, koj tuaj yeem tsim cov qauv cais, uas nws thaj teb yog RGB Cheebtsam, tab sis kuv txiav txim siab siv chav ua xim xim los ntawm Kos duab npe. Txhawm rau kom muaj xim, peb siv txoj kev GetPixel (x, y), qhov x, y yog cov saib xyuas cov duab pixel. Yog tias koj xav siv koj tus qauv los khaws cov xim, ntau dua piv txwv ntawm chav kawm xim, tom qab ntawd koj tuaj yeem siv cov bmp. GetPixel (x, y).x tus qauv kom tau ib qho RGB tivthaiv, qhov twg x yog R, G, lossis B.
Xim [,] xim = xim tshiab [bmp. Width, bmp. Height];
rau (rau cov y = 0; y <bmp. Height; y ++)
rau (rau cov x = 0; x <bmp. Width; x ++)
{
xim [x, y] = bmp. GetPixel (x, y);
}
Txhawm rau sau xim qhov tseem ceeb rau cov ntaub ntawv, peb siv StreamWriter chav kawm. Txij li R, G, B cov ntsiab lus ntawm Cov Xim Piv yog ntawm hom byte, peb hloov lawv siv ToString ("X2") txoj kev mus rau ib txoj hlua hom uas yuav khaws tus hex nqi ntawm RGB cov ntsiab lus.
StreamWriter steamWriter = Cov Tawm Tshiab StreamWriter ("c: / 1.txt");
rau (rau cov y = 0; y <bmp. Height; y ++)
{
rau (rau cov x = 0; x <bmp. Width; x ++)
{
steamWriter. Write (xim [x, y]. R. ToString ("X2"));
steamWriter. Write (xim [x, y]. G. ToString ("X2"));
steamWriter. Write (xim [x, y]. B. ToString ("X2") + ");
}
cov khoom cua sov. WriteLine ();
}
steamWriter. Kaw ();
Tam sim no cia peb nqa tawm cov haujlwm thim rov qab - hloov cov ntawv sib txuas rau cov duab.
Nrog kev pab ntawm StreamReader peb nyeem cov ntaub ntawv los ntawm cov ntawv.
StreamReader txtFile = tshiab StreamReader ("c: / 1.txt");
Peb suav qhov dav thiab qhov siab ntawm daim duab. Txij li thaum txhua lub pixel yuav siv 6 lub cim thiab 1 qhov sib txawv, thiab tsis muaj qhov sib txawv ntawm qhov kawg, peb siv cov qauv hauv qab no los laij qhov dav:
temp = txtFile. ReadLine ();
dav = (temp. Ntev + 1) / 7;
Duab qhov siab yog tus naj npawb ntawm cov kab hauv cov ntawv:
thaum (! txtFile. EndOfStream)
{
txtFile. ReadLine ();
qhov siab ++;
}
qhov siab ++;
Txav mus rau tus nyeem ntawv taw tes rau hauv cov ntawv xa mus rau thaum pib:
txtFile. DiscardBufferedData ();
Tsim qhov ua piv txwv tshiab ntawm chav kawm Bitmap:
Bitmap bmp2 = Bitmap tshiab (dav, qhov siab);
Siv txoj kev sib cais, peb cais cov khoom sib npaug. Peb tshaj tawm peb qhov hloov pauv ntawm byte hom - R, G, B. Siv Cov Txheeb thiab Parse, xaiv cov xim sib cais.
Txhawm rau sau cov pixel hauv cov duab nrog cov xim no, siv SetPixel (j, i, Color. FromArgb (R, G, B)), qhov twg j, kuv yog cov duab ua haujlwm, Color. FromArgb (R, G, B)) yog tus qauv uas tsim cov qauv Xim.
rau (rau cov menyuam = =; i <qhov siab; kuv + +)
{
temp = txtFile2. ReadLine ();
hlua kab nrig = temp. Split ('');
rau (rau cov menyuam j = 0; j <qhov dav; j + +)
{
R = byte. Parse (khoo [j.) Seem (0, 2), System. Globalization. NumberStyles. HexNumber)
G = byte. Parse (khoo [j.) Seem (2, 2),
System. Globalization. NumberStyles. HexNumber)
B = byte. Parse (khoo [j.) Seem (4, 2), System. Globalization. NumberStyles. HexNumber)
bmp2. SetPixel (j, i, Xim. FromArgb (R, G, B));
}
}
Tam sim no koj tuaj yeem txuag cov duab. Yog tias koj ua haujlwm nyob rau hauv WindowsForm koj tuaj yeem siv tus qauv SaveFileDialog:
SaveFileDialog saveImage = tshiab SaveFileDialog ();
saveImage. Filter = bmp files (*. bmp) | *. bmp | Txhua cov ntaub ntawv (*. *)
| *. * ;
saveImage. ShowDialog ();
bmp2. Save (saveImage. FileName);