0

I have a Canvas element that is located on a Grid. As long as the Canvas is located in the upper left corner of the grid, my code works fine, but if I move the Canvas anywhere else, it stores a blank image.

        private void Save_Button_Click(object sender, RoutedEventArgs e) {
            Canvas surface = Image_Canvas;
            Transform transform = surface.LayoutTransform;
            // reset current transform (in case it is scaled or rotated)
            surface.LayoutTransform = null;

            // Get the size of canvas
            Size size = new Size(surface.Width, surface.Height);
            surface.Measure(size);
            surface.Arrange(new Rect(size));

            // Create a render bitmap and push the surface to it
            RenderTargetBitmap renderBitmap =
              new RenderTargetBitmap(
                (int)size.Width,
                (int)size.Height,
                96d,
                96d,
                PixelFormats.Pbgra32);
            renderBitmap.Render(surface);

            using (FileStream outStream = new FileStream("C:\\Users\\ttttt\\NewImage.png", FileMode.Create)) {
                PngBitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
                encoder.Save(outStream);
            }
            Image_Canvas.LayoutTransform = transform;
        }

I must be missing something to identify the Canvas location on the grid?

RNovey
  • 58
  • 5

0 Answers0