/// <summary> /// Gets the requested font family or falls back to using <c>Roboto</c>. /// </summary> /// <param name="fontFamily">The name of the font family to get.</param> /// <param name="size">The requested size of the returned font.</param> /// <param name="style">The requested style of the returned font.</param> /// <returns>Thr requested font, or the Roboto font if the requested font cannot be found.</returns> public static Font GetFont(string fontFamily, float size, FontStyle style = FontStyle.Regular) { // TODO: cache? if (fontFamily == Roboto) { // shortcut case return(BundledFonts.CreateFont(fontFamily, size, style)); } else if (SystemFonts.TryFind(fontFamily, out var family)) { // default case return(family.CreateFont(size, style)); } else { // fallback case return(BundledFonts.CreateFont(Roboto, size, style)); } }
/// <returns>Base64 image data</returns> public string GetNumberImage(int number) { var font = SystemFonts.CreateFont("Arial", 20, FontStyle.Bold); var text = number.ToString(); Image img = backGround; using var img2 = img.Clone(ctx => { var imgSize = ctx.GetCurrentSize(); var size = TextMeasurer.Measure(text, new RendererOptions(font)); ctx.DrawText(text, font, Color.White, new PointF(imgSize.Width / 2 - size.Width / 2, imgSize.Height / 2 - size.Height / 2)); }); using var memoryStream = new MemoryStream(); img2.Save(memoryStream, new PngEncoder()); var base64 = Convert.ToBase64String(memoryStream.ToArray()); return("data:image/png;base64, " + base64); }
public void SystemFont_Get_ReturnsExpected_WindowsNames(Func <Font> getFont, string systemFontName, string windowsFontName) { using (Font font = getFont()) using (Font otherFont = getFont()) using (Font fontFromName = SystemFonts.GetFontByName(systemFontName)) { Assert.NotSame(font, otherFont); Assert.Equal(font, otherFont); Assert.Equal(font, fontFromName); Assert.Equal(systemFontName, font.SystemFontName); // Windows 8 updated some system fonts. if (!PlatformDetection.IsWindows7) { Assert.Equal(windowsFontName, font.Name); } } }
public TextureBrushesSection2() { image = TestIcons.Textures; var drawable = new Drawable(); var drawableTarget = new DrawableTarget(drawable); var layout = new DynamicLayout { Padding = new Padding(10) }; layout.AddSeparateRow(null, drawableTarget.Checkbox(), null); layout.Add(drawable); this.Content = layout; var w = image.Size.Width / 3; // same as height var img = image.Clone(new Rectangle(w, w, w, w)); var textureBrush = new TextureBrush(img); var solidBrush = new SolidBrush(Colors.Blue); var linearGradientBrush = new LinearGradientBrush(Colors.White, Colors.Black, PointF.Empty, new PointF(0, 100), Platform); var font = SystemFonts.Default(); drawable.BackgroundColor = Colors.Green; drawable.MouseMove += HandleMouseMove; drawable.MouseDown += HandleMouseMove; drawable.Paint += (s, e) => { var graphics = drawableTarget.BeginDraw(e); graphics.DrawText(font, Colors.White, 3, 3, "Move the mouse in this area to move the shapes."); // texture brushes var temp = location; DrawShapes(textureBrush, temp, img.Size, graphics); // solid brushes temp = temp + new PointF(200, 0); DrawShapes(solidBrush, temp, img.Size, graphics); // linear gradient brushes temp = temp + new PointF(200, 0); DrawShapes(linearGradientBrush, temp, img.Size, graphics); drawableTarget.EndDraw(e); }; }
public FlowsheetObjectPanelItem() { int padding = (int)(GlobalSettings.Settings.UIScalingFactor * 5); int height = (int)(GlobalSettings.Settings.UIScalingFactor * 70); int iconsize = height - (int)(GlobalSettings.Settings.UIScalingFactor * 6) * padding; Size = new Size(width, height); imgIcon = new ImageView() { Size = new Eto.Drawing.Size(iconsize, iconsize) }; txtName = new Label() { Text = "Name", Font = SystemFonts.Bold() }; txtDescription = new Label() { Text = "Description", Size = new Size(padding + width - (int)(GlobalSettings.Settings.UIScalingFactor * 10) - iconsize, height - (int)(GlobalSettings.Settings.UIScalingFactor * 20)) }; txtName.Font = new Font(SystemFont.Bold, DWSIM.UI.Shared.Common.GetEditorFontSize()); txtDescription.Font = new Font(SystemFont.Default, DWSIM.UI.Shared.Common.GetEditorFontSize()); Add(imgIcon, padding, 3 * padding); Add(txtName, padding + iconsize + (int)(GlobalSettings.Settings.UIScalingFactor * 6), padding); Add(txtDescription, padding + iconsize + (int)(GlobalSettings.Settings.UIScalingFactor * 6), padding + (int)(GlobalSettings.Settings.UIScalingFactor * 16)); MouseEnter += FlowsheetObjectPanelItem_MouseEnter; MouseLeave += FlowsheetObjectPanelItem_MouseLeave; if (!GlobalSettings.Settings.DarkMode) { BackgroundColor = Colors.White; } else { BackgroundColor = Colors.Black; } }
public byte[] GetCaptcha(string text) { var code = text; var r = new Random(); using var image = new Image <Rgba32>(_width, _height); var font = SystemFonts.CreateFont(SystemFonts.Families.First().Name, 25, FontStyle.Bold); image.Mutate(ctx => { ctx.Fill(Color.White); for (int i = 0; i < code.Length; i++) { ctx.DrawText(code[i].ToString(), font, Colors[r.Next(Colors.Length)], new PointF((this._width - 10) * i / code.Length + 5, r.Next(this._height / 5, this._height / 4)) ); } for (int i = 0; i < 5; i++) { var pen = new Pen(Colors[r.Next(Colors.Length)], 1); var p1 = new PointF(r.Next(_width), r.Next(_height)); var p2 = new PointF(r.Next(_width), r.Next(_height)); ctx.DrawLines(pen, p1, p2); } for (int i = 0; i < 20; i++) { var pen = new Pen(Colors[r.Next(Colors.Length)], 1); var p1 = new PointF(r.Next(_width), r.Next(_height)); var p2 = new PointF(p1.X + 1f, p1.Y + 1f); ctx.DrawLines(pen, p1, p2); } }); using var ms = new System.IO.MemoryStream(); image.SaveAsPng(ms); return(ms.ToArray()); }
public override object ProvideValue(IServiceProvider serviceProvider) { if (SystemFont != null) { return(new Font(SystemFont.Value, Size, Decoration)); } var size = Size ?? SystemFonts.Default().Size; var familyName = Family ?? SystemFonts.Default().FamilyName; var family = new FontFamily(familyName); if (!string.IsNullOrEmpty(Typeface)) { var typeface = family.Typefaces.FirstOrDefault(r => string.Equals(r.Name, Typeface, StringComparison.OrdinalIgnoreCase)); if (typeface != null) { return(new Font(typeface, size, Decoration)); } } return(new Font(family, size, Style, Decoration)); }
public static Image <Rgba32> CreateCharvatar(string characters, int width = 100, int height = 100, string fontFamilyName = "Arial", float fontEmSize = 70, Rgba32?textColor = null) { var image = new Image <Rgba32>(width, height); var backgroundColor = ColorHelper.GenerateRandomColor(Rgba32.White); image.Mutate(x => x.BackgroundColor(backgroundColor)); image.Mutate(x => x.Brightness((float)0.7)); image.Mutate(x => x.Contrast((float)1.8)); var textGraphicsOptions = new TextGraphicsOptions(true) { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center }; var fontFamily = SystemFonts.Find(fontFamilyName); var font = new Font(fontFamily, fontEmSize); var center = new PointF(image.Width / 2, image.Height / 2); image.Mutate(x => x.DrawText(textGraphicsOptions, characters, font, textColor ?? Rgba32.White, center)); return(image); }
public static Label CreateAndAddTwoLabelsRow2(this DynamicLayout container, String text1, String text2) { var txt = new Label { Text = text1, VerticalAlignment = VerticalAlignment.Center, Font = SystemFonts.Bold(null, FontDecoration.None) }; txt.Font = new Font(SystemFont.Default, GetEditorFontSize()); var txt2 = new Label { Text = text2, Width = 350, VerticalAlignment = VerticalAlignment.Center }; txt2.Font = new Font(SystemFont.Default, GetEditorFontSize()); var tr = new TableRow(txt, null, txt2); container.AddRow(tr); container.CreateAndAddEmptySpace(); return(txt2); }
public string GetNavComImage(string type, bool dependant, string value1 = null, string value2 = null, bool showMainOnly = false, bool valid = false) { var font = SystemFonts.CreateFont("Arial", 17, FontStyle.Regular); var valueFont = SystemFonts.CreateFont("Arial", showMainOnly ? 26 : 13, FontStyle.Regular); Image img = backGround; using var img2 = img.Clone(ctx => { var imgSize = ctx.GetCurrentSize(); if (!string.IsNullOrWhiteSpace(type)) { var size = TextMeasurer.Measure(type, new RendererOptions(font)); Color displayColor = dependant ? Color.White : Color.LightGray; ctx.DrawText(type, font, displayColor, new PointF(imgSize.Width / 2 - size.Width / 2, imgSize.Height / 4)); } if (!string.IsNullOrWhiteSpace(value1)) { var size1 = TextMeasurer.Measure(value1, new RendererOptions(valueFont)); Color displayColor = dependant ? Color.Yellow : Color.LightGray; ctx.DrawText(value1, valueFont, displayColor, new PointF(imgSize.Width / 2 - size1.Width / 2, imgSize.Height / 2)); } if (!string.IsNullOrWhiteSpace(value2) && !showMainOnly) { var size2 = TextMeasurer.Measure(value2, new RendererOptions(valueFont)); Color displayColor = dependant ? Color.White : Color.LightGray; if (string.IsNullOrWhiteSpace(value1)) { displayColor = valid ? Color.Green : Color.Red; } ctx.DrawText(value2, valueFont, displayColor, new PointF(imgSize.Width / 2 - size2.Width / 2, imgSize.Height / 2 + size2.Height + 2)); } }); using var memoryStream = new MemoryStream(); img2.Save(memoryStream, new PngEncoder()); var base64 = Convert.ToBase64String(memoryStream.ToArray()); return("data:image/png;base64, " + base64); }
public static IImageProcessingContext <TPixel> ApplyFaceInfo <TPixel>(this IImageProcessingContext <TPixel> processingContext, IList <FaceDetails> faces, TPixel color, TPixel textColor) where TPixel : struct, IPixel <TPixel> { return(processingContext.Apply(img => { var gfxOptions = new GraphicsOptions(true) { }; var gfxTextOptions = new TextGraphicsOptions(true) { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center }; var font = SystemFonts.CreateFont("Arial", 39, FontStyle.Bold); foreach (var face in faces) { PointF left_top = new PointF((float)face.BoundingBox.X, (float)face.BoundingBox.Y); PointF left_bottom = new PointF((float)face.BoundingBox.X, (float)face.BoundingBox.Y + (float)face.BoundingBox.Height); PointF right_top = new PointF((float)face.BoundingBox.X + (float)face.BoundingBox.Width, (float)face.BoundingBox.Y); PointF right_bottom = new PointF((float)face.BoundingBox.X + (float)face.BoundingBox.Width, (float)face.BoundingBox.Y + (float)face.BoundingBox.Height); PointF[] boundingBox = new PointF[] { left_top, right_top, right_bottom, left_bottom }; img.Mutate(i => i.DrawPolygon(color, 5f, boundingBox, gfxOptions)); PointF text_Age_pos = new PointF((float)face.BoundingBox.X + (float)face.BoundingBox.Width / 2, (float)face.BoundingBox.Y - 45f); SizeF size = TextMeasurer.Measure($"{face.Age}", new RendererOptions(font) { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, Origin = text_Age_pos }); PointF[] text_Background_Age = new PointF[] { new PointF(text_Age_pos.X - size.Width / 2 - 20f, text_Age_pos.Y - 20f), new PointF(text_Age_pos.X + size.Width / 2 + 20f, text_Age_pos.Y - 20f), new PointF(text_Age_pos.X + size.Width / 2 + 20f, text_Age_pos.Y + size.Height - 10f), new PointF(text_Age_pos.X - size.Width / 2 - 20f, text_Age_pos.Y + size.Height - 10f) }; img.Mutate(i => i.FillPolygon(color, text_Background_Age, gfxOptions)); img.Mutate(i => i.DrawText($"{face.Age}", font, textColor, text_Age_pos, gfxTextOptions)); PointF text_Emotion_pos = new PointF((float)face.BoundingBox.X + (float)face.BoundingBox.Width / 2, (float)face.BoundingBox.Y + (float)face.BoundingBox.Height + 45f); size = TextMeasurer.Measure($"{face.Emotion}", new RendererOptions(font)); PointF[] text_Background_Emotion = new PointF[] { new PointF(text_Emotion_pos.X - size.Width / 2 - 20f, text_Emotion_pos.Y - 20f), new PointF(text_Emotion_pos.X + size.Width / 2 + 20f, text_Emotion_pos.Y - 20f), new PointF(text_Emotion_pos.X + size.Width / 2 + 20f, text_Emotion_pos.Y + size.Height - 10f), new PointF(text_Emotion_pos.X - size.Width / 2 - 20f, text_Emotion_pos.Y + size.Height - 10f) }; img.Mutate(i => i.FillPolygon(color, text_Background_Emotion, gfxOptions)); img.Mutate(i => i.DrawText($"{face.Emotion}", font, textColor, text_Emotion_pos, gfxTextOptions)); } })); }
static void Main(string[] args) { var raw = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; using var img = new Image <Rgba32>(144, 144); var font = SystemFonts.CreateFont("Consolas", 24, FontStyle.Regular); var size = TextMeasurer.Measure("H", new RendererOptions(font)); var tgo = new TextGraphicsOptions { HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Top, DpiX = 72, DpiY = 72, }; var go = new GraphicsOptions { }; var y = 0; for (var i = 0; i < raw.Length; i++) { var x = (i % 6) * 24; if (i % 6 == 0) { y = (int)(i / 6 * 24); } var i1 = i; var y1 = y; img.Mutate(ctx => ctx .Fill(go, Color.Green, new RectangleF(x, y1, 24, 24)) .DrawText(tgo, raw[i1].ToString(), font, Color.Black, new PointF(x, y1))); } img.Save("temp.png"); }
private void openPic_Click(object sender, EventArgs e) { if (this.openPicDialog.ShowDialog(this) != DialogResult.OK) { return; } using var image = Image.Load <Rgb24>(this.openPicDialog.FileName); var timer = Stopwatch.StartNew(); ObjectDetectionResult[] detections = YOLO.Detect(this.infer, supportedSize: new Size(MS_COCO.InputSize, MS_COCO.InputSize), image: image); timer.Stop(); image.Mutate(context => { var font = SystemFonts.CreateFont("Arial", 16); var textColor = Color.White; var boxPen = new Pen(Color.White, width: 4); foreach (var detection in detections) { string className = detection.Class < MS_COCO.ClassCount && detection.Class >= 0 ? MS_COCO.ClassNames[detection.Class] : "imaginary class"; string text = $"{className}: {detection.Score:P0}"; var box = Scale(detection.Box, image.Size()); context.DrawText(text, font, textColor, TopLeft(box)); var drawingBox = new RectangularPolygon(box); context.Draw(boxPen, drawingBox); } }); using var temp = new MemoryStream(); image.SaveAsBmp(temp); temp.Position = 0; this.pictureBox.Image = new System.Drawing.Bitmap(temp); this.Text = "YOLO " + string.Join(", ", detections.Select(d => MS_COCO.ClassNames[d.Class])) + " in " + timer.ElapsedMilliseconds + "ms"; }
private void InitializeComponent() { Orientation = Orientation.Horizontal; Padding = new Padding(14, 7, 7, 7); Spacing = 7; _titleLabel = new Label(); _titleLabel.TextColor = Color.Parse("#42484a"); _titleLabel.Font = SystemFonts.Label(16); _titleLayout = new StackLayout(); _titleLayout.Orientation = Orientation.Vertical; _titleLayout.Padding = new Padding(0, 10); _titleLayout.Items.Add(_titleLabel); _photoBox = new CircularImageView(); _photoBox.Size = new Size(65, 65); _settingsButton = new Button(); _settingsButton.Image = Bitmap.FromResource("preferences"); _settingsButton.Width = 40; _settingsButton.Height = 40; _settingsButton.Click += OnPreferencesButtonClick; _exportButton = new Button(); _exportButton.Image = Bitmap.FromResource("export"); _exportButton.Width = 40; _exportButton.Height = 40; _exportButton.Click += OnExportButtonClick; Items.Add(new StackLayoutItem(_photoBox)); Items.Add(new StackLayoutItem(_titleLayout, HorizontalAlignment.Left, true)); Items.Add(new StackLayoutItem(_exportButton, HorizontalAlignment.Right) { VerticalAlignment = VerticalAlignment.Center }); Items.Add(new StackLayoutItem(_settingsButton, HorizontalAlignment.Right) { VerticalAlignment = VerticalAlignment.Center }); }
static void Main(string[] args) { List<Rgba32> colors = new List<Rgba32>(); using (var fs = File.OpenRead(@"L:\Sync\MHW Mods\chunks_v0\chunk\common\face_edit\face_edit_palette.pal")) using (BinaryReader br = new BinaryReader(fs)) { char[] magic = br.ReadChars(3); fs.Position += 5; // Padding while (fs.Position < fs.Length) { byte[] b = br.ReadBytes(4); colors.Add(new Rgba32(b[0], b[1], b[2], b[3])); } } int width = 500; int rowHeight = 100; int height = rowHeight * colors.Count; using (var img = new Image<Rgba32>(width, height)) { var font = SystemFonts.CreateFont("Arial", 24); img.Mutate(gfx => { for (int i = 0; i < colors.Count; i++) { int y = i * rowHeight; gfx.Fill(colors[i], new Rectangle(0, y, width, rowHeight)); gfx.DrawText(colors[i].ToHex(), font, Rgba32.White, new PointF(10, y + 20)); } }); img.Save("face_edit_palette.png"); } var jsonColors = colors.Select(x => x.ToHex()); var json = JsonConvert.SerializeObject(jsonColors); File.WriteAllText("palette.json", json); }
public async Task Magic8(CommandContext ctx, [Description("Question to answer")][RemainingText] string question) { if (ctx.Channel.Get(ConfigManager.Enabled) .And(ctx.Channel.GetMethodEnabled())) { await ctx.TriggerTypingAsync(); Rectangle size = new Rectangle(0, 0, 400, 400); Bitmap bmp = new Bitmap(size.Size, PixelFormat.Format32bppRgb); Graphics g = new Graphics(bmp); //Background g.Clear(Colors.White); //Main Circle g.FillEllipse(Brushes.Black, size); //Center circle size.Width /= 2; size.Height /= 2; size.X = size.Width / 2; size.Y = size.Height / 2; g.DrawEllipse(new Pen(Eto.Drawing.Color.FromArgb(100, 80, 80, 80), 6), size); //Triangle PointF center = new PointF(size.X + size.Width / 2, size.Y + size.Height / 2); float radius = size.Width / 2f; g.FillPolygon(Brushes.Blue, new PointF(center.X - 0.866f * radius, center.Y - 0.5f * radius), new PointF(center.X + 0.866f * radius, center.Y - 0.5f * radius), new PointF(center.X, center.Y + radius)); Font font = SystemFonts.Default(); font = new Font(font.Family, font.Size * (180f / g.MeasureString(font, "QWERTBTESTSTR").Width)); string answer = AnswerList[Program.Rnd.Next(AnswerList.Length)]; size.Top = (int)System.Math.Round(size.Center.Y - g.MeasureString(font, answer).Height / 2); g.DrawText(font, Brushes.White, size, answer, FormattedTextWrapMode.Word, FormattedTextAlignment.Center); g.Flush(); g.Dispose(); await using MemoryStream str = new MemoryStream(); bmp.Save(str, ImageFormat.Jpeg); str.Position = 0; await ctx.RespondWithFileAsync("Magic8.jpg", str); } }
public void Recreate() { var device = _renderer.Device; if (texture != null) { RemoveAndDispose(ref textureView); RemoveAndDispose(ref texture); RemoveAndDispose(ref textCache); } textCache = AddDisposable(new TextCache(device)); Font = SystemFonts.CreateFont(FontName, FontSize, FontStyle); texture = AddDisposable(textCache.GetTextTexture(Content, Font, TextAlignment, Color, Size)); textureView = AddDisposable(device.ResourceFactory.CreateTextureView(texture)); textureSet = AddDisposable(device.ResourceFactory.CreateResourceSet(new ResourceSetDescription( _renderer.Shader.TextureLayout, textureView, device.Aniso4xSampler))); }
public void FontDialogFromFormShouldWork() { bool wasChanged = false; Font selectedFont; FontDialog fd = null; // don't let this GC until after the test ManualForm("Click on the label and change the font", form => { selectedFont = SystemFonts.User(); var label = new Label { Text = selectedFont.FamilyName, TextColor = SystemColors.Highlight }; label.MouseDown += (sender, e) => { fd = new FontDialog(); fd.Font = selectedFont; fd.FontChanged += (sender2, e2) => { selectedFont = fd.Font; label.Text = fd.Font.FamilyName; wasChanged = true; }; fd.ShowDialog(label); Application.Instance.AsyncInvoke(() => { GC.Collect(); GC.WaitForPendingFinalizers(); }); }; return(new StackLayout { Items = { label, new TextArea() }, Padding = 10 }); }); Assert.IsTrue(wasChanged, "#1 - Font was not changed!"); }
public static (string code, byte[] bytes) GenVCode(int num) { var code = GenCode(num); var r = new Random(); using var image = new Image <Rgba32>(Width, Height); var font = SystemFonts.CreateFont(SystemFonts.Families.First().Name, 25, FontStyle.Bold); image.Mutate(ctx => { ctx.Fill(Color.White); for (int i = 0; i < code.Length; i++) { ctx.DrawText(code[i].ToString(), font, Colors[r.Next(Colors.Length)], new PointF(20 * i + 5, r.Next(2, 12))); } for (int i = 0; i < 5; i++) { var pen = new Pen(Colors[r.Next(Colors.Length)], 1); var p1 = new PointF(r.Next(Width), r.Next(Height)); var p2 = new PointF(r.Next(Width), r.Next(Height)); ctx.DrawLines(pen, p1, p2); } for (int i = 0; i < 20; i++) { var pen = new Pen(Colors[r.Next(Colors.Length)], 1); var p1 = new PointF(r.Next(Width), r.Next(Height)); var p2 = new PointF(p1.X + 1f, p1.Y + 1f); ctx.DrawLines(pen, p1, p2); } }); using var ms = new System.IO.MemoryStream(); image.SaveAsPng(ms); return(code, ms.ToArray()); }
public void DrawableWithCanFocusShouldGetFirstMouseDownOnInactiveWindow() { bool wasClicked = false; bool gotFocusBeforeClick = false; Form(form => { var drawable = new Drawable(); var font = SystemFonts.Default(); drawable.Paint += (sender, e) => { e.Graphics.FillRectangle(Colors.Blue, 0, 0, drawable.Width, drawable.Height); e.Graphics.DrawText(font, SystemColors.ControlText, 0, 0, "Clicking once on this control should close the form"); }; drawable.Size = new Size(350, 200); drawable.CanFocus = true; drawable.MouseDown += (sender, e) => { wasClicked = true; form.Close(); }; form.Content = drawable; form.GotFocus += (sender, e) => { Application.Instance.AsyncInvoke(() => { if (!wasClicked) { gotFocusBeforeClick = true; } }); }; form.ShowActivated = false; form.Owner = Application.Instance.MainForm; }, -1); Assert.IsTrue(wasClicked, "#1 Drawable didn't get clicked"); Assert.IsFalse(gotFocusBeforeClick, "#2 Form should not have got focus before MouseDown event"); }
public RenderSurface(IImageProcessingContext context, int width, int height, bool debugMode) { Context = context ?? throw new ArgumentNullException(nameof(context)); SurfaceWidth = width; SurfaceHeight = height; DebugMode = debugMode; // This draws red rectangles around the text in DrawText fonts = new Dictionary <FontSize, Font> { [FontSize.Small] = SystemFonts.CreateFont("Arial", (float)FontSize.Small), [FontSize.Medium] = SystemFonts.CreateFont("Arial", (float)FontSize.Medium) }; // Make a red -> green palette for the progress bar palette = Enumerable.Range(0, 101).Select(i => { var ratio = i / 100f; var r = 1f - ratio; var g = ratio; return(new Color(new Vector4(r, g, 0f, 1f))); }).ToArray(); }
private Container GroupDeviceSelection() { return(new Panel { //Text = "Device Selection", Content = new StackLayout { Orientation = Orientation.Horizontal, VerticalContentAlignment = VerticalAlignment.Center, //Padding = 5, Spacing = 5, Items = { new Label { Text = "Device:", Visible = false }, (_dropDownSerialPort = new DropDown{ Width = 256, }), (_btnRefresh = new Button{ Text = "Refresh", }), (_btnRun = new Button{ Text = "Run" }), new StackLayoutItem { Control = (_lblConnected = new Label { TextAlignment = TextAlignment.Center, Font = SystemFonts.Bold(), Visible = false, }), Expand = true, }, }, }, }); }
public string GetNavComActionLabel(string label, bool error = false) { var font = SystemFonts.CreateFont("Arial", 20, FontStyle.Bold); Image img = backGround; using var img2 = img.Clone(ctx => { var imgSize = ctx.GetCurrentSize(); if (label != null) { var size = TextMeasurer.Measure(label, new RendererOptions(font)); ctx.DrawText(label, font, error ? Color.Red : Color.White, new PointF(imgSize.Width / 2 - size.Width / 2, imgSize.Height / 2)); } }); using var memoryStream = new MemoryStream(); img2.Save(memoryStream, new PngEncoder()); var base64 = Convert.ToBase64String(memoryStream.ToArray()); return("data:image/png;base64, " + base64); }
private byte[] CreateImageFromMessage(string message) { using var theImage = new Image <Rgba32>(1000, 500); // foreach (var theInstalledFont in SystemFonts.Collection.Families) { // Console.WriteLine(theInstalledFont.Name); // } Font theFont = SystemFonts.CreateFont("Noto Mono", 12, FontStyle.Regular); var theTextGraphicsOptions = new TextGraphicsOptions(true) { // draw the text along the path wrapping at the end of the line WrapTextWidth = 1000 }; // lets generate the text as a set of vectors drawn along the path IPathCollection theGlyphs = TextBuilder.GenerateGlyphs( message, new PointF(10.0f, 10.0f), new RendererOptions(theFont, theTextGraphicsOptions.DpiX, theTextGraphicsOptions.DpiY) { HorizontalAlignment = theTextGraphicsOptions.HorizontalAlignment, TabWidth = theTextGraphicsOptions.TabWidth, VerticalAlignment = theTextGraphicsOptions.VerticalAlignment, WrappingWidth = theTextGraphicsOptions.WrapTextWidth, ApplyKerning = theTextGraphicsOptions.ApplyKerning } ); theImage.Mutate(ctx => ctx .Fill(Rgba32.White) .Fill((GraphicsOptions)theTextGraphicsOptions, Rgba32.Black, theGlyphs)); using var theMemoryStream = new MemoryStream(); theImage.SaveAsPng(theMemoryStream); return(theMemoryStream.GetBuffer()); }
public void DrawString(String str, int x, int y) { if (string.IsNullOrEmpty(str)) { return; } // TODO-Fonts: Fallback for missing font Font excelFont = new Font(SystemFonts.Get(font.Name.Equals("SansSerif") ? "Arial" : font.Name), (int)(font.Size / verticalPixelsPerPoint), font.FontMetrics.Description.Style); { int width = (int)((TextMeasurer.Measure(str, new TextOptions(excelFont)).Width * 8) + 12); int height = (int)((font.Size / verticalPixelsPerPoint) + 6) * 2; y -= Convert.ToInt32((font.Size / verticalPixelsPerPoint) + 2 * verticalPixelsPerPoint); // we want to Draw the shape from the top-left HSSFTextbox textbox = escherGroup.CreateTextbox(new HSSFChildAnchor(x, y, x + width, y + height)); textbox.IsNoFill = (true); textbox.LineStyle = LineStyle.None; HSSFRichTextString s = new HSSFRichTextString(str); HSSFFont hssfFont = MatchFont(excelFont); s.ApplyFont(hssfFont); textbox.String = (s); } }
public GetPixelSection() { var location = new Point(100, 100); var image = TestIcons.Textures; var drawable = new Drawable(); var drawableTarget = new DrawableTarget(drawable) { UseOffScreenBitmap = true }; this.Content = drawable; EventHandler <MouseEventArgs> mouseHandler = (s, e) => { location = new Point(e.Location); ((Control)s).Invalidate(); e.Handled = true; }; drawable.MouseMove += mouseHandler; drawable.MouseDown += mouseHandler; var font = SystemFonts.Default(); drawable.BackgroundColor = Colors.Green; drawable.Paint += (s, e) => { var graphics = drawableTarget.BeginDraw(e); var imageLocation = new PointF(100, 100); graphics.DrawText(font, Colors.White, 3, 3, "Move the mouse in this area to read the pixel color."); graphics.DrawImage(image, imageLocation); var loc = location - (Point)imageLocation; loc.Restrict(new Rectangle(image.Size)); var pixelColor = image.GetPixel(loc.X, loc.Y); graphics.DrawText(font, Colors.White, 3, 20, "Color: " + pixelColor); drawableTarget.EndDraw(e); }; }
private void DrawCustomGauge(bool top, string labelText, string value, float ratio, int img_width, float chevronSize, float width_margin, float chart_width, float min, float max, IImageProcessingContext ctx, bool hideHeader) { float.TryParse(value, out float floatValue); bool missingHeaderLabel = (labelText?.Length ?? 0) == 0; bool writeValueHeaderAndChevron = !hideHeader || (floatValue >= min && floatValue <= max); if (writeValueHeaderAndChevron && !missingHeaderLabel) { var pen = new Pen(Color.White, chevronSize + 1); var arrowStartX = (ratio * img_width) + width_margin; var arrowStartY = (HALF_WIDTH - ((chart_width / 2) * (top ? 1 : -1))); var arrowAddY = arrowStartY - ((chevronSize * 2) * (top ? 1 : -1)); var startPoint = new PointF(arrowStartX, arrowStartY); var right = new PointF(arrowStartX + chevronSize, arrowAddY); var left = new PointF(arrowStartX - chevronSize, arrowAddY); PointF[] needle = { startPoint, right, left, startPoint }; var valueText = value.ToString(); var textColor = (floatValue > max || floatValue < min) ? Color.Red : Color.White; var font = SystemFonts.CreateFont("Arial", chevronSize * 4, FontStyle.Regular); var size = TextMeasurer.Measure(valueText, new RendererOptions(font)); float adjustY = top ? Math.Abs(-5 - size.Height) : 5; arrowAddY = top ? arrowAddY - adjustY : arrowAddY + adjustY; var valuePoint = new PointF(HALF_WIDTH - size.Width / 2, arrowAddY); ctx.DrawText(valueText, font, textColor, valuePoint); ctx.DrawPolygon(pen, needle); var text = labelText != string.Empty ? labelText[0].ToString() : string.Empty; size = TextMeasurer.Measure(text, new RendererOptions(SystemFonts.CreateFont("Arial", chevronSize * 3, FontStyle.Regular))); startPoint.Y -= top ? size.Height : 0; startPoint.X -= size.Width / 2; ctx.DrawText(text, SystemFonts.CreateFont("Arial", chevronSize * 3, FontStyle.Regular), Color.Black, startPoint); } }
Control DropDownWithFonts() { var fontCache = new Dictionary <FontFamily, Font>(); var dropDown = new DropDown(); dropDown.DataStore = Fonts.AvailableFontFamilies.OrderBy(r => r.LocalizedName).ToList(); dropDown.ItemTextBinding = Binding.Property((FontFamily f) => f.LocalizedName); dropDown.FormatItem += (sender, e) => { if (e.Item is FontFamily family) { if (!fontCache.TryGetValue(family, out var font)) { if (Platform.IsGtk && !EtoEnvironment.Platform.IsLinux) { // gtksharp has issues getting font faces on !linux font = new Font(family, e.Font?.Size ?? SystemFonts.Default().Size); } else { var typeface = family.Typefaces.FirstOrDefault(); if (typeface != null && !typeface.IsSymbol && typeface.HasCharacterRange(32, 126)) { font = new Font(family, e.Font?.Size ?? SystemFonts.Default().Size); } else { font = SystemFonts.Default(); } } fontCache[family] = font; } e.Font = font; } }; return(dropDown); }
public Font GetOrCreateFont(string fontName, float fontSize, FontWeight fontWeight) { var key = new FontKey { FontName = fontName, FontSize = fontSize, FontWeight = fontWeight }; if (!_cachedFonts.TryGetValue(key, out var font)) { var alternatives = _fontFallbackSettings.GetFallbackList(fontName); var fontNameFound = alternatives .Prepend(fontName) .FirstOrDefault(name => SystemFonts.TryFind(name, out _)); if (fontNameFound != fontName) { Logger.Info($"Requesting font {fontName}, actually found {fontNameFound}"); } var fontStyle = fontWeight == FontWeight.Bold ? FontStyle.Bold : FontStyle.Regular; if (fontNameFound != null) { font = SystemFonts.CreateFont(fontNameFound, fontSize, fontStyle); } else { Logger.Info($"Will use embedded font as fallback for font {fontName}"); font = _fallbackFonts.CreateFont(FallbackEmbeddedFont, fontSize, fontStyle); } _cachedFonts.Add(key, font); } return(font); }
public static string CreatePlaceholderImage(int width, int height) { string base64 = ""; using (var image = new Image <Rgba32>(Configuration.Default, width, height, Rgba32.LightGray)) { int padding = 15; var text = width + " x " + height; var font = new Font(SystemFonts.Find("Calibri"), 36, FontStyle.Regular); var textGraphicsOptions = new TextGraphicsOptions(true) { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center }; SizeF textSize = TextMeasurer.Measure(text, new RendererOptions(font)); float scalingFactor = Math.Min(image.Width / textSize.Width, image.Height / textSize.Height); Font scaledFont = new Font(font, (scalingFactor * font.Size) - scalingFactor * padding); image.Mutate(x => x.DrawText(textGraphicsOptions, text, scaledFont, Color.DarkGray, new PointF(image.Width / 2, image.Height / 2))); base64 = image.ToBase64String(JpegFormat.Instance); } return(base64); }