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 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(); } }) ); }); }