/// <summary> /// 显示当前Cell的信息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void grdFont_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e) { try { // 计算当前Cell字符宽度 Bitmap cellImg = this.grdFont.Rows[e.RowIndex].Cells[e.ColumnIndex].Value as Bitmap; int minX = cellImg.Width; for (int y = 0; y < cellImg.Height; y++) { for (int x = 0; x < cellImg.Width; x++) { Color color = cellImg.GetPixel(x, y); if (color.A != 0) { if (x < minX) { minX = x; } break; } } } int maxX = 0; for (int y = 0; y < cellImg.Height; y++) { for (int x = cellImg.Width - 1; x >= 0; x--) { Color color = cellImg.GetPixel(x, y); if (color.A != 0) { if (x > maxX) { maxX = x; } break; } } } // 查看对应的旧(或新)字符 string[] fontChars; if (this.isViewOld) { fontChars = File.ReadAllLines(this.isComText ? cnTextFont : cnFileFont); } else { fontChars = Bio1TextEditor.GetFontChars(this.isComText); } string otherChar = fontChars[e.RowIndex * 32 + e.ColumnIndex]; this.Text = "Cell图片宽度 : " + (maxX - minX) + " 对应的字符:" + otherChar; } catch { } }
/// <summary> /// 根据单个字符生成字库图片 /// </summary> /// <param name="fontChar"></param> /// <returns></returns> private Bitmap CreateCharBitMap(string fontChar) { Bitmap bmp = new Bitmap(32, 32); Graphics g = Graphics.FromImage(bmp); g.Clear(Color.FromArgb(0, 0, 0, 0)); g.SmoothingMode = SmoothingMode.HighQuality; g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; Pen blackPen = new Pen(Color.Black, 0.01F); if (this.useOldFont.Contains(fontChar)) { // 取得原有的字库 int fontW = 32; int fontH = 32; int charFontIndex = 0; string[] diffData = Bio1TextEditor.GetDiffData(fontChar, this.isComText).Split(' '); int index = Convert.ToInt32(diffData[0]); // 根据旧字符宽度信息,设置新字符宽度信息 if (this.isComText) { this.newFontCharInfoLst.Add(this.fontCharInfoLst[index]); } else { this.newFontCharInfoLst.Add(this.fontCharInfoLst[index + 32 * 34 + 2]); } // 取得旧字库图片 for (int y = 0; y < fontBmp.Height; y += fontH) { Image[] rowImage = new Image[this.fontBmp.Width / fontW]; for (int x = 0; x < this.fontBmp.Width; x += fontW) { if (charFontIndex == index) { for (int yPixel = 0; yPixel < fontH; yPixel++) { for (int xPixel = 0; xPixel < fontW; xPixel++) { bmp.SetPixel(xPixel, yPixel, this.fontBmp.GetPixel(x + xPixel, y + yPixel)); } } return(bmp); } charFontIndex++; } } } else { FontCharInfo charInfo = null; if (string.IsNullOrEmpty(fontChar)) { // 更新进度条 this.ProcessBarStep(); return(bmp); } else if (" " == fontChar) { charInfo = new FontCharInfo(); charInfo.LeftPadding = 32; charInfo.Width = 0; this.newFontCharInfoLst.Add(charInfo); // 更新进度条 this.ProcessBarStep(); return(bmp); } // 单个子对齐风格 StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Near; CharacterRange[] characterRanges = { new CharacterRange(0, 1) }; sf.SetMeasurableCharacterRanges(characterRanges); // 在指定的区域内写入特定汉字 RectangleF rectangle = new RectangleF(0, 0, 32, 30); GraphicsPath graphPath = new GraphicsPath(); graphPath.AddString(fontChar, new FontFamily(txtFontTest.Font.Name), (int)FontStyle.Regular, txtFontTest.Font.Size, rectangle, sf); g.FillPath(Brushes.White, graphPath); g.DrawPath(blackPen, graphPath); Region[] charRegions = g.MeasureCharacterRanges(fontChar, this.selectedFont, rectangle, sf); int charWidth = (int)charRegions[0].GetBounds(g).Width; int charLeftWidth = 32 + 1 - charWidth; if (charLeftWidth <= 0) { charLeftWidth = 0; } else if (charLeftWidth % 2 != 0) { charLeftWidth = charLeftWidth / 2 + 1; } else { charLeftWidth = charLeftWidth / 2; } charInfo = new FontCharInfo(); charInfo.LeftPadding = charLeftWidth; charInfo.Width = charLeftWidth + charWidth; this.newFontCharInfoLst.Add(charInfo); } // 更新进度条 this.ProcessBarStep(); return(bmp); }