private void AsynDisplayImage() { ThreadPool.QueueUserWorkItem(o => { Application.Current.Dispatcher.BeginInvoke( DispatcherPriority.SystemIdle, new Action(() => { //新开一个线程池执行draw BMDrawer bmd = new BMDrawer(); string teststrings = characterTextBox.Text; //放大比例 double rate = 1f; PresentationSource source = PresentationSource.FromVisual(this); double dpiX = 0f, dpiY = 0f; if (source != null) { dpiX = 96.0 * source.CompositionTarget.TransformToDevice.M11; dpiY = 96.0 * source.CompositionTarget.TransformToDevice.M22; } rate = dpiX / 96d; Bitmap bmp = bmd.test_draw(teststrings); BitmapImage cbmp = BitmapToBitmapImage(bmp); image.Height = cbmp.Height * rate; image.Width = cbmp.Width * rate; image.Source = cbmp; if (checkboxShowLine.IsChecked == true) { DrawingVisual drawingVisual = new DrawingVisual(); using (DrawingContext drawingContext = drawingVisual.RenderOpen()) { // // ... draw on the drawingContext // System.Windows.Media.Pen pen = new System.Windows.Media.Pen(System.Windows.Media.Brushes.Red, (double)0.3f); drawingContext.DrawImage(image.Source, new Rect(0, 0, image.Source.Width * rate, image.Source.Height * rate)); int x_lines = (int)Math.Round((float)(int)image.Width / (float)GlobalSettings.iTileWidth); int y_lines = (int)Math.Round((float)(int)image.Height / (float)GlobalSettings.iTileHeight);; for (int i = 0; i < x_lines + 1; i++) { drawingContext.DrawLine(pen, new System.Windows.Point(i * GlobalSettings.iTileWidth, 0), new System.Windows.Point(i * GlobalSettings.iTileWidth, (float)(int)image.Height)); } for (int i = 0; i < y_lines + 1; i++) { drawingContext.DrawLine(pen, new System.Windows.Point(0, i * GlobalSettings.iTileHeight), new System.Windows.Point((float)(int)image.Width, i * GlobalSettings.iTileHeight)); } drawingContext.Close(); RenderTargetBitmap nbmp = new RenderTargetBitmap((int)(image.Width * rate), (int)(image.Height * rate), 96.0 * rate * rate, 96.0 * rate * rate, PixelFormats.Default); nbmp.Render(drawingVisual); image.Source = nbmp; } } }) ); }); }
private void AsynSaveMultiImage(string saveFolderName, System.Drawing.Imaging.ImageFormat fmt) { ThreadPool.QueueUserWorkItem(o => { Application.Current.Dispatcher.BeginInvoke( DispatcherPriority.SystemIdle, new Action(() => { //新开一个线程池执行draw BMDrawer bmd = new BMDrawer(); string teststrings = tmps.tmpstr; Bitmap[] bmp = bmd.DrawMultiImages(teststrings); if (bmp.Length > 0) { for (int i = 0; i < bmp.Length; i++) { Bitmap tmpBmp = bmp[i]; Bitmap dest = new Bitmap(tmpBmp.Width, tmpBmp.Height); if (GlobalSettings.bOptmizeAlpha == true) { System.Drawing.Color pixel; for (int x = 0; x < tmpBmp.Width; x++) { for (int y = 0; y < tmpBmp.Height; y++) { pixel = tmpBmp.GetPixel(x, y); int r, g, b, a, Result = 0; r = pixel.R; g = pixel.G; b = pixel.B; a = pixel.A; Result = Math.Max(Math.Max(r * a / 255, g * a / 255), b * a / 255); dest.SetPixel(x, y, System.Drawing.Color.FromArgb(Result, 255, 255, 255)); } } } else { Graphics draw = null; draw = Graphics.FromImage(dest); draw.DrawImage(tmpBmp, 0, 0); } string fot_name = System.IO.Path.GetFileName(GlobalSettings.fFontName); string name = string.Format("{0}\\{1}_{2}.{3}", saveFolderName, fot_name, i, fmt.ToString()); dest.Save(name, fmt); dest.Dispose(); tmpBmp.Dispose(); } } }) ); }); }
private void AsynSaveSingleImage(string saveFolderName, System.Drawing.Imaging.ImageFormat fmt) { ThreadPool.QueueUserWorkItem(o => { Application.Current.Dispatcher.BeginInvoke( DispatcherPriority.SystemIdle, new Action(() => { //新开一个线程池执行draw BMDrawer bmd = new BMDrawer(); string teststrings = tmps.tmpstr; if (teststrings.Length > 0) { Bitmap tmpBmp = bmd.test_draw(teststrings); Bitmap dest = new Bitmap(tmpBmp.Width, tmpBmp.Height); if (GlobalSettings.bOptmizeAlpha == true) { System.Drawing.Color pixel; for (int x = 0; x < tmpBmp.Width; x++) { for (int y = 0; y < tmpBmp.Height; y++) { pixel = tmpBmp.GetPixel(x, y); int r, g, b, a, Result = 0; r = pixel.R; g = pixel.G; b = pixel.B; a = pixel.A; Result = Math.Max(Math.Max(r * a / 255, g * a / 255), b * a / 255); dest.SetPixel(x, y, System.Drawing.Color.FromArgb(Result, 255, 255, 255)); } } } else { dest = tmpBmp; } dest.Save(string.Format("{0}\\font.{1}", saveFolderName, fmt.ToString()), fmt); dest.Dispose(); tmpBmp.Dispose(); } }) ); }); }
private void buttonSaveTable_Click(object sender, RoutedEventArgs e) { string message = ""; bool checkResult = checkBeforeRender(ref message); if (checkResult == false) { System.Windows.MessageBox.Show(message); return; } SaveFileDialog savefiledialog = new SaveFileDialog(); savefiledialog.RestoreDirectory = true; savefiledialog.Filter = "Font Binary(*.bin)|*.bin|Excel csv File(*.csv)|*.csv|BMFont fnt File(*.fnt)|*.fnt"; if (savefiledialog.ShowDialog() == true) { string saveFileName = savefiledialog.FileName; int currentIndex = savefiledialog.FilterIndex; FileStream fs; switch (currentIndex) { case 1: fs = new FileStream(saveFileName, FileMode.Create, FileAccess.Write); BinaryWriter baseStream = new BinaryWriter(fs); BMDrawer bmd = new BMDrawer(); Table.XYWH[] XYWHS = bmd.GetXYWHTable(characterTextBox.Text); baseStream.BaseStream.WriteByte(0x46); baseStream.BaseStream.WriteByte(0x4E); baseStream.BaseStream.WriteByte(0x54); baseStream.BaseStream.WriteByte(0x42); baseStream.Write(BitConverter.GetBytes(XYWHS.Length)); foreach (var v in XYWHS) { baseStream.Write(BitConverter.GetBytes(v.charid)); baseStream.Write(BitConverter.GetBytes(v.x_pos)); baseStream.Write(BitConverter.GetBytes(v.y_pos)); baseStream.Write(BitConverter.GetBytes(v.c_width)); baseStream.Write(BitConverter.GetBytes(v.c_height)); baseStream.Write(BitConverter.GetBytes(v.page_num)); } baseStream.Close(); fs.Close(); break; case 2: fs = new FileStream(saveFileName, FileMode.Create, FileAccess.Write); StreamWriter sw = new StreamWriter(fs); BMDrawer bmd1 = new BMDrawer(); Table.XYWH[] XYWHS1 = bmd1.GetXYWHTable(characterTextBox.Text); foreach (var v in XYWHS1) { sw.WriteLine(string.Format("{0},{1},{2},{3},{4},{5}", v.charid, v.x_pos, v.y_pos, v.c_width, v.c_height, v.page_num)); } sw.Close(); fs.Close(); break; case 3: // bmfont fnt fine fs = new FileStream(saveFileName, FileMode.Create, FileAccess.Write); StreamWriter sw1 = new StreamWriter(fs); BMDrawer bmd2 = new BMDrawer(); Table.XYWH[] XYWHS2 = bmd2.GetXYWHTable(characterTextBox.Text); //write fnt header int lineHeight = GlobalSettings.iTileHeight, baseHeight = GlobalSettings.iTileHeight, scaleW = GlobalSettings.iImageWidth, scaleH = GlobalSettings.iImageWidth, pages = (int)XYWHS2[XYWHS2.Length - 1].page_num; string bmfont_name = System.IO.Path.GetFileName(GlobalSettings.fFontName); sw1.WriteLine(string.Format("info face=\"{0}\" bold=0 italic=0 charset=\"\" unicode=1 stretchH=100 smooth=1 aa=4 padding=0,0,0,0 spacing=0,0 outline=0", bmfont_name)); sw1.WriteLine(string.Format("common lineHeight={0} base={1} scaleW={2} scaleH={3} pages={4} packed=0 alphaChnl=1 redChnl=0 greenChnl=0 blueChnl=0", lineHeight, baseHeight, scaleW, scaleH, pages) ); sw1.WriteLine(string.Format("page id=0 file=\"{0}_0.png\"", bmfont_name)); sw1.WriteLine(string.Format("chars count={0}", XYWHS2.Length)); foreach (var v in XYWHS2) { sw1.WriteLine(string.Format("char id={0} x={1} y={2} width={3} height={4} xoffset={5} yoffset={6} xadvance={7} page={8} chnl=15", v.charid, v.x_pos, v.y_pos, v.c_width, v.c_height, 0, 0, v.c_width, v.page_num) ); } sw1.Close(); fs.Close(); break; default: break; } } }