C#截图

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32961291/article/details/80507745
 private void TakeScreenShot(String fileName) private void TakeScreenShot(String fileName)
        {
            Rect bounds = VisualTreeHelper.GetDescendantBounds(this);
            double dpi = 96d;
            RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)bounds.Width, (int)bounds.Height,
                                                                       dpi, dpi, PixelFormats.Default);


            DrawingVisual dv = new DrawingVisual();
            using (DrawingContext dc = dv.RenderOpen())
            {
                VisualBrush vb = new VisualBrush(this);
                dc.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size));
            }


            renderBitmap.Render(dv);


            PngBitmapEncoder encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(renderBitmap));


            using (FileStream file = File.Create(fileName))
            {
                encoder.Save(file);
            }
        }        }

猜你喜欢

转载自blog.csdn.net/qq_32961291/article/details/80507745