Csharp: Image Character spacing,
Reference windows api:
[DllImport ("gdi32.dll", CharSet = CharSet. auto)] public static extern int SetTextCharacterExtra (IntPtr hdc, int nCharExtra); // picture Character spacing [DllImport ("gdi32.dll")] public static extern bool DeleteObject (IntPtr handle ); [DllImport ("gdi32.dll")] public static extern IntPtr SelectObject (IntPtr hdc, IntPtr bmp );
. NET 2.0
/// <Summary> //// </summary> /// <param name = "sender"> </param> /// <param name = "e "> </param> // <param name =" fontSpace "> </param> private void pictureBox_Paint (object sender, paintEventArgs e) {Graphics g = e. graphics; Font font = new Font (" ICS", 12.0F); Brush brush = Brushes. red; string text2 = "tu juwen"; IntPtr hdc = e. graphics. getHdc (); SetTextCharacterExtra (hdc, 16); // set the Character Spacing e. graphics. releaseHdc (hdc); e. graphics. drawString (text2, font, brush, 20, 25); // pictureBox1.Paint + = new System. windows. forms. paintEventHandler (this. picturebox#paint );}
. NET 3.5 or above:
/// <Summary> /// the Character spacing of the image (valid only for Chinese characters, numbers, letters, and symbols. In Chinese, the Chinese-Japanese mixed sorting is invalid) /// tu juwen ///. above net3.0 /// </summary> /// <param name = "width"> image width </param> /// <param name = "height"> image height </param> /// <param name = "space"> spacing </param> /// <param name = "strtext"> text to be displayed </param> // /<returns> image </returns> Bitmap CreateImageString (int width, int height, int space, string strtext) {Bitmap image = new Bitmap (width, height); using (Graphics g = Graphics. fromImage (image) {// draw the image border // g. drawRectangle (Pens. black, 0, 0, width-1, height-1); Font font = new Font ("", 12.0F); Brush brush = Brushes. red; // draw the output DrawStringExtra (g, space, x => {x. drawString (strtext, font, brush, 0, 2) ;}) ;}return image ;} /// <summary> ///// </summary> /// <param name = "g"> </param> /// <param name = "nCharExtra "> </param> /// <param name =" action "> </param> void DrawStringExtra (Graphics g, int nCharExtra, Action <Graphics> action) {IntPtr hdc = g. getHdc (); SetTextCharacterExtra (hdc, nCharExtra); try {using (Graphics g1 = Graphics. fromHdc (hdc) {action (g1) ;}} finally {SetTextCharacterExtra (hdc, 0); g. releaseHdc (hdc );}}
Call: (can be applied to print)
pictureBox1.Image = CreateImageStrin(100, 30, 10, "3315000");
. Net 2.0 self-written white space to set the Padding. High versions include Padding, PadRight, and PadLeft.
/// <Summary> /// fill the space with characters, and set the Character Spacing // tu juwen // </summary> /// <param name = "str"> </param> /// <returns> </ returns> public string SetPadstring (string str, int padwidth) {string s = string. empty; string m = string. empty; if (str. length> 0) {char [] arr = str. toCharArray (); foreach (char d in arr) {// MessageBox. show (d. toString (); m = m + d + PadRight (padwidth) ;}} s = m; return s ;} /// <summary> /// fill in spaces /// </summary> /// <param name = "totalWidth"> </param> /// <returns> </returns> public string PadLeft (int totalWidth) {string s = ""; if (totalWidth> 0) {for (int I = 0; I <totalWidth; I ++) {s = "" + s ;}} return s ;} /// <summary> /// fill in spaces /// </summary> /// <param name = "TotalWidth"> </param> /// <returns> </returns> public string PadRight (int TotalWidth) {string s = ""; if (TotalWidth> 0) {for (int I = 0; I <TotalWidth; I ++) {s = s + "";}} return s ;}
// Test string cs = "331500 tu juwen"; // char [] arr = cs. toCharArray (); // foreach (char d in arr) // {// MessageBox. show (d. toString (); // m = m + d + PadRight (1); //} // MessageBox. show (m); MessageBox. show (SetPadstring (cs, 1 ));