protected override void InnerDraw(GraphicsDisplay display, GeoStar.Core.TrackCancel cancel) { if (m_TileList.Count == 0) { return; } List <QuadTile> vList = new List <QuadTile>(); lock (this) { vList.AddRange(m_TileList); } System.Windows.Media.DrawingContext dc = display.DrawingContext; foreach (var item in vList) { Box box = m_Pyramid.TileExtent(item.Level, item.Row, item.Col); System.Windows.Rect rect = display.DT.FromMapWPF(box); dc.DrawRectangle(null, new System.Windows.Media.Pen(System.Windows.Media.Brushes.Red, 1), rect); System.Windows.Media.FormattedText formattedText = new System.Windows.Media.FormattedText( string.Format(" {0},{1},{2}", item.Level, item.Row, item.Col), System.Globalization.CultureInfo.CurrentCulture, System.Windows.FlowDirection.LeftToRight, new System.Windows.Media.Typeface("微软雅黑"), 12, System.Windows.Media.Brushes.Black); dc.DrawText(formattedText, rect.Location); } }
private ToolStripMenuItem SetFonts() { ToolStripMenuItem menuItem = new ToolStripMenuItem("Insert Font"); List <string> fonts = new List <string>(); foreach (System.Windows.Media.FontFamily font in System.Windows.Media.Fonts.SystemFontFamilies) //WPF fonts { fonts.Add(font.FamilyNames.Values.First()); } fonts.Sort(); foreach (string fontName in fonts) { Bitmap bmp = null; try { int size = 20; //bmp = new Bitmap(4 * size, 5 * size / 4); //Graphics g = Graphics.FromImage(bmp); //g.SmoothingMode = SmoothingMode.HighQuality; //g.InterpolationMode = InterpolationMode.HighQualityBicubic; //g.PixelOffsetMode = PixelOffsetMode.HighQuality; //g.DrawString("Basic", new Font(new FontFamily(fontName), size, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.Black, 1, 1); System.Windows.Media.DrawingVisual dv = new System.Windows.Media.DrawingVisual(); using (System.Windows.Media.DrawingContext dc = dv.RenderOpen()) { dc.DrawRectangle(System.Windows.Media.Brushes.White, null, new System.Windows.Rect(0, 0, 4 * size, 5 * size / 4)); dc.DrawText(new System.Windows.Media.FormattedText("Basic", System.Globalization.CultureInfo.InvariantCulture, System.Windows.FlowDirection.LeftToRight, new System.Windows.Media.Typeface(fontName), size, System.Windows.Media.Brushes.Black), new System.Windows.Point(1, 1)); } System.Windows.Media.Imaging.RenderTargetBitmap rtb = new System.Windows.Media.Imaging.RenderTargetBitmap(4 * size, 5 * size / 4, 96, 96, System.Windows.Media.PixelFormats.Pbgra32); rtb.Render(dv); rtb.Freeze(); MemoryStream stream = new MemoryStream(); System.Windows.Media.Imaging.BitmapEncoder encoder = new System.Windows.Media.Imaging.BmpBitmapEncoder(); encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(rtb)); encoder.Save(stream); bmp = new Bitmap(stream); } catch { } ToolStripMenuItem item = new ToolStripMenuItem(fontName, bmp, Insert); item.ImageScaling = ToolStripItemImageScaling.None; menuItem.DropDownItems.Add(item); } return(menuItem); }
protected override void OnRender(System.Windows.Media.DrawingContext drawingContext) { base.OnRender(drawingContext); var text = new FormattedText( this.text, System.Globalization.CultureInfo.CurrentCulture, System.Windows.FlowDirection.LeftToRight, new System.Windows.Media.Typeface(fontFamily), fontSize, foreground); drawingContext.DrawText(text, new Point(3, 3)); }
protected override void OnRender(System.Windows.Media.DrawingContext drawingContext) { base.OnRender(drawingContext); Matrix m = PresentationSource.FromVisual(Application.Current.MainWindow).CompositionTarget.TransformToDevice; var text = new FormattedText( this.text, System.Globalization.CultureInfo.CurrentCulture, System.Windows.FlowDirection.LeftToRight, new Typeface(fontFamily), fontSize, foreground, m.M11); drawingContext.DrawText(text, new System.Windows.Point(3, 3)); }
/// <summary>Draw Text to canvas according coordinates and label</summary> public void DrawText(string label, float x, float y, float xmin, float ymin, float xmax, float ymax) { if (_fillColor.Color.A > 0) { // Create formatted text in a particular font at a particular size var typeFace = new System.Windows.Media.Typeface(_fontFamily, _fontStyle, _fontWeight, _fontStretch); var ft = new System.Windows.Media.FormattedText ( label, System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeFace, _fontSize, _fillColor ); ft.TextAlignment = TextAlignment.Left; // Draw the text var clipped = PushRenderStates(); _drawingContext.DrawText(ft, new System.Windows.Point(x, y - _baseline)); PopRenderStates(clipped); } }
/* * // Windows Forms版 TODO: 直書きあり * public static void DrawString(string str, double fontSize) * { * var window = System.Windows.Application.Current.MainWindow; * * double[] glDoubleColor = new double[4]; * GL.GetDouble(GetPName.CurrentColor, glDoubleColor); * byte glR = (byte)(glDoubleColor[0] * 255); * byte glG = (byte)(glDoubleColor[1] * 255); * byte glB = (byte)(glDoubleColor[2] * 255); * byte glA = (byte)(glDoubleColor[3] * 255); //255; * var glColor = System.Drawing.Color.FromArgb(glA, glR, glG, glB); * System.Drawing.Brush foreground = new System.Drawing.SolidBrush(glColor); * * //var font = new System.Drawing.Font("Arial", 12); * //var font = new System.Drawing.Font(window.FontFamily.Source, (int)window.FontSize); * // 1 point = 1 / 72.0 inch = (1 / 72.0) * 96 = 1.333333 pixel * var font = new System.Drawing.Font(window.FontFamily.Source, (int)(fontSize / 1.333333)); * // サイズ取得用に生成 * var bmp = new System.Drawing.Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb); * System.Drawing.SizeF size; * using (var g = System.Drawing.Graphics.FromImage(bmp)) * { * g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; * * size = g.MeasureString(str, font); * } * * // 再生成 * { * int width = (int)size.Width; * //int height = (int)size.Height; * // TODO: テキストのY座標がずれている : 画像の高さが大きすぎる * int height = (int)(size.Height * 0.75); * bmp = new System.Drawing.Bitmap(width, height, * System.Drawing.Imaging.PixelFormat.Format32bppArgb); * } * * using (var g = System.Drawing.Graphics.FromImage(bmp)) * { * g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; * * g.DrawString(str, font, foreground, new System.Drawing.Point(0, 0)); * } * * // check * //bmp.Save("1.bmp"); * * // 上下反転する * bmp.RotateFlip(System.Drawing.RotateFlipType.RotateNoneFlipY); * * var bitmapData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), * System.Drawing.Imaging.ImageLockMode.ReadOnly, * System.Drawing.Imaging.PixelFormat.Format32bppArgb); * int bmpWidth = bitmapData.Width; * int bmpHeight = bitmapData.Height; * * bool isTexture = GL.IsEnabled(EnableCap.Texture2D); * bool isBlend = GL.IsEnabled(EnableCap.Blend); * GL.Enable(EnableCap.Texture2D); * GL.Enable(EnableCap.Blend); * GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha); * * int texture = GL.GenTexture(); * GL.BindTexture(TextureTarget.Texture2D, texture); * GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, * (int)TextureMinFilter.Linear); * GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, * (int)TextureMagFilter.Linear); * GL.TexImage2D(TextureTarget.Texture2D, 0, * PixelInternalFormat.Rgba, * bmpWidth, bmpHeight, 0, * PixelFormat.Bgra, PixelType.UnsignedByte, bitmapData.Scan0); * * GL.PushMatrix(); * OpenTK.Matrix4d m; * GL.GetDouble(GetPName.ModelviewMatrix, out m); * //GL.Translate(0, 0, -m.M14); * GL.Scale(1.0 / m.M11, 1.0 / m.M22, 1.0 / m.M33); * GL.Scale(FontScale, FontScale, 1.0); // TODO: これを計算で求める必要がある * * GL.Begin(PrimitiveType.Quads); * GL.TexCoord2(0, 0); * GL.Vertex2(0, 0); * * GL.TexCoord2(1, 0); * GL.Vertex2(bmpWidth, 0); * * GL.TexCoord2(1, 1); * GL.Vertex2(bmpWidth, bmpHeight); * * GL.TexCoord2(0, 1); * GL.Vertex2(0, bmpHeight); * GL.End(); * GL.PopMatrix(); * * bmp.UnlockBits(bitmapData); * * if (!isTexture) * { * GL.Disable(EnableCap.Texture2D); * } * if (!isBlend) * { * GL.Disable(EnableCap.Blend); * } * } */ // WPF版 TODO: 直書きあり public static void DrawString(string str, double fontSize) { var window = System.Windows.Application.Current.MainWindow; double[] glDoubleColor = new double[4]; GL.GetDouble(GetPName.CurrentColor, glDoubleColor); var glColor = new System.Windows.Media.Color(); glColor.R = (byte)(glDoubleColor[0] * 255); glColor.G = (byte)(glDoubleColor[1] * 255); glColor.B = (byte)(glDoubleColor[2] * 255); glColor.A = (byte)(glDoubleColor[3] * 255); //255; System.Windows.Media.Brush foreground = new System.Windows.Media.SolidColorBrush(glColor); //System.Windows.Media.Brush foreground = window.Foreground; var text = new System.Windows.Media.FormattedText( str, new System.Globalization.CultureInfo("en-us"), System.Windows.FlowDirection.LeftToRight, new System.Windows.Media.Typeface( window.FontFamily, System.Windows.FontStyles.Normal, System.Windows.FontWeights.Normal, new System.Windows.FontStretch()), fontSize, //window.FontSize, foreground); var drawingVisual = new System.Windows.Media.DrawingVisual(); using (System.Windows.Media.DrawingContext drawingContext = drawingVisual.RenderOpen()) { drawingContext.DrawText(text, new System.Windows.Point(0, 0)); } System.Windows.Media.Imaging.RenderTargetBitmap bmp = null; { int width = (int)text.Width; int height = (int)text.Height; int dpiX = 96; int dpiY = 96; bmp = new System.Windows.Media.Imaging.RenderTargetBitmap( width, height, dpiX, dpiY, System.Windows.Media.PixelFormats.Pbgra32); } bmp.Render(drawingVisual); int bmpWidth = bmp.PixelWidth; int bmpHeight = bmp.PixelHeight; int stride = bmpWidth * 4; byte[] tmpbits = new byte[stride * bmpHeight]; var rectangle = new System.Windows.Int32Rect(0, 0, bmpWidth, bmpHeight); bmp.CopyPixels(rectangle, tmpbits, stride, 0); // 上下反転する byte[] bits = new byte[stride * bmpHeight]; for (int h = 0; h < bmpHeight; h++) { for (int w = 0; w < stride; w++) { bits[h * stride + w] = tmpbits[(bmpHeight - 1 - h) * stride + w]; } } // check //var png = new System.Windows.Media.Imaging.PngBitmapEncoder(); //png.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bmp)); //using (var fs = new FileStream("1.png", FileMode.Create)) //{ // png.Save(fs); //} bool isTexture = GL.IsEnabled(EnableCap.Texture2D); bool isBlend = GL.IsEnabled(EnableCap.Blend); GL.Enable(EnableCap.Texture2D); GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha); int texture = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, texture); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bmpWidth, bmpHeight, 0, PixelFormat.Bgra, PixelType.UnsignedByte, bits); GL.PushMatrix(); OpenTK.Matrix4d m; GL.GetDouble(GetPName.ModelviewMatrix, out m); GL.Scale(1.0 / m.M11, 1.0 / m.M22, 1.0 / m.M33); GL.Scale(FontScale, FontScale, 1.0); // TODO: これを計算で求める必要がある GL.Begin(PrimitiveType.Quads); GL.TexCoord2(0, 0); GL.Vertex2(0, 0); GL.TexCoord2(1, 0); GL.Vertex2(bmpWidth, 0); GL.TexCoord2(1, 1); GL.Vertex2(bmpWidth, bmpHeight); GL.TexCoord2(0, 1); GL.Vertex2(0, bmpHeight); GL.End(); GL.PopMatrix(); if (!isTexture) { GL.Disable(EnableCap.Texture2D); } if (!isBlend) { GL.Disable(EnableCap.Blend); } }