public static Image <Emgu.CV.Structure.Rgb, float> ConvertImage2RGB(WriteableBitmap wtbBmp, Rect useToCheckFaceRect) { RenderTargetBitmap rtbitmap = new RenderTargetBitmap(wtbBmp.PixelWidth, wtbBmp.PixelHeight, wtbBmp.DpiX, wtbBmp.DpiY, System.Windows.Media.PixelFormats.Default); System.Windows.Media.DrawingVisual drawingVisual = new System.Windows.Media.DrawingVisual(); using (var dc = drawingVisual.RenderOpen()) { dc.DrawImage(wtbBmp, new Rect(0, 0, wtbBmp.Width, wtbBmp.Height)); } rtbitmap.Render(drawingVisual); JpegBitmapEncoder bitmapEncoder = new JpegBitmapEncoder(); bitmapEncoder.Frames.Add(BitmapFrame.Create(rtbitmap)); MemoryStream ms = new MemoryStream(); bitmapEncoder.Save(ms); using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(ms)) { Image <Emgu.CV.Structure.Rgb, float> image = new Image <Emgu.CV.Structure.Rgb, float>(bitmap).GetSubRect(new System.Drawing.Rectangle((int)useToCheckFaceRect.X, (int)useToCheckFaceRect.Y, (int)useToCheckFaceRect.Width, (int)useToCheckFaceRect.Height)).Convert <Emgu.CV.Structure.Rgb, float>(); bitmapEncoder = null; rtbitmap = null; drawingVisual = null; return(image); } }
public void Save(string fileName, bool compositeBackground) { iApp.File.EnsureDirectoryExistsForFile(fileName); var target = compositeBackground ? backCanvas : (FrameworkElement)inkCanvas; var bitmap = new RenderTargetBitmap((int)target.ActualWidth, (int)target.ActualHeight, 96, 96, System.Windows.Media.PixelFormats.Default); var visual = new System.Windows.Media.DrawingVisual(); using (var context = visual.RenderOpen()) { var brush = new System.Windows.Media.VisualBrush(target); context.DrawRectangle(brush, null, new Rect(0, 0, target.ActualWidth, target.ActualHeight)); } bitmap.Render(visual); var encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bitmap)); using (var stream = new MemoryStream()) { encoder.Save(stream); System.IO.File.WriteAllBytes(fileName, stream.ToArray()); } var handler = DrawingSaved; if (handler != null) { handler(Pair ?? this, new SaveEventArgs(fileName)); } }
public static Byte[] BitmapImage2Byte(BitmapImage imageSource) { try { //resize ve 32x32 Rect rect = new Rect(0, 0, 32, 32); // Create a DrawingVisual/Context to render with System.Windows.Media.DrawingVisual drawingVisual = new System.Windows.Media.DrawingVisual(); using (System.Windows.Media.DrawingContext drawingContext = drawingVisual.RenderOpen()) { drawingContext.DrawImage(imageSource, rect); } RenderTargetBitmap resizedImage = new RenderTargetBitmap(32, 32, 96, 96, System.Windows.Media.PixelFormats.Default); resizedImage.Render(drawingVisual); // MemoryStream memStream = new MemoryStream(); PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(resizedImage)); encoder.Save(memStream); return(memStream.GetBuffer()); } catch { return(new Byte[0]); } }
private static void _Draw(PointerBitmap ptr, System.Windows.Media.PixelFormat fmt, Action <System.Windows.Media.DrawingContext> onDraw) { // https://stackoverflow.com/questions/88488/getting-a-drawingcontext-for-a-wpf-writeablebitmap // https://social.msdn.microsoft.com/Forums/vstudio/en-US/84299fec-94a1-49a1-b3bc-ec48b8bdf04f/getting-a-drawingcontext-for-a-writeablebitmap?forum=wpf // https://stackoverflow.com/questions/7250282/how-to-draw-directly-on-bitmap-bitmapsource-writeablebitmap-in-wpf var src = BitmapSource.Create(ptr.Info.Width, ptr.Info.Height, 96, 96, fmt, null, ptr.Pointer, ptr.Info.BitmapByteSize, ptr.Info.StepByteSize); var dv = new System.Windows.Media.DrawingVisual(); var dc = dv.RenderOpen(); { // dc.DrawImage(src, new System.Windows.Rect(0, 0, src.PixelWidth, src.PixelHeight)); dc.DrawRectangle(System.Windows.Media.Brushes.Green, null, new System.Windows.Rect(50, 50, 200, 100)); onDraw(dc); } var rt = new RenderTargetBitmap(src.PixelWidth, src.PixelHeight, src.DpiX, src.DpiY, System.Windows.Media.PixelFormats.Pbgra32); rt.Render(dv); // ptr.Bitmap.SetPixels(0, 0, rt.ToMemoryBitmap()); rt.CopyPixels(System.Windows.Int32Rect.Empty, ptr.Pointer, ptr.Info.BitmapByteSize, ptr.Info.StepByteSize); }
public static BitmapSource Capture(FrameworkElement view) { if (view == null) { throw new ArgumentNullException(nameof(view)); } var width = (int)view.ActualWidth; var height = (int)view.ActualHeight; if (width <= 0 || height <= 0) { return(null); } var bmp = new RenderTargetBitmap( width, height, 96, // TODO: Handle other DPI settings 96, System.Windows.Media.PixelFormats.Default); var dv = new System.Windows.Media.DrawingVisual(); using (var dc = dv.RenderOpen()) { dc.DrawRectangle( new System.Windows.Media.VisualBrush(view), null, new Rect(0, 0, width, height)); } bmp.Render(dv); return(bmp); }
/// <summary> /// Save image as file from WriteableBitmap /// </summary> /// <param name="wtbBmp">WriteableBitmap</param> /// <param name="path">full path with file name</param> public static void SaveWriteableBitmap(WriteableBitmap wtbBmp, string path) { if (wtbBmp == null) { return; } RenderTargetBitmap rtbitmap = new RenderTargetBitmap(wtbBmp.PixelWidth, wtbBmp.PixelHeight, wtbBmp.DpiX, wtbBmp.DpiY, System.Windows.Media.PixelFormats.Default); System.Windows.Media.DrawingVisual drawingVisual = new System.Windows.Media.DrawingVisual(); using (var dc = drawingVisual.RenderOpen()) { dc.DrawImage(wtbBmp, new Rect(0, 0, wtbBmp.Width, wtbBmp.Height)); } rtbitmap.Render(drawingVisual); JpegBitmapEncoder bitmapEncoder = new JpegBitmapEncoder(); bitmapEncoder.Frames.Add(BitmapFrame.Create(rtbitmap)); //string strDir = @"Images\"; //string strpath = strDir + DateTime.Now.ToString("yyyyMMddfff") + ".jpg"; if (!File.Exists(path)) { MemoryStream ms = new MemoryStream(); //bitmapEncoder.Save(File.OpenWrite(path)); bitmapEncoder.Save(ms); System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(ms); bitmap.Save(path); } }
public _DrawingContext(System.Windows.Media.Imaging.RenderTargetBitmap rt) { _RenderTarget = rt; _Visual = new System.Windows.Media.DrawingVisual(); _Context = _Visual.RenderOpen(); SetContext(_Context); }
// Create a DrawingVisual that contains an ellipse. private System.Windows.Media.DrawingVisual CreateDrawingVisualEllipses() { System.Windows.Media.DrawingVisual drawingVisual = new System.Windows.Media.DrawingVisual(); DrawingContext drawingContext = drawingVisual.RenderOpen(); drawingContext.DrawEllipse(Brushes.Maroon, null, new Point(430, 136), 20, 20); drawingContext.Close(); return(drawingVisual); }
public void DrawStringTest() { const int ColumnCount = 10; const int MaxDrawCount = 30; // use int.MaxValue to draw them all const double fontSize = 50d; // the height of each cell has to include over/under hanging glyphs var cellSize = new Size(fontSize, fontSize * _glyphTypeface.Height); var glyphs = from glyphIndex in _glyphTypeface.CharacterToGlyphMap.Values select _glyphTypeface.GetGlyphOutline(glyphIndex, fontSize, 1d); // now create the visual we'll draw them to var dv = new System.Windows.Media.DrawingVisual(); var drawCount = -1; using (var dc = dv.RenderOpen()) { foreach (var g in glyphs) { drawCount++; if (drawCount >= MaxDrawCount) { break; // don't draw more than you want } if (g.IsEmpty()) { continue; // don't draw the blank ones } // center horizontally in the cell var xOffset = (drawCount % ColumnCount) * cellSize.Width + (cellSize.Width - g.Bounds.Width) / 2d; // place the character on the baseline of the cell var yOffset = (drawCount / ColumnCount) * cellSize.Height + fontSize * _glyphTypeface.Baseline; dc.PushTransform(new System.Windows.Media.TranslateTransform(xOffset, yOffset)); dc.DrawGeometry(System.Windows.Media.Brushes.Red, null, g); dc.Pop(); // get rid of the transform } } var rowCount = drawCount / ColumnCount; if (drawCount % ColumnCount != 0) { rowCount++; // to include partial rows } var bitWidth = (int)Math.Ceiling(cellSize.Width * ColumnCount); var bitHeight = (int)Math.Ceiling(cellSize.Height * rowCount); var bmp = new System.Windows.Media.Imaging.RenderTargetBitmap(bitWidth, bitHeight, 96, 96, System.Windows.Media.PixelFormats.Pbgra32); bmp.Render(dv); var encoder = new System.Windows.Media.Imaging.PngBitmapEncoder(); encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bmp)); using (var file = new FileStream("FontTable.png", FileMode.Create)) encoder.Save(file); }
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); }
public System.Drawing.Bitmap RenderSingleCharacter(ushort glyphIndex, System.Drawing.Color backColor, System.Drawing.Color foreColor, int fontSize) { try { var geometry = _glyphTypeface.GetGlyphOutline(glyphIndex, fontSize, 1); var advanceWidth = _glyphTypeface.AdvanceWidths[glyphIndex]; var cellSize = new Size(fontSize, fontSize * _glyphTypeface.Height); var offsetX = advanceWidth * cellSize.Width; var offsetY = fontSize * _glyphTypeface.Baseline; var drawingVisual = new System.Windows.Media.DrawingVisual(); var brush = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(foreColor.A, foreColor.R, foreColor.G, foreColor.B)); var pen = new System.Windows.Media.Pen(); using (var dc = drawingVisual.RenderOpen()) { dc.PushTransform(new System.Windows.Media.TranslateTransform(0, offsetY)); dc.DrawGeometry(brush, null, geometry); dc.Pop(); // get rid of the transform } var bitWidth = (int)Math.Ceiling(offsetX); var bitHeight = (int)Math.Ceiling(cellSize.Height); var targetImage = new System.Windows.Media.Imaging.RenderTargetBitmap(bitWidth, bitHeight, Dpi, Dpi, System.Windows.Media.PixelFormats.Pbgra32); targetImage.Render(drawingVisual); var bmpEncoder = new System.Windows.Media.Imaging.PngBitmapEncoder(); bmpEncoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(targetImage)); var bmp = new System.Drawing.Bitmap(bitWidth, bitHeight); bmp.SetResolution(Dpi, Dpi); using (var graphics = System.Drawing.Graphics.FromImage(bmp)) { graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; graphics.Clear(backColor); using (var ms = new MemoryStream()) { bmpEncoder.Save(ms); graphics.DrawImage(System.Drawing.Image.FromStream(ms), new System.Drawing.PointF(0, 0)); } graphics.Save(); } return(bmp); } catch (Exception ex) { Console.WriteLine(ex.Message); return(null); } }
private void printimage(System.Windows.Media.ImageSource bi) { var vis = new System.Windows.Media.DrawingVisual(); System.Windows.Media.DrawingContext dc = vis.RenderOpen(); dc.DrawImage(bi, new System.Windows.Rect { Width = bi.Width, Height = bi.Height }); dc.Close(); var pdialog = new System.Windows.Controls.PrintDialog(); // if (pdialog.ShowDialog() == true) { pdialog.PrintVisual(vis, "My Image"); } }
// Create a DrawingVisual that contains a rectangle. private System.Windows.Media.DrawingVisual CreateDrawingVisualRectangle() { System.Windows.Media.DrawingVisual drawingVisual = new System.Windows.Media.DrawingVisual(); // Retrieve the DrawingContext in order to create new drawing content. DrawingContext drawingContext = drawingVisual.RenderOpen(); // Create a rectangle and draw it in the DrawingContext. Rect rect = new Rect(new Point(160, 100), new Size(320, 80)); drawingContext.DrawRectangle(Brushes.LightBlue, null, rect); // Persist the drawing content. drawingContext.Close(); return(drawingVisual); }
/// <summary> /// Converts the framework element to drawing visual. /// </summary> /// <param name="element">The framework element that must be converted.</param> /// <param name="size">The size of the diagram.</param> /// <returns>A new DrawingVisual with the Diagram.</returns> internal static System.Windows.Media.DrawingVisual DrawingVisualFromFrameworkElement(System.Windows.FrameworkElement element, System.Windows.Size size) { System.Windows.Media.DrawingVisual dv = new System.Windows.Media.DrawingVisual(); System.Windows.Media.Imaging.RenderTargetBitmap renderBitmap = new System.Windows.Media.Imaging.RenderTargetBitmap( (int)size.Width, (int)size.Height, 96D, 96D, System.Windows.Media.PixelFormats.Default); renderBitmap.Render(element); using (System.Windows.Media.DrawingContext context = dv.RenderOpen()) { context.DrawImage(renderBitmap, new System.Windows.Rect(size)); } return dv; }
// Create a DrawingVisual that contains text. private System.Windows.Media.DrawingVisual CreateDrawingVisualText() { // Create an instance of a DrawingVisual. System.Windows.Media.DrawingVisual drawingVisual = new System.Windows.Media.DrawingVisual(); // Retrieve the DrawingContext from the DrawingVisual. DrawingContext drawingContext = drawingVisual.RenderOpen(); // Draw a formatted text string into the DrawingContext. drawingContext.DrawText( new FormattedText("Click Me!", CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, new Typeface("Verdana"), 36, Brushes.Black), new Point(200, 116)); // Close the DrawingContext to persist changes to the DrawingVisual. drawingContext.Close(); return(drawingVisual); }
/* * // 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); } }
public static void CreateRemappedRasterFromVector(string xamlFile, System.Windows.Media.Color newColor, System.IO.Stream stream, double height, double width, string dynamicText) { string remapBase = "ReMapMe_"; int max = 3; int index = 0; int missed = 0; Object temp = null; string dynamText = "DynamicText"; System.Windows.Controls.Canvas theVisual = System.Windows.Markup.XamlReader.Load(new System.IO.FileStream(xamlFile, System.IO.FileMode.Open)) as System.Windows.Controls.Canvas; if (null == theVisual) { return; } System.Windows.Size size = new System.Windows.Size(width, height); System.Windows.Media.DrawingVisual drawingVisual = new System.Windows.Media.DrawingVisual(); System.Windows.Media.VisualBrush visualBrush = new System.Windows.Media.VisualBrush(theVisual); System.Windows.Rect rect = new System.Windows.Rect(new System.Windows.Point(), size); while (missed < max) { string elementid = remapBase + index; temp = theVisual.FindName(elementid); if (null == temp) { missed++; index++; continue; } System.Windows.Media.SolidColorBrush scb = ((temp as System.Windows.Shapes.Path).Fill as System.Windows.Media.SolidColorBrush); System.Windows.Media.Color c = scb.Color; c.B = newColor.B; c.R = newColor.R; c.G = newColor.G; scb.Color = c; index++; } theVisual.Arrange(rect); theVisual.UpdateLayout(); using (System.Windows.Media.DrawingContext dc = drawingVisual.RenderOpen()) { dc.DrawRectangle(visualBrush, null, rect); } if (!dynamText.IsNullOrWhitespace()) { temp = theVisual.FindName(dynamText); if (null != temp) { //do dynamic text shit } } System.Windows.Media.Imaging.RenderTargetBitmap render = new System.Windows.Media.Imaging.RenderTargetBitmap((int)height, (int)width, 96, 96, System.Windows.Media.PixelFormats.Pbgra32); render.Render(drawingVisual); System.Windows.Media.Imaging.PngBitmapEncoder encoder = new System.Windows.Media.Imaging.PngBitmapEncoder(); encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(render)); encoder.Save(stream); stream.Flush(); stream.Close(); visualBrush = null; drawingVisual = null; theVisual = null; render = null; encoder = null; }
public override DocumentPage GetPage(int pageNumber) { // This is called starting at zero, but we are really starting at the given page. pageNumber += startingPage; if (showProgressDialog) { if (outer.controller.UpdateProgressDialog(string.Format(MiscText.PrintingPage, pageNumber + 1, outer.totalPages), (double)pageNumber / (double)outer.totalPages)) { throw new Exception(MiscText.CancelledByUser); } } Margins margins = new Margins(this.margins.Left, this.margins.Right, this.margins.Top, this.margins.Bottom); bool landscape = outer.pageSettings.Landscape; PaperSize paperSize = outer.pageSettings.PaperSize; bool rotate; outer.ChangePageSettings(pageNumber, ref landscape, ref paperSize, margins); rotate = (landscape != outer.pageSettings.Landscape); this.pageSize = new System.Windows.Size(HundrethsToPoints(paperSize.Width), HundrethsToPoints(paperSize.Height)); if (outer.pageSettings.Landscape) { this.pageSize = new System.Windows.Size(this.pageSize.Height, this.pageSize.Width); } // Get margins in terms of normal page orientation, in points. double leftMargin = HundrethsToPoints(rotate ? margins.Bottom : margins.Left); double rightMargin = HundrethsToPoints(rotate ? margins.Top : margins.Right); double topMargin = HundrethsToPoints(rotate ? margins.Left : margins.Top); double bottomMargin = HundrethsToPoints(rotate ? margins.Right : margins.Bottom); System.Windows.Rect contentRect = new System.Windows.Rect(leftMargin, topMargin, pageSize.Width - leftMargin - rightMargin, pageSize.Height - topMargin - bottomMargin); System.Windows.Rect boundingRect = new System.Windows.Rect(0, 0, pageSize.Width - leftMargin - rightMargin, pageSize.Height - topMargin - bottomMargin); System.Windows.Media.DrawingVisual visual = new System.Windows.Media.DrawingVisual(); using (System.Windows.Media.DrawingContext dc = visual.RenderOpen()) { if (outer.printingToBitmaps) { // This is kind of hacky way to get the printing to bitmaps white, but much easier than the alternative. dc.DrawRectangle(System.Windows.Media.Brushes.White, null, new System.Windows.Rect(-1, -1, pageSize.Width + 2, pageSize.Height + 2)); } // Clip to the bounding rect within margins. dc.PushClip(new System.Windows.Media.RectangleGeometry(boundingRect)); if (rotate) { // Rotate and translate to handle landscape mode. dc.PushTransform(new System.Windows.Media.TranslateTransform(0, boundingRect.Height)); dc.PushTransform(new System.Windows.Media.RotateTransform(-90)); } // Scale to hundreths of an inch instead of points (1/96 of inch). dc.PushTransform(new System.Windows.Media.ScaleTransform(96.0 / 100.0, 96.0 / 100.0)); IGraphicsTarget graphicsTarget; if (outer.colorModel == ColorModel.RGB) { graphicsTarget = new WPF_GraphicsTarget(dc); } else if (outer.colorModel == ColorModel.CMYK) { graphicsTarget = new WPF_GraphicsTarget(dc, new WPFSwopColorConverter()); } else { throw new NotImplementedException(); } using (graphicsTarget) { outer.DrawPage(graphicsTarget, pageNumber, new SizeF((float)contentRect.Width, (float)contentRect.Height), dpi); } graphicsTarget = null; } return(new DocumentPage(visual, pageSize, contentRect, contentRect)); }
private System.Windows.Media.DrawingVisual CreateDrawingVisualProvince(Prov prov, MapColorModes mapColor = MapColorModes.Provs) { var drawingVisual = new System.Windows.Media.DrawingVisual(); var dc = drawingVisual.RenderOpen(); Brush brush = null; if (mapColor == MapColorModes.Provs) { var color1 = System.Drawing.Color.FromArgb(prov.Color); brush = new SolidColorBrush(Color.FromRgb(color1.R, color1.G, color1.B)); } else if (mapColor == MapColorModes.Country) { var color = Logic.GetCountryColor(prov.Owner); if (color == null) { if (prov.IsLake || prov.IsSea) { var color1 = System.Drawing.Color.FromArgb(prov.Color); brush = new SolidColorBrush(Color.FromRgb(color1.R, color1.G, color1.B)); } else if (!prov.IsWaste) { brush = new SolidColorBrush(Colors.LightGray); } else { brush = new SolidColorBrush(Colors.DimGray); } } else { brush = new SolidColorBrush(color.Value); } } //dc.DrawGeometry(brush,new Pen(Brushes.Red,0),prov.Geometry ); dc.DrawGeometry(brush, new Pen(brush, 0.45), prov.Geometry); ////////////////////////////////////////// // FormattedText formattedText = new FormattedText( //"11", //CultureInfo.GetCultureInfo("en-us"), //FlowDirection.LeftToRight, //new Typeface(new FontFamily("宋体"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal), //12, //Brushes.Red); // Geometry textGeometry = formattedText.BuildGeometry(new Point(0, 0)); // var rect = prov.Geometry.Bounds; // var rect1 = textGeometry.Bounds; // var w = rect.Width/rect1.Width; // var h = rect.Height/rect1.Height; // var scale = Math.Min(w, h); // //var max=t // var translateTransform = new ScaleTransform(); // //translateTransform.X = -textGeometry.Bounds.Left; // //translateTransform.Y = -textGeometry.Bounds.Top; // dc.PushTransform(translateTransform); // dc.DrawGeometry(Brushes.Red, new Pen(Brushes.Red, 1.0), textGeometry); dc.Close(); return(drawingVisual); }
public override DocumentPage GetPage(int pageNumber) { // This is called starting at zero, but we are really starting at the given page. pageNumber += startingPage; if (showProgressDialog) { if (outer.controller.UpdateProgressDialog(string.Format(MiscText.PrintingPage, pageNumber + 1, outer.totalPages), (double)pageNumber / (double)outer.totalPages)) throw new Exception(MiscText.CancelledByUser); } Margins margins = new Margins(this.margins.Left, this.margins.Right, this.margins.Top, this.margins.Bottom); bool landscape = outer.pageSettings.Landscape; PaperSize paperSize = outer.pageSettings.PaperSize; bool rotate; outer.ChangePageSettings(pageNumber, ref landscape, ref paperSize, margins); rotate = (landscape != outer.pageSettings.Landscape); this.pageSize = new System.Windows.Size(HundrethsToPoints(paperSize.Width), HundrethsToPoints(paperSize.Height)); if (outer.pageSettings.Landscape) { this.pageSize = new System.Windows.Size(this.pageSize.Height, this.pageSize.Width); } // Get margins in terms of normal page orientation, in points. double leftMargin = HundrethsToPoints(rotate ? margins.Bottom : margins.Left); double rightMargin = HundrethsToPoints(rotate ? margins.Top : margins.Right); double topMargin = HundrethsToPoints(rotate ? margins.Left : margins.Top); double bottomMargin = HundrethsToPoints(rotate ? margins.Right : margins.Bottom); System.Windows.Rect contentRect = new System.Windows.Rect(leftMargin, topMargin, pageSize.Width - leftMargin - rightMargin, pageSize.Height - topMargin - bottomMargin); System.Windows.Rect boundingRect = new System.Windows.Rect(0, 0, pageSize.Width - leftMargin - rightMargin, pageSize.Height - topMargin - bottomMargin); System.Windows.Media.DrawingVisual visual = new System.Windows.Media.DrawingVisual(); using (System.Windows.Media.DrawingContext dc = visual.RenderOpen()) { if (outer.printingToBitmaps) { // This is kind of hacky way to get the printing to bitmaps white, but much easier than the alternative. dc.DrawRectangle(System.Windows.Media.Brushes.White, null, new System.Windows.Rect(-1, -1, pageSize.Width + 2, pageSize.Height + 2)); } // Clip to the bounding rect within margins. dc.PushClip(new System.Windows.Media.RectangleGeometry(boundingRect)); if (rotate) { // Rotate and translate to handle landscape mode. dc.PushTransform(new System.Windows.Media.TranslateTransform(0, boundingRect.Height)); dc.PushTransform(new System.Windows.Media.RotateTransform(-90)); } // Scale to hundreths of an inch instead of points (1/96 of inch). dc.PushTransform(new System.Windows.Media.ScaleTransform(96.0 / 100.0, 96.0 / 100.0)); IGraphicsTarget graphicsTarget; if (outer.colorModel == ColorModel.RGB) graphicsTarget = new WPF_GraphicsTarget(dc); else if (outer.colorModel == ColorModel.CMYK) graphicsTarget = new WPF_GraphicsTarget(dc, new WPFSwopColorConverter()); else throw new NotImplementedException(); using (graphicsTarget) { outer.DrawPage(graphicsTarget, pageNumber, new SizeF((float)contentRect.Width, (float)contentRect.Height), dpi); } graphicsTarget = null; } return new DocumentPage(visual, pageSize, contentRect, contentRect); }
/// <summary> /// 渲染字符串 /// </summary> /// <param name="previewText"></param> /// <param name="backColor">背景色</param> /// <param name="foreColor">前景色</param> /// <param name="fontSize">font size in pixel</param> /// <returns></returns> public System.Drawing.Bitmap RenderString(string previewText, System.Drawing.Color backColor, System.Drawing.Color foreColor, int fontSize) { try { if (string.IsNullOrWhiteSpace(previewText)) { if (_glyphTypeface.FamilyNames.Count == 0) { previewText = "no name"; } else if (_glyphTypeface.FamilyNames.ContainsKey(CultureInfo.CurrentCulture)) { previewText = _glyphTypeface.FamilyNames[CultureInfo.CurrentCulture]; } else { previewText = _glyphTypeface.FamilyNames[_glyphTypeface.FamilyNames.Keys.First()]; } } var glyphIndexList = GetGlyphIndexList(previewText); if (glyphIndexList.Count <= 0) { return(null); } var geometryList = glyphIndexList.Select(glyphIndex => _glyphTypeface.GetGlyphOutline(glyphIndex, fontSize, 1d)).ToList(); var advanceWidthList = glyphIndexList.Select(glyphIndex => _glyphTypeface.AdvanceWidths[glyphIndex]).ToList(); var cellSize = new Size(fontSize, fontSize * _glyphTypeface.Height); var offsetX = 0d; var offsetY = fontSize * _glyphTypeface.Baseline; var drawingVisual = new System.Windows.Media.DrawingVisual(); var brush = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(foreColor.R, foreColor.G, foreColor.B)); //var pen = new System.Windows.Media.Pen(brush, 0); using (var dc = drawingVisual.RenderOpen()) { for (var i = 0; i < geometryList.Count; i++) { var geometry = geometryList[i]; var advanceWidth = advanceWidthList[i]; //if (geometry.IsEmpty()) continue; dc.PushTransform(new System.Windows.Media.TranslateTransform(offsetX, offsetY)); dc.DrawGeometry(brush, null, geometry); dc.Pop(); // get rid of the transform offsetX += advanceWidth * cellSize.Width; } } var bitWidth = (int)Math.Ceiling(offsetX); var bitHeight = (int)Math.Ceiling(cellSize.Height); var targetImage = new System.Windows.Media.Imaging.RenderTargetBitmap(bitWidth, bitHeight, Dpi, Dpi, System.Windows.Media.PixelFormats.Pbgra32); targetImage.Render(drawingVisual); var bmpEncoder = new System.Windows.Media.Imaging.PngBitmapEncoder(); bmpEncoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(targetImage)); //redraw to Bitmap var bmp = new System.Drawing.Bitmap(bitWidth, bitHeight); bmp.SetResolution(Dpi, Dpi); using (var graphics = System.Drawing.Graphics.FromImage(bmp)) { graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; graphics.Clear(backColor); using (var ms = new MemoryStream()) { bmpEncoder.Save(ms); graphics.DrawImage(System.Drawing.Image.FromStream(ms), new System.Drawing.PointF(0, 0)); } graphics.Save(); } return(bmp); } catch (Exception ex) { Console.WriteLine(ex.Message); return(null); } }