public static string CreateTileBackground(this Project project) { Grid grid = new Grid(); Rectangle rect = new Rectangle(); rect.Width = 173; rect.Height = 173; rect.Fill = new SolidColorBrush(project.Color.ToColor()); TextBlock text = new TextBlock(); text.Text = "TASK"; text.Foreground = new SolidColorBrush(Colors.White); text.FontSize = 32; grid.Children.Add(rect); grid.Children.Add(text); grid.Arrange(new Rect(0d, 0d, 173d, 173d)); WriteableBitmap wbmp = new WriteableBitmap(grid, null); string tiledirectory = "tiles"; string fullPath = tiledirectory + @"/" + project.Name + ".jpg"; using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { if (!store.DirectoryExists(tiledirectory)) { store.CreateDirectory(tiledirectory); } using (var stream = store.OpenFile(fullPath, System.IO.FileMode.OpenOrCreate)) { wbmp.SaveJpeg(stream, 173, 173, 0, 100); } } return "isostore:/" + fullPath; }
/// <summary> /// Renders the brush. /// </summary> /// <param name="path"> /// The path. /// </param> /// <param name="brush"> /// The brush. /// </param> /// <param name="w"> /// The w. /// </param> /// <param name="h"> /// The h. /// </param> public static void RenderBrush(string path, Brush brush, int w, int h) { var ib = brush as ImageBrush; if (ib != null) { var bi = ib.ImageSource as BitmapImage; if (bi != null) { w = bi.PixelWidth; h = bi.PixelHeight; } } var bmp = new RenderTargetBitmap(w, h, 96, 96, PixelFormats.Pbgra32); var rect = new Grid { Background = brush, Width = 1, Height = 1, LayoutTransform = new ScaleTransform(w, h) }; rect.Arrange(new Rect(0, 0, w, h)); bmp.Render(rect); var encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bmp)); using (Stream stm = File.Create(path)) { encoder.Save(stm); } }
/// <summary> /// Export the specified plot model to an xaml string. /// </summary> /// <param name="model">The model.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="background">The background.</param> /// <returns>A xaml string.</returns> public static string ExportToString(PlotModel model, double width, double height, OxyColor background = null) { var g = new Grid(); if (background != null) { g.Background = background.ToBrush(); } var c = new Canvas(); g.Children.Add(c); var size = new Size(width, height); g.Measure(size); g.Arrange(new Rect(0, 0, width, height)); g.UpdateLayout(); var rc = new ShapesRenderContext(c) { UseStreamGeometry = false }; model.Update(); model.Render(rc, width, height); var sb = new StringBuilder(); using (var sw = new StringWriter(sb)) { var xw = XmlWriter.Create(sw, new XmlWriterSettings { Indent = true }); XamlWriter.Save(c, xw); } return sb.ToString(); }
void create() { Grid grid = new Grid(); grid.HorizontalAlignment = HorizontalAlignment.Center; grid.VerticalAlignment = VerticalAlignment.Center; grid.Background = null; Rating ratingControl = new Rating(); ratingControl.HorizontalAlignment = HorizontalAlignment.Left; ratingControl.VerticalAlignment = VerticalAlignment.Top; ratingControl.ItemCount = 5; ratingControl.Foreground = new SolidColorBrush(Colors.Red); ratingControl.Background = null; ratingControl.IsReadOnly = true; ratingControl.SelectionMode = RatingSelectionMode.Continuous; double scale = 0.6; ratingControl.LayoutTransform = new ScaleTransform(scale, scale); grid.Children.Add(ratingControl); int i = 0; double nrStars = 0; do { ratingControl.Value = nrStars; grid.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); grid.Arrange(new Rect(new Size(double.MaxValue, double.MaxValue))); Rect size = VisualTreeHelper.GetDescendantBounds(ratingControl); //grid.Arrange(new Rect(new Size(size.Width, size.Height))); RenderTargetBitmap bitmap = new RenderTargetBitmap((int)(size.Width * scale), (int)(size.Height * scale), 96, 96, PixelFormats.Default); Window dummy = new Window(); dummy.Content = grid; dummy.SizeToContent = SizeToContent.WidthAndHeight; dummy.Show(); bitmap.Render(ratingControl); RatingBitmap.Add(bitmap); nrStars += 1.0 / 5; BitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bitmap, null, null, null)); FileStream outputFile = new FileStream("d:\\" + i + "stars.png", FileMode.Create); encoder.Save(outputFile); i++; } while (nrStars <= 1); }
private void Button_Click(object sender, RoutedEventArgs e) { var icon = (sender as Button).DataContext as IconWrapper; var grid = new Grid {Height = 36, Width = 36, Background = Brushes.White}; var visual = new Rectangle {Fill = icon.Icon}; grid.Children.Add(visual); var renderBitmap = new RenderTargetBitmap(36, 36, 96d, 96d, PixelFormats.Pbgra32); var size = new Size(36, 36); grid.Measure(size); grid.Arrange(new Rect(size)); // Update the layout for the surface. This should flush out any layout queues that hold a reference. renderBitmap.Render(grid); Clipboard.SetImage(renderBitmap); }
public void RenderAndSave(ImageRendererArguments args, out string fileName) { var mainContainer = new Grid(); mainContainer.Children.Add(args.UiContainer); mainContainer.Measure(new Size(args.Width, args.Height)); mainContainer.Arrange(new Rect(0, 0, args.Width, args.Height)); mainContainer.UpdateLayout(); var encoder = new PngBitmapEncoder(); var render = RenderBitmap(mainContainer, args.Dpi); var workingDirectory = @"c:\temp"; fileName = Path.Combine(workingDirectory, $"dwg_{Guid.NewGuid()}.png"); render.Render(mainContainer); encoder.Frames.Add(BitmapFrame.Create(render)); using (var s = File.Open(fileName, FileMode.Create)) { encoder.Save(s); } }
public string ExecuteThread(FileItem item,string infile, string dest, ValuePairEnumerator configData) { try { var conf = new OverlayTransformViewModel(configData); using (var fileStream = new MemoryStream(File.ReadAllBytes(infile))) { BitmapDecoder bmpDec = BitmapDecoder.Create(fileStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); WriteableBitmap writeableBitmap = BitmapFactory.ConvertToPbgra32Format(bmpDec.Frames[0]); writeableBitmap.Freeze(); Grid grid = new Grid { Width = writeableBitmap.PixelWidth, Height = writeableBitmap.PixelHeight, ClipToBounds = true, SnapsToDevicePixels = true }; grid.UpdateLayout(); var size = new Size(writeableBitmap.PixelWidth, writeableBitmap.PixelWidth); grid.Measure(size); grid.Arrange(new Rect(size)); Image overlay = new Image(); Image image = new Image { Width = writeableBitmap.PixelWidth, Height = writeableBitmap.PixelHeight }; image.BeginInit(); image.Source = writeableBitmap; image.EndInit(); image.Stretch = Stretch.Fill; grid.Children.Add(image); grid.UpdateLayout(); string text = ""; if (!string.IsNullOrEmpty(conf.Text)) { Regex regPattern = new Regex(@"\[(.*?)\]", RegexOptions.Singleline); MatchCollection matchX = regPattern.Matches(conf.Text); text = matchX.Cast<Match>() .Aggregate(conf.Text, (current1, match) => item.FileNameTemplates.Where( template => String.Compare(template.Name, match.Value, StringComparison.InvariantCultureIgnoreCase) == 0).Aggregate(current1, (current, template) => current.Replace(match.Value, template.Value))); } TextBlock textBlock = new TextBlock { Text = text, Foreground = (SolidColorBrush) new BrushConverter().ConvertFromString(conf.FontColor), FontFamily = (FontFamily) new FontFamilyConverter().ConvertFromString(conf.Font), FontSize = conf.FontSize, Opacity = conf.Transparency/100.00 }; if (conf.A11) { textBlock.HorizontalAlignment = HorizontalAlignment.Left; textBlock.VerticalAlignment = VerticalAlignment.Top; } if (conf.A12) { textBlock.HorizontalAlignment = HorizontalAlignment.Center; textBlock.VerticalAlignment = VerticalAlignment.Top; } if (conf.A13) { textBlock.HorizontalAlignment = HorizontalAlignment.Right; textBlock.VerticalAlignment = VerticalAlignment.Top; } if (conf.A21) { textBlock.HorizontalAlignment = HorizontalAlignment.Left; textBlock.VerticalAlignment = VerticalAlignment.Center; } if (conf.A22) { textBlock.HorizontalAlignment = HorizontalAlignment.Center; textBlock.VerticalAlignment = VerticalAlignment.Center; } if (conf.A23) { textBlock.HorizontalAlignment = HorizontalAlignment.Right; textBlock.VerticalAlignment = VerticalAlignment.Center; } if (conf.A31) { textBlock.HorizontalAlignment = HorizontalAlignment.Left; textBlock.VerticalAlignment = VerticalAlignment.Bottom; } if (conf.A32) { textBlock.HorizontalAlignment = HorizontalAlignment.Center; textBlock.VerticalAlignment = VerticalAlignment.Bottom; } if (conf.A33) { textBlock.HorizontalAlignment = HorizontalAlignment.Right; textBlock.VerticalAlignment = VerticalAlignment.Bottom; } textBlock.Margin = new Thickness(conf.Margins); if (File.Exists(conf.OverlayFile)) { overlay.Source = BitmapLoader.Instance.LoadImage(conf.OverlayFile, 0, 0); overlay.Opacity = textBlock.Opacity; if (!conf.StrechOverlay) { overlay.HorizontalAlignment = textBlock.HorizontalAlignment; overlay.VerticalAlignment = textBlock.VerticalAlignment; overlay.Stretch = Stretch.None; } else { overlay.HorizontalAlignment = HorizontalAlignment.Stretch; overlay.VerticalAlignment = VerticalAlignment.Stretch; overlay.Stretch = Stretch.UniformToFill; } grid.Children.Add(overlay); grid.UpdateLayout(); } grid.Children.Add(textBlock); grid.UpdateLayout(); BitmapLoader.Save2Jpg( BitmapLoader.SaveImageSource(grid, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight), dest); } } catch (Exception ex) { Log.Error("Overlay Transform Plugin error ", ex); } return dest; }
void PrintOnClick(object sender, RoutedEventArgs args) { PrintDialog dlg = new PrintDialog(); if ((bool)dlg.ShowDialog().GetValueOrDefault()) { // Create Grid panel. Grid grid = new Grid(); // Define five auto-sized rows and columns. for (int i = 0; i < 5; i++) { ColumnDefinition coldef = new ColumnDefinition(); coldef.Width = GridLength.Auto; grid.ColumnDefinitions.Add(coldef); RowDefinition rowdef = new RowDefinition(); rowdef.Height = GridLength.Auto; grid.RowDefinitions.Add(rowdef); } // Give the Grid a gradient brush. grid.Background = new LinearGradientBrush(Colors.Gray, Colors.White, new Point(0, 0), new Point(1, 1)); // Every program needs a bit of randomness. Random rand = new Random(); // Fill the Grid with 25 buttons. for (int i = 0; i < 25; i++) { Button btn = new Button(); btn.FontSize = 12 + rand.Next(8); btn.Content = "Button No. " + (i + 1); btn.HorizontalAlignment = HorizontalAlignment.Center; btn.VerticalAlignment = VerticalAlignment.Center; btn.Margin = new Thickness(6); grid.Children.Add(btn); Grid.SetRow(btn, i % 5); Grid.SetColumn(btn, i / 5); } // Size the Grid. grid.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity)); Size sizeGrid = grid.DesiredSize; // Determine point for centering Grid on page. Point ptGrid = new Point((dlg.PrintableAreaWidth - sizeGrid.Width) / 2, (dlg.PrintableAreaHeight - sizeGrid.Height) / 2); // Layout pass. grid.Arrange(new Rect(ptGrid, sizeGrid)); // Now print it. dlg.PrintVisual(grid, Title); } }
private void MakeCollage(IEnumerable<string> imageFilenames, string outputFilename) { var collage = new Grid(); foreach (var filename in imageFilenames) { collage.Children.Add(new Image { Source = LoadBitmapImage(filename), HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, }); } collage.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); collage.Arrange(new Rect(0d, 0d, collage.DesiredSize.Width, collage.DesiredSize.Height)); var bitmap = new RenderTargetBitmap((int)collage.DesiredSize.Width, (int)collage.DesiredSize.Height, 96, 96, PixelFormats.Default); bitmap.Render(collage); bitmap.SaveAsPng(outputFilename); }
public void NegativeMargin () { var grid = new Grid (); var poker = new LayoutPoker { Margin = new Thickness (-1, -1, -1, -1), MeasureOverrideResult = new Size (80, 80), ArrangeOverrideResult = new Size (80, 80) }; grid.Children.Add (poker); grid.Measure (infinity); grid.Arrange (new Rect (0, 0, 50, 50)); Assert.AreEqual (new Size (78, 78), grid.DesiredSize, "#1"); Assert.AreEqual (new Size (78, 78), poker.DesiredSize, "#2"); Assert.AreEqual (new Size (78, 78), new Size (grid.ActualWidth, grid.ActualHeight), "#3"); Assert.AreEqual (new Size (80, 80), new Size (poker.ActualWidth, poker.ActualHeight), "#4"); Assert.AreEqual (new Size (78, 78), grid.RenderSize, "#5"); Assert.AreEqual (new Size (80, 80), poker.RenderSize, "#6"); RectangleGeometry geom = LayoutInformation.GetLayoutClip (grid) as RectangleGeometry; Assert.IsNotNull (geom, "#7"); Assert.AreEqual (new Rect (0, 0, 50, 50), geom.Rect, "#8"); Assert.IsNull (LayoutInformation.GetLayoutClip (poker), "#9"); Assert.AreEqual (new Rect (0, 0, 50, 50), LayoutInformation.GetLayoutSlot (grid), "#10"); Assert.AreEqual (new Rect (0, 0, 78, 78), LayoutInformation.GetLayoutSlot (poker), "#11"); }
private void btnMoveRight_Loaded(object sender, RoutedEventArgs e) { //take screenshot Grid grd = new Grid(); grd.Background = (ImageBrush)this.FindResource("HeaderBrush"); int height = 100; grd.Height = height; grd.Width = tabsGrid.ActualWidth; tabsGrid.Children.Add(grd); grd.Measure(new Size(tabsGrid.ActualWidth, height)); Size size = grd.DesiredSize; grd.Arrange(new Rect(new Point(0, 0), size)); byte[] img = TextureUtil.TakeScreenShot(tabsGrid, (int)tabsGrid.ActualWidth, (int)tabsGrid.ActualHeight); //crop image Point p = btnMoveRight.TranslatePoint(new Point(0, 0), mainGrid); img = TextureUtil.CropImageFile(img, 80, height - 2, (int)p.X, 0); //create brush BitmapImage bmImage = new BitmapImage(); bmImage.BeginInit(); bmImage.StreamSource = new MemoryStream(img); bmImage.CreateOptions = BitmapCreateOptions.None; bmImage.CacheOption = BitmapCacheOption.Default; bmImage.EndInit(); btnMoveRight.Background = new ImageBrush(bmImage); tabsGrid.Children.Remove(grd); }
private string CreateEmptyBackground(Grid grid) { TextBlock block = grid.Children[1] as TextBlock; block.Text = "For more?"; block.FontSize = 23.0; (grid.Children[0] as TextBlock).Text = string.Empty; (grid.Children[2] as TextBlock).Text = "Turn on Live Tile!"; (grid.Children[3] as TextBlock).Text = string.Empty; grid.Arrange(new Rect(0.0, 0.0, 173.0, 173.0)); TileInfoUpdatingAgent.SavePicture(grid, "Shared/ShellContent/tiles", "Shared/ShellContent/tiles/LiveTile.png"); return "isostore:/Shared/ShellContent/tiles/LiveTile.png"; }
public string CreateBackground(Grid grid) { if (grid != null) { TextBlock block = grid.Children[0] as TextBlock; string[] strArray = LocalizedStrings.GetLanguageInfoByKey("FormatterForExpenseInShellTile").Split(new char[] { ',' }); block.Text = System.DateTime.Today.ToString(strArray[0]) + strArray[1]; TextBlock block2 = grid.Children[1] as TextBlock; block2.Text = this.TodaySummary.TotalExpenseAmountInfo; TextBlock block3 = grid.Children[2] as TextBlock; block3.Text = LocalizedStrings.GetCombinedText(AppResources.CurrentWeek, AppResources.Expense, false).ToLowerInvariant(); (grid.Children[3] as TextBlock).Text = this.ThisWeekSummary.TotalExpenseAmountInfo; grid.Arrange(new Rect(0.0, 0.0, 173.0, 173.0)); TileInfoUpdatingAgent.SavePicture(grid, "Shared/ShellContent/tiles", "Shared/ShellContent/tiles/LiveTile.png"); } return "isostore:/Shared/ShellContent/tiles/LiveTile.png"; }
public void ArrangeTest_TwoChildren () { Grid g = new Grid (); RowDefinition rdef; ColumnDefinition cdef; rdef = new RowDefinition (); rdef.Height = new GridLength (50); g.RowDefinitions.Add (rdef); cdef = new ColumnDefinition (); cdef.Width = new GridLength (100); g.ColumnDefinitions.Add (cdef); cdef = new ColumnDefinition (); cdef.Width = new GridLength (20); g.ColumnDefinitions.Add (cdef); g.Margin = new Thickness (5); var ra = new Border (); var rb = new Border (); Grid.SetRow (ra, 0); Grid.SetColumn (ra, 0); Grid.SetRow (rb, 0); Grid.SetColumn (rb, 1); g.Children.Add (ra); g.Children.Add (rb); g.Measure (new Size (Double.PositiveInfinity, Double.PositiveInfinity)); Assert.AreEqual (new Size (0,0), ra.DesiredSize, "ra actual after measure"); Assert.AreEqual (new Size (0,0), rb.DesiredSize, "rb actual after measure"); Assert.AreEqual (new Size (130,60), g.DesiredSize, "g desired 1"); g.Arrange (new Rect (0,0,g.DesiredSize.Width,g.DesiredSize.Height)); Assert.AreEqual (new Size (0,0), ra.DesiredSize, "ra desired 0"); Assert.AreEqual (new Size (130,60), g.DesiredSize, "g desired 1"); Assert.AreEqual (new Rect (0,0,100,50).ToString (), LayoutInformation.GetLayoutSlot (ra).ToString(), "slot"); Assert.AreEqual (new Size (100,50), new Size (ra.ActualWidth, ra.ActualHeight), "ra actual after arrange"); Assert.AreEqual (new Size (20,50), new Size (rb.ActualWidth, rb.ActualHeight), "rb actual after arrange"); Assert.AreEqual (new Size (120,50), new Size (g.ActualWidth, g.ActualHeight), "g actual after arrange"); }
/// <summary> /// This method constructs the document page (visual) to print /// </summary> private DocumentPage ConstructPage(Grid content, int pageNumber) { if (content == null) return null; //Build the page inc header and footer Grid pageGrid = new Grid(); //Header row AddGridRow(pageGrid, GridLength.Auto); //Content row AddGridRow(pageGrid, new GridLength(1.0d, GridUnitType.Star)); //Footer row AddGridRow(pageGrid, GridLength.Auto); ContentControl pageHeader = new ContentControl(); pageHeader.Content = this.CreateDocumentHeader(); pageGrid.Children.Add(pageHeader); if (content != null) { content.SetValue(Grid.RowProperty, 1); pageGrid.Children.Add(content); } ContentControl pageFooter = new ContentControl(); pageFooter.Content = CreateDocumentFooter(pageNumber + 1); pageFooter.SetValue(Grid.RowProperty, 2); pageGrid.Children.Add(pageFooter); double width = this.PageSize.Width - (this.PageMargin.Left + this.PageMargin.Right); double height = this.PageSize.Height - (this.PageMargin.Top + this.PageMargin.Bottom); pageGrid.Measure(new Size(width, height)); pageGrid.Arrange(new Rect(this.PageMargin.Left, this.PageMargin.Top, width, height)); //return new DocumentPage(pageGrid); return new DocumentPage(pageGrid, PageSize, new Rect(content.DesiredSize), new Rect(content.DesiredSize)); }
/// <summary> /// Adds a cube face. /// </summary> /// <param name="normal"> /// The normal. /// </param> /// <param name="up"> /// The up vector. /// </param> /// <param name="b"> /// The brush. /// </param> /// <param name="text"> /// The text. /// </param> private void AddCubeFace(Vector3D normal, Vector3D up, Brush b, string text) { var grid = new Grid { Width = 20, Height = 20, Background = b }; grid.Children.Add( new TextBlock { Text = text, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center, FontSize = 15, Foreground = Brushes.White }); grid.Arrange(new Rect(new Point(0, 0), new Size(20, 20))); var bmp = new RenderTargetBitmap((int)grid.Width, (int)grid.Height, 96, 96, PixelFormats.Default); bmp.Render(grid); var material = MaterialHelper.CreateMaterial(new ImageBrush(bmp)); double a = this.Size; var builder = new MeshBuilder(false, true); builder.AddCubeFace(this.Center, normal, up, a, a, a); var geometry = builder.ToMesh(); geometry.Freeze(); var model = new GeometryModel3D { Geometry = geometry, Material = material }; var element = new ModelUIElement3D { Model = model }; element.MouseLeftButtonDown += this.FaceMouseLeftButtonDown; this.faceNormals.Add(element, normal); this.faceUpVectors.Add(element, up); this.Children.Add(element); }
private void UserControl_Loaded(object sender, RoutedEventArgs e) { var dialog = new PrintDialog(); document.DocumentPaginator.PageSize = new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight); fixedPage = new FixedPage { Height = document.DocumentPaginator.PageSize.Height, Width = document.DocumentPaginator.PageSize.Width }; grid = new Grid(); grid.Arrange(new Rect(0, 0, fixedPage.Width, fixedPage.Height)); fixedPage.Children.Add(grid); pageContent = new PageContent { Child = fixedPage }; pageContent.Arrange(new Rect(0, 0, fixedPage.Width, fixedPage.Height)); fixedPage.Margin = new Thickness(20, 44, 20, 44); for (int i = 0; i < totalRows; i++) { grid.RowDefinitions.Add(new RowDefinition()); } for (int j = 0; j < totalColumns; j++) { grid.ColumnDefinitions.Add(new ColumnDefinition()); } document.Pages.Add(pageContent); }
public static void updateTile(string backgroundPath, string frontText) { Uri backgroundUri = new Uri(backgroundPath, UriKind.Relative); MemoryStream ms = new MemoryStream(); Deployment.Current.Dispatcher.BeginInvoke(() => { Grid grid = new Grid(); StreamResourceInfo info = Application.GetResourceStream(backgroundUri); info.Stream.CopyTo(ms); /*BitmapImage imageSource = new BitmapImage(backgroundUri); grid.Background = new ImageBrush { ImageSource = imageSource, Opacity = 1, };*/ WriteableBitmap imgSrc = new WriteableBitmap(1, 1); imgSrc.SetSource(info.Stream); Image img = new Image(); img.Source = imgSrc; grid.Children.Add(img); /*TextBlock text = new TextBlock() { FontSize = 20, Foreground = new SolidColorBrush(Colors.White) }; text.Text = frontText; text.HorizontalAlignment = System.Windows.HorizontalAlignment.Right; text.VerticalAlignment = System.Windows.VerticalAlignment.Bottom; Thickness th = new Thickness(); th.Top = 143; th.Left = 133; text.Margin = th; grid.Children.Add(text); */ grid.Arrange(new Rect(0, 0, 173, 173)); // force render WriteableBitmap wbmp = new WriteableBitmap(173, 173); Canvas can = new Canvas(); can.Background = (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"]; can.Width = 173; can.Height = 173; wbmp.Render(can, null); wbmp.Render(grid, null); wbmp.Invalidate(); //wbmp.SaveJpeg(ms, 173, 173, 0, 100); }); using (var stream = isolatedStorage.CreateFile("/Shared/ShellContent/tile.png")) { ms.CopyTo(stream); } TranslationHelper.Pair<string, int> title_info = TranslationHelper.getTitle(); string tile_title = title_info.First; int tile_width = title_info.Second; string title = tile_title + spaces(tile_width - tile_title.Length - frontText.Length) + frontText; Uri iconUri = new Uri("isostore:/Shared/ShellContent/tile.png", UriKind.Absolute); ShellTile TileToFind = ShellTile.ActiveTiles.First(); if (TileToFind != null) { StandardTileData NewTileData = new StandardTileData { Title = title, Count = 0, BackgroundImage = backgroundUri, }; TileToFind.Update(NewTileData); } }
protected static string WriteTileToDisk(string year, string description, int width, int height, string fontSize, Thickness margins) { Grid container = new Grid() { Width = width, Height = height, Background = (Brush)Application.Current.Resources["TransparentBrush"] }; container.Children.Add(GetTextBlockToRender(description, fontSize, margins)); // Force the container to render itself container.UpdateLayout(); container.Arrange(new Rect(0, 0, width, height)); var writeableBitmap = new WriteableBitmap(container, null); string fileName = SharedImagePath + "tile" + height + width + ".png"; using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication()) { using (var stream = new IsolatedStorageFileStream(fileName, FileMode.Create, storage)) { if (writeableBitmap.PixelHeight > 0) { writeableBitmap.WritePNG(stream); } } } return fileName; }
public void StarRowsWithChild2_NoSpan_ExplicitSize () { // Check what happens when there are two explicit rows and no explicit column Grid grid = new Grid { Width = 75, Height = 75, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Bottom, }; grid.AddRows (new GridLength (1, GridUnitType.Star), new GridLength (1, GridUnitType.Star)); grid.Children.Add (new MyContentControl (50, 50)); // Initial values Assert.AreEqual (new Size (0, 0), grid.DesiredSize, "#1"); grid.CheckRowHeights ("#2", 0, 0); // After measure grid.Measure (Infinity); grid.Arrange (new Rect (0, 0, grid.DesiredSize.Width, grid.DesiredSize.Height)); Assert.AreEqual (new Size (75, 75), grid.DesiredSize, "#3"); grid.CheckRowHeights ("#4", 37.5, 37.5); // Measure again grid.Measure (new Size (100, 100)); grid.Arrange (new Rect (0, 0, grid.DesiredSize.Width, grid.DesiredSize.Height)); Assert.AreEqual (new Size (75, 75), grid.DesiredSize, "#5"); grid.CheckRowHeights ("#6", 37.5, 37.5); }
public void ExpandInArrange_OutsideTree_GridParent_UnfixedSize () { // We always expand star rows if we're not in the live tree // with a parent var parent = new Grid (); // Measure with infinity and check results. MyGrid grid = new MyGrid (); grid.AddRows (Star); grid.AddColumns (Star); grid.AddChild (ContentControlWithChild (), 0, 0, 1, 1); parent.Children.Add (grid); parent.Measure (Infinity); grid.CheckMeasureArgs ("#1", Infinity); grid.CheckMeasureResult ("#2", new Size (50, 50)); Assert.AreEqual (new Size (50, 50), grid.DesiredSize, "#3"); // When we pass in the desired size as the arrange arg, // the rows/cols use that as their height/width parent.Arrange (new Rect (0, 0, grid.DesiredSize.Width, grid.DesiredSize.Height)); grid.CheckArrangeArgs ("#4", grid.DesiredSize); grid.CheckArrangeResult ("#5", grid.DesiredSize); grid.CheckRowHeights ("#6", grid.DesiredSize.Height); grid.CheckColWidths ("#7", grid.DesiredSize.Width); // If we pass in twice the desired size, the rows/cols consume that too grid.Reset (); parent.Arrange (new Rect (0, 0, 100, 100)); grid.CheckMeasureArgs ("#8"); // No remeasures grid.CheckArrangeArgs ("#9", new Size (100, 100)); grid.CheckArrangeResult ("#10", new Size (100, 100)); grid.CheckRowHeights ("#11", 100); grid.CheckColWidths ("#12", 100); // If we measure with a finite size, the rows/cols still expand // to consume the available space grid.Reset (); parent.Measure (new Size (1000, 1000)); grid.CheckMeasureArgs ("#13", new Size (1000, 1000)); grid.CheckMeasureResult ("#14", new Size (50, 50)); Assert.AreEqual (new Size (50, 50), grid.DesiredSize, "#15"); // When we pass in the desired size as the arrange arg, // the rows/cols use that as their height/width parent.Arrange (new Rect (0, 0, grid.DesiredSize.Width, grid.DesiredSize.Height)); grid.CheckArrangeArgs ("#16", grid.DesiredSize); grid.CheckArrangeResult ("#17", grid.DesiredSize); grid.CheckRowHeights ("#18", grid.DesiredSize.Height); grid.CheckColWidths ("#19", grid.DesiredSize.Width); // If we pass in twice the desired size, the rows/cols consume that too grid.Reset (); parent.Arrange (new Rect (0, 0, 100, 100)); grid.CheckMeasureArgs ("#20"); // No remeasures grid.CheckArrangeArgs ("#21", new Size (100, 100)); grid.CheckArrangeResult ("#22", new Size (100, 100)); grid.CheckRowHeights ("#23", 100); grid.CheckColWidths ("#24", 100); }
public void StarRowsWithChild_NoSpan_ExplicitSize () { // Check what happens if there is no explicit ColumnDefinition added Grid grid = new Grid { Width = 75, Height = 75, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Bottom, }; grid.AddRows (new GridLength (1, GridUnitType.Star)); grid.Children.Add (new MyContentControl (50, 50)); // Initial values Assert.AreEqual (new Size (0, 0), grid.DesiredSize, "#1"); Assert.AreEqual (0, grid.RowDefinitions [0].ActualHeight, "#2"); // After measure grid.Measure (Infinity); grid.Arrange (new Rect (0, 0, grid.DesiredSize.Width, grid.DesiredSize.Height)); Assert.AreEqual (new Size (75, 75), grid.DesiredSize, "#4"); Assert.AreEqual (75, grid.RowDefinitions [0].ActualHeight, "#5"); // Measure again grid.Measure (new Size (100, 100)); grid.Arrange (new Rect (0, 0, grid.DesiredSize.Width, grid.DesiredSize.Height)); Assert.AreEqual (new Size (75, 75), grid.DesiredSize, "#7"); Assert.AreEqual (75, grid.RowDefinitions [0].ActualHeight, "#8"); }
public void StarRowsWithChild2 () { // Check what happens when there are two explicit rows and no explicit column Grid grid = new Grid (); grid.AddRows (new GridLength (1, GridUnitType.Star), new GridLength (1, GridUnitType.Star)); grid.Children.Add (new MyContentControl (50, 50)); // Initial values Assert.AreEqual (new Size (0, 0), grid.DesiredSize, "#1"); grid.CheckRowHeights ("#2", 0, 0); // After measure grid.Measure (Infinity); grid.Arrange (new Rect (0, 0, grid.DesiredSize.Width, grid.DesiredSize.Height)); Assert.AreEqual (new Size (50, 50), grid.DesiredSize, "#3"); grid.CheckRowHeights ("#4", 50, 0); // Measure again grid.Measure (new Size (100, 100)); grid.Arrange (new Rect (0, 0, grid.DesiredSize.Width, grid.DesiredSize.Height)); Assert.AreEqual (new Size (50, 50), grid.DesiredSize, "#5"); grid.CheckRowHeights ("#6", 50, 0); }
public void ExpandStars_NoRowsOrCols2 () { // We don't expand autogenerated rows/cols if we don't have Alignment.Stretch Grid grid = new Grid { VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center }; grid.Children.Add (new Rectangle { Width = 50, Height = 50 }); grid.Measure (new Size (200, 200)); grid.Arrange (new Rect (0, 0, 200, 200)); Assert.AreEqual (50, grid.ActualWidth, "#1"); Assert.AreEqual (50, grid.ActualHeight, "#2"); }
public void UnfixedGridAllStar () { // Check the widths/heights of the rows/cols without specifying a size for the grid // Measuring the rows initialises the sizes to Infinity for 'star' elements Grid grid = new Grid (); grid.AddRows (new GridLength (1, GridUnitType.Star)); grid.AddColumns (new GridLength (1, GridUnitType.Star)); // Initial values Assert.AreEqual (new Size (0, 0), grid.DesiredSize, "#1"); Assert.AreEqual (0, grid.RowDefinitions [0].ActualHeight, "#2"); Assert.AreEqual (0, grid.ColumnDefinitions [0].ActualWidth, "#3"); // After measure grid.Measure (Infinity); grid.Arrange (new Rect (0, 0, grid.DesiredSize.Width, grid.DesiredSize.Height)); Assert.AreEqual (new Size (0, 0), grid.DesiredSize, "#4"); Assert.AreEqual (0, grid.RowDefinitions [0].ActualHeight, "#5"); Assert.AreEqual (0, grid.ColumnDefinitions [0].ActualWidth, "#6"); // Measure again grid.Measure (new Size (100, 100)); grid.Arrange (new Rect (0, 0, grid.DesiredSize.Width, grid.DesiredSize.Height)); Assert.AreEqual (new Size (0, 0), grid.DesiredSize, "#7"); Assert.AreEqual (0, grid.RowDefinitions [0].ActualHeight, "#8"); Assert.AreEqual (0, grid.ColumnDefinitions [0].ActualWidth, "#9"); }
public void ExpandStars_NoRowsOrCols () { // If the rows/cols are autogenerated, we still expand them Grid grid = new Grid (); grid.Children.Add (new Rectangle { Width = 50, Height = 50 }); grid.Measure (new Size (200, 200)); grid.Arrange (new Rect (0, 0, 200, 200)); Assert.AreEqual (200, grid.ActualWidth, "#1"); Assert.AreEqual (200, grid.ActualHeight, "#2"); }
private void print_Click(object sender, RoutedEventArgs e) { PrintDialog printDialog = new PrintDialog(); Grid printGrid = new Grid(); ColumnDefinition col1 = new ColumnDefinition() { Width = new GridLength(170) }; ColumnDefinition col2 = new ColumnDefinition() { Width = new GridLength(190) }; ColumnDefinition col3 = new ColumnDefinition(); ColumnDefinition col4 = new ColumnDefinition(); ColumnDefinition col5 = new ColumnDefinition(); ColumnDefinition col6 = new ColumnDefinition(); printGrid.ColumnDefinitions.Add(col1); printGrid.ColumnDefinitions.Add(col2); printGrid.ColumnDefinitions.Add(col3); printGrid.ColumnDefinitions.Add(col4); printGrid.ColumnDefinitions.Add(col5); // printGrid.ColumnDefinitions.Add(col6); booking = booking.OrderBy(a => a.vonBuchung).ToList(); int countPages = 1; booking.OrderByDescending(a => a.vonBuchung); Label labelPrint = new Label(); #region Kopfzeile labelPrint = new Label() { Name = "print_Date", Content = "Buchungsliste (" + dpVon.Text + " - " + dpBis.Text + ") Stand vom: " + DateTime.Now, Margin = new Thickness( left: 20, top: 10, right: 0, bottom: 0), FontSize = 13, FontStyle = FontStyles.Oblique }; Grid.SetColumn(labelPrint, 0); labelPrint.SetValue(Grid.ColumnSpanProperty, 4); printGrid.Children.Add(labelPrint); //0 labelPrint = new Label() { Name = "print_head", Content = "Name", Margin = new Thickness( left: 15, top: 26, right: 0, bottom: 0), FontSize = 16, FontWeight = FontWeights.Bold }; Grid.SetColumn(labelPrint, 0); printGrid.Children.Add(labelPrint); //1 labelPrint = new Label() { Name = "print_head", Content = " von - bis", Margin = new Thickness( left: 0, top: 26, right: 0, bottom: 0), FontSize = 16, FontWeight = FontWeights.Bold }; Grid.SetColumn(labelPrint, 1); printGrid.Children.Add(labelPrint); //2 //3 labelPrint = new Label() { Name = "print_head", Content = " Zimmer", Margin = new Thickness( left: 0, top: 26, right: 0, bottom: 0), FontSize = 16, FontWeight = FontWeights.Bold }; Grid.SetColumn(labelPrint, 2); printGrid.Children.Add(labelPrint); //4 labelPrint = new Label() { Name = "print_head", Content = "Erw/Kind", Margin = new Thickness( left: 0, top: 26, right: 0, bottom: 0), FontSize = 16, FontWeight = FontWeights.Bold }; Grid.SetColumn(labelPrint, 3); printGrid.Children.Add(labelPrint); #endregion int infoCount = 0; string infostring = ""; int lengthToWrite = 23; int top = 46; for (int i = 0; i < booking.Count(); i++) { //0 labelPrint = new Label() { Name = "print_" + i, Content = booking.ElementAt(i).Kunde.vornameKunde + " " + booking.ElementAt(i).Kunde.nachnameKunde, Margin = new Thickness( left: 15, top: top, right: 0, bottom: 0), FontSize = 14 }; Grid.SetColumn(labelPrint, 0); printGrid.Children.Add(labelPrint); //1 labelPrint = new Label() { Name = "print_" + i, Content = booking.ElementAt(i).vonBuchung.ToShortDateString() + " - " + booking.ElementAt(i).bisBuchung.ToShortDateString(), Margin = new Thickness( left: 0, top: top, right: 0, bottom: 0), FontSize = 14 }; Grid.SetColumn(labelPrint, 1); printGrid.Children.Add(labelPrint); //2 //3 labelPrint = new Label() { Name = "print_" + i, Content = hm.repZimmerTyp.GetById(booking.ElementAt(i).idZimmerTyp).typBezeichnung, Margin = new Thickness( left: 20, top: top, right: 0, bottom: 0), FontSize = 14 }; if (booking.ElementAt(i).idZimmer != null) labelPrint.Content = booking.ElementAt(i).idZimmer + ", " + hm.repZimmerTyp.GetById(booking.ElementAt(i).idZimmerTyp).typBezeichnung; Grid.SetColumn(labelPrint, 2); printGrid.Children.Add(labelPrint); //4 labelPrint = new Label() { Name = "print_" + i, Content = booking.ElementAt(i).erwachseneBuchung + "/" + booking.ElementAt(i).kinderBuchung, Margin = new Thickness( left: 15, top: top, right: 0, bottom: 0), FontSize = 14 }; Grid.SetColumn(labelPrint, 3); printGrid.Children.Add(labelPrint); string stringVerpf = booking.ElementAt(i).board; string verpf = ""; switch (stringVerpf) { case "bb": verpf = "Frühstück; "; break; case "hb": verpf = "Halbpension; "; break; case "fb": verpf = "Vollpension; "; break; case "nb": verpf = "Keine Verpflegung; "; break; } if (booking.ElementAt(i).infoBuchung != null) infostring = "(" + verpf + booking.ElementAt(i).infoBuchung + ")"; else { infostring = "(" + verpf + ")"; } infoCount = infostring.Length; //5 do { if (infoCount > 0) { lengthToWrite = (infoCount > 23) ? 23 : infostring.Length; labelPrint = new Label() { Name = "print_" + i, Content = infostring.Substring(0, lengthToWrite), Margin = new Thickness( left: 0, top: top, right: 0, bottom: 0), FontSize = 14 }; Grid.SetColumn(labelPrint, 4); printGrid.Children.Add(labelPrint); } infostring = infostring.Remove(0, lengthToWrite); top += 16; infoCount -= 23; } while ((infoCount + 23) > 23); // top += 16; } PrintCapabilities capabilities = printDialog.PrintQueue.GetPrintCapabilities(printDialog.PrintTicket); Size pageSize = new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight); Size visibleSize = new Size(capabilities.PageImageableArea.ExtentWidth, (capabilities.PageImageableArea.ExtentHeight - 45)); FixedDocument fixedDoc = new FixedDocument(); //If the toPrint visual is not displayed on screen we neeed to measure and arrange it printGrid.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); printGrid.Arrange(new Rect(new Point(0, 0), printGrid.DesiredSize)); // Size size = printGrid.DesiredSize; //Will assume for simplicity the control fits horizontally on the page double yOffset = 0; while (yOffset < size.Height) { VisualBrush vb = new VisualBrush(printGrid); vb.Stretch = Stretch.None; vb.AlignmentX = AlignmentX.Left; vb.AlignmentY = AlignmentY.Top; vb.ViewboxUnits = BrushMappingMode.Absolute; vb.TileMode = TileMode.None; vb.Viewbox = new Rect(0, yOffset, visibleSize.Width, visibleSize.Height); PageContent pageContent = new PageContent(); FixedPage page = new FixedPage(); ((IAddChild)pageContent).AddChild(page); fixedDoc.Pages.Add(pageContent); page.Width = pageSize.Width; page.Height = pageSize.Height; Canvas canvas = new Canvas(); if (countPages <= 1) { FixedPage.SetLeft(canvas, capabilities.PageImageableArea.OriginWidth); FixedPage.SetTop(canvas, capabilities.PageImageableArea.OriginHeight); } else { FixedPage.SetLeft(canvas, capabilities.PageImageableArea.OriginWidth); FixedPage.SetTop(canvas, capabilities.PageImageableArea.OriginHeight + 46); } // FixedPage.SetBottom(canvas, capabilities.PageImageableArea.ExtentHeight-50); canvas.Width = visibleSize.Width; canvas.Height = visibleSize.Height; canvas.Background = vb; page.Children.Add(canvas); //Für die Seiten Angabe am Ende der Seite int pagesNum = Convert.ToInt16(Math.Ceiling((size.Height / visibleSize.Height))); page.Children.Add(new Label() { Name = "page_", Content = "Seite " + countPages + "/" + pagesNum, Margin = new Thickness( left: canvas.Width - 100, top: canvas.Height, right: 0, bottom: 0), FontSize = 14 }); yOffset += visibleSize.Height; countPages++; } //DokumentenVorschau falls benötigt Window wnd = new Window(); DocumentViewer viewer = new DocumentViewer(); viewer.Document = fixedDoc; wnd.Content = viewer; wnd.ShowDialog(); //if (printDialog.ShowDialog() == true) //{ // //printDialog.PrintVisual(page, "just a test"); // // printDialog.PrintDocument(document.DocumentPaginator, "test"); // printDialog.PrintDocument(fixedDoc.DocumentPaginator, "Buchungsliste Ausdrucken"); //} }
public void DefaultDefinitions () { Grid grid = new Grid (); grid.Children.Add (new Border ()); Assert.IsTrue (grid.ColumnDefinitions != null); Assert.IsTrue (grid.RowDefinitions != null); Assert.AreEqual (0, grid.ColumnDefinitions.Count); Assert.AreEqual (0, grid.RowDefinitions.Count); grid.Measure (new Size (Double.PositiveInfinity, Double.PositiveInfinity)); Assert.IsTrue (grid.ColumnDefinitions != null); Assert.IsTrue (grid.RowDefinitions != null); Assert.AreEqual (0, grid.ColumnDefinitions.Count); Assert.AreEqual (0, grid.RowDefinitions.Count); grid.Arrange (new Rect (0,0, grid.DesiredSize.Width, grid.DesiredSize.Height)); Assert.IsTrue (grid.ColumnDefinitions != null); Assert.IsTrue (grid.RowDefinitions != null); Assert.AreEqual (0, grid.ColumnDefinitions.Count); Assert.AreEqual (0, grid.RowDefinitions.Count); }
public static void PrintVisual(Grid g) { printDialog = new PrintDialog(); if (printDialog.ShowDialog() == false) return; FrameworkElement e = g as FrameworkElement; Transform originalScale = e.LayoutTransform; //get selected printer capabilities PrintCapabilities capabilities = printDialog.PrintQueue.GetPrintCapabilities(printDialog.PrintTicket); //get scale of the print wrt to screen of WPF visual double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / g.ActualWidth, capabilities.PageImageableArea.ExtentHeight / g.ActualHeight); //Transform the Visual to scale g.LayoutTransform = new ScaleTransform(scale, scale); //get the size of the printer page Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight); //update the layout of the visual to the printer page size. g.Measure(sz); g.Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz)); //now print the visual to printer to fit on the one page. printDialog.PrintVisual(g, "Onion Smart Solutions"); e.LayoutTransform = originalScale; }
void PrintOnClick(object sender, RoutedEventArgs args) { PrintDialog dlg = new PrintDialog(); if (dlg.ShowDialog().GetValueOrDefault()) { Grid grid = new Grid(); for (int i = 0; i < 5; i++) { ColumnDefinition coldef = new ColumnDefinition(); coldef.Width = GridLength.Auto; grid.ColumnDefinitions.Add(coldef); RowDefinition rowdef = new RowDefinition(); rowdef.Height = GridLength.Auto; grid.RowDefinitions.Add(rowdef); } grid.Background = new LinearGradientBrush(Colors.Gray, Colors.White, new Point(0,0), new Point(1,1)); Random rand = new Random(); for (int i = 0; i < 25; i++) { Button btn = new Button(); btn.FontSize = 12 + rand.Next(8); btn.Content = "Button No. " + (i + 1); btn.HorizontalAlignment = HorizontalAlignment.Center; btn.VerticalAlignment = VerticalAlignment.Center; btn.Margin = new Thickness(6); grid.Children.Add(btn); Grid.SetRow(btn, i%5); Grid.SetColumn(btn, i/5); } grid.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity)); Size sizeGrid = grid.DesiredSize; Point ptGrid = new Point((dlg.PrintableAreaWidth - sizeGrid.Width)/2, (dlg.PrintableAreaHeight - sizeGrid.Height)/2); grid.Arrange(new Rect(ptGrid, sizeGrid)); dlg.PrintVisual(grid, Title); } }