Creating a VisualBrush without ever displaying the Visual it represents?

In this case control is never rendered on the screen. I just added the canvas to a grid and on a button click I called the save command. And it worked.

Up vote 0 down vote favorite 1 share g+ share fb share tw.

Using the code below, I am attempting to fill a Canvas with UIElements and save it as a tif Image. However, my Image is always blank. It is because the Canvas is never displayed on the screen and some sort of initialization and drawing never took place?

How can I make this work? The Canvas creation would go something like this: Canvas theCanvas = new Canvas(); theCanvas. Width = 2740; theCanvas.

Height = 2280; ... Button button = new Button(); button. Content = "Push Me. "; button.

Height = 50; button. Width = 200; Canvas. SetTop(button, 200); Canvas.

SetLeft(button, 300); theCanvas.Children. Add(button); To create the Image and save it: using (System.IO. FileStream fs = new System.IO.

FileStream(path, System.IO.FileMode. Create)) { RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)inWidth, (int)inHeight, 1 / 300, 1 / 300, PixelFormats. Pbgra32); DrawingVisual visual = new DrawingVisual(); using (DrawingContext context = visual.RenderOpen()) { VisualBrush brush = new VisualBrush(inCanvas); context.

DrawRectangle(brush, null, new Rect(new Point(), new Size(inWidth, inHeight))); } renderBitmap. Render(visual); BitmapEncoder encoder = new TiffBitmapEncoder(); encoder.Frames. Add(BitmapFrame.

Create(renderBitmap)); encoder. Save(fs); fs.Close(); } c# wpf image canvas visualbrush link|improve this question edited Sep 18 '11 at 17:58Dave Clemmer1,77331030 asked Feb 4 '10 at 20:07Dan Vogel5261726 72% accept rate.

1 I had the same problem. Stackoverflow.com/questions/1877115/… – Will? Feb 4 '10 at 20:16 Well, may be a diff problem (I was doing something wrong) but the code is all there... – Will?

Feb 4 '10 at 20:19 yeah this helped. I was missing the Measure and Arrange. – Dan Vogel Feb 5 '10 at 18:45.

In this case control is never rendered on the screen. I just added the canvas to a grid and on a button click I called the save command. And it worked.

Private void Window_Loaded(object sender, RoutedEventArgs e) { theCanvas. Width = 2740; theCanvas. Height = 2280; Button button = new Button(); button.

Content = "Push Me. "; button. Height = 50; button.

Width = 200; Canvas. SetTop(button, 200); Canvas. SetLeft(button, 300); theCanvas.Children.

Add(button); mainGri.Children. Add(theCanvas); } private void mainGri_MouseDown(object sender, MouseButtonEventArgs e) { } private void button1_Click(object sender, RoutedEventArgs e) { String path = @"c:\\a. Jpg"; using (System.IO.

FileStream fs = new System.IO. FileStream(path, System.IO.FileMode. Create)) { int inWidth = 300; int inHeight = 400; RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)inWidth, (int)inHeight, 1 / 300, 1 / 300, PixelFormats.

Pbgra32); DrawingVisual visual = new DrawingVisual(); using (DrawingContext context = visual.RenderOpen()) { VisualBrush brush = new VisualBrush(theCanvas); context. DrawRectangle(brush, null, new Rect(new Point(), new Size(inWidth, inHeight))); } renderBitmap. Render(visual); BitmapEncoder encoder = new TiffBitmapEncoder(); encoder.Frames.

Add(BitmapFrame. Create(renderBitmap)); encoder. Save(fs); fs.Close(); } } } }.

My goal is to not have to display the canvas. – Dan Vogel Feb 5 '10 at 18:50.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions