0

I help to write one sentence in one bitmap. I managed to do with

string lien = "C:/KMP_WT/Resources/bitmap/test.bmp";
Bitmap bmp = new Bitmap(W,H);
bmp.SetResolution(300, 300);
using (Graphics graphics = Graphics.FromImage(bmp))
        {
            Font font = new Font("Arial", 12);
            graphics.FillRectangle(new SolidBrush(Color.White), 0, 0, bmp.Width, bmp.Height);
            graphics.DrawString(Compotage.Text, font, new SolidBrush(Color.Black), 0, 0);
            graphics.Flush();
            font.Dispose();
            graphics.Dispose();
        }
        Bitmap bmp1bit = bmp.Clone(new Rectangle(0, 0, W, H), System.Drawing.Imaging.PixelFormat.Format1bppIndexed);
        bmp1bit.Save(lien);![enter image description here][1]

However I have one problem, even if window sees it like bmp one bitmap 1bpp when i open it with bloc-note i have it:

‰PNG
IHDR  …   ô   ˆ   sRGB ®Îé   gAMA  ±üa   PLTE   ÿÿÿ¥ÙŸÝ     pHYs  à  ÃÇo¨d  úIDATxÚíÝOŽÜDp7qQŠeÄ    s–BXIÄXf¦º±D/9B®b§¹F5¹€ae‚©Ç÷½*»Ý“™$B™’geÒŽíªŸëß«ª^L
¹åã¯[Ç[_}e­oàøóß­Õ­¿t·«ß˜xÚÌ'åå+o§xt*œtø™Š¢ˆ®Ä¢”±‘ÁD‚à†Û#…ÙD'±–P4H -'ž4R3Çoñ¶}%C#{<ÑVxÿP•¶ãHi   $fºEÄÕ3q¬±¢¸ÕWDÛ¢æ   ýzñÜ!‰­HµWq(4äÍWÞ0_D<á×âÐP¬‘(”OxÅ­E”,4RõÔVÅß!*âuýV3XĮ隵|ã&–ãf*šOœ”S5‹
Þ¬ÝK¨¥êÐp½H©bÈ¢Ÿœìe/CçñRìI¨ÇµØÉ"¢‘ë©’ô¨"¤åÉT!j¯’Yܫ肟êŽ×™í¯÷ñïy#[ÄI×âNÅrÑþ!‡“^X¤l×bÕÊsˆÚ&h–¸Añ¨âm36¨òjQ·Ë,N›XPl´R¢CïÛ‰ô\FØh½çŽ÷?xf‹w{N±¥Ø¡(‹ˆWµˆSüNEëÁk¥H…Ò9®Äèr€8ÖÁ
³Å»½ xPW´ÛEÄOµÑ\ÉôHÅ ùq¢ôáL<¨øÂ¡ÁñèÈ>ˆÓ2ö׈ü\‹¨ŠO€Æ»Í¹¨'Ã"¢¾‘ÝbthmV   ïn!ú$6EÓ­Äg+‘ƒyëX£8“ŠýJ”•ÈìÑH=hrû$¶GYj^ê‘V"ª„âcûx¯fR¥Pœšù]ñˆ‹GdƒˆʇÞ.Ÿ@Üí«¯‰ã%CŠâ2ôñ¾»$¦öp+1¨è(2ªò)ŽÖ»‹X½UÜ­Åj%f1nVâ ªŠ[äVö©÷S¬9o    Ë,úßúè â*YöÈLKÑœ‰~À„ÈY¯m5×H‘1ÑqôI<"®–'A6‰÷³øÔ!¶‹81JSDÔãÔµˆ^Åò
œ¹µŒ+›sA÷’xo³w‹8Ô*j$ï0éÍ"½OQlt®dÈ™’Ø\-ú3Q›œbóbŸDǹc9áÕ'±Q±G6¿Ó3}ž$g«+D,Nb‘Å›Äp›YD~EµÑ#/êc.ãÀ çeúºÀí å‹ÕIdÏÑZ¥¸ôœâ±_‹‹(YdGíN³ÇÆxÑÑvÔr"ùCˆˆ±ÎDí9çbŽ9õˆCU’{NÅFØœD%¼Å.÷CŒèÑ\ÖPì¯gâ&ÇÕ‹z+§Üsk’¸ã`,‘÷ ˆ?UIÜIZ;¥6ì¯íI,ñŠ_,`;]p®ÄƒLß4Ä2‹¨òáÓE<¾M¬òlåÅi%æ“Àf9ÍŒÓC?ÓJ޹Æ2ˆ?ì5—´’Î"yûúŒì††‹÷ÏEDrNõìŽYD€Ëbd€döŽ+ÚBŸ¡ÈÙÊ]ž¯kŠw–ÙJòl5ÖWŠOÒ{á}À
y,Ö׋£?­süž¢ÜYfd   ³8f1ª¸ÇÉ?~Šèr„DGÆñÊUÇ™˜Örþg·kñeZRÔY­ó¢èg\!xìN"sI3Àµb—Å&‰óÊ
žD]¢¤% ˺ˆXÉ5iJ"ÒìT¼bõx'ñ¹ŠÝ¼zd8   _JNN"–ª¢bà^bÔ52T‘Ѷå
¹9[!ï$æÀ5ļðŠa^!sJŠ´BvIÄ|RÜpët3Õ耥xP;‰´g»€Eì—Žï|ó.€ÓÞ¤';TÓNÅ’¹w³ÈbPdPڇè»#„¼y§sA±«C^!OX @¬·ÅMªâ68ç“nXÆ­p3Ùd‘keŠ[(ÕItg»9FÜ$¥îWâ˜vs½ÓBËØéŠi«‘4rC£}dQSá)§ÓõŽUÅqÙ?FäÄ=r%‹ÜP«ˆ¬Ø…‚^¡(³k²xQ«9‹"ï=åt»Þ•«¹ON1—Ñ×+‡<’tW®b(¾â¸
žW˜áXÔ+‘£,‹wÜ»~óyýX}ó0êJï(ùÓË5Çûú)Öïúä{ûÎêÙ­‹b¢‰&šh¢‰&šh¢‰&šh¢‰&šh¢‰&šh¢‰&šh¢‰&šh¢‰&šh¢‰&šh¢‰&šh¢‰&šh¢‰&šh¢‰&šh¢‰&šh¢‰&~hâÇð»§ VíwZšøÿ°Zý D±(w‡ErÿÙa£ã&ŽÑV7pØx¼‘Ãjõƒm~¼‰ÃÚÑDÿ;¢}p‡E¹C´ÎM·_ÆàÔ’W}Ò?J_nB3    IEND®B`‚

So like you see,it is one png and not bitmap 1bpp.

After it i try create my bitmap myself:

string lien = "C:/KMP_WT/Resources/bitmap/test.bmp";
int W = 864;
int H = 300;


Bitmap bmp = new Bitmap(W, H);
bmp.SetResolution(300, 300);
using (Graphics graphics = Graphics.FromImage(bmp))
{
    Font font = new Font("Arial", 12);
    graphics.FillRectangle(new SolidBrush(Color.White), 0, 0, W, H);
    graphics.DrawString("Hello World", font, new SolidBrush(Color.Black), 0, 0);
    graphics.Flush();
    font.Dispose();
    graphics.Dispose();
}
//block it here 

int taille_data=(864/32)*4*300;
int taille_fichier = taille_data + 62;
byte[] data = new byte[taille_data];
FileStream bitmap = new FileStream(lien, FileMode.Create);
bitmap.Write(System.Text.Encoding.ASCII.GetBytes("BM"),0,2);
bitmap.Write(BitConverter.GetBytes(taille_fichier), 0, 4);
bitmap.Seek(4, SeekOrigin.Current);
bitmap.Write(BitConverter.GetBytes(62), 0, 4);
bitmap.Write(BitConverter.GetBytes(40), 0, 4);
bitmap.Write(BitConverter.GetBytes(W), 0, 4);
bitmap.Write(BitConverter.GetBytes(H), 0, 4);
bitmap.Write(BitConverter.GetBytes(1), 0, 2);
bitmap.Write(BitConverter.GetBytes(1), 0, 2);
bitmap.Write(BitConverter.GetBytes(0), 0, 4);
bitmap.Write(BitConverter.GetBytes(taille_data), 0, 4);
bitmap.Write(BitConverter.GetBytes(11811), 0, 4);
bitmap.Write(BitConverter.GetBytes(11811), 0, 4);
bitmap.Write(BitConverter.GetBytes(0), 0, 4);
bitmap.Write(BitConverter.GetBytes(0), 0, 4);
bitmap.Seek(8, SeekOrigin.Current);
bitmap.Write(data, 0, taille_data);
bitmap.Close();
Imprimante.Envoyer_Commande(s, ip_local, listenPort, "#CMD_1 file_name " + lien);

I am block it, I dont know how treate my graphics for get the bytes and send to byte[] data

Thank you for you time and help.

If you have other solution for have one byte[] where the sentence is write with font and size, I am taker.

N.B. I have to have real bitmap because after this program, I use one printer and it read only BMP 1BPP.

2
  • Have you tried bmp1bit.Save(lien,ImageFormat.Bmp) ? Commented Feb 5, 2014 at 16:19
  • WTF thanks you very much, I past all my day... you save me. Commented Feb 5, 2014 at 16:34

1 Answer 1

1

Specify the format for the Bitmap-ImageFormat.Bmp

bmp1.Save("mycoolimage.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
Sign up to request clarification or add additional context in comments.

Comments