728x90
using System.IO; using System.Windows; using System.Windows.Media; using System.Windows.Media.Imaging; namespace WpfApp4 { public class Function { /// <summary> /// Grid를 Bitmap이미지로 변환 /// </summary> /// <param name="grid">이미지 변환 할 Grid</param> /// <param name="filePath">저장될 png 파일 경로</param> /// <returns></returns> public static void SaveControlToImage(FrameworkElement grid, string filePath) { Size size = new Size(grid.ActualWidth, grid.ActualHeight); if (!size.IsEmpty) { RenderTargetBitmap result = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Pbgra32); DrawingVisual drawingvisual = new DrawingVisual(); using (DrawingContext context = drawingvisual.RenderOpen()) { context.DrawRectangle(new VisualBrush(grid), null, new Rect(new Point(), size)); context.Close(); } result.Render(drawingvisual); FileStream stream = new FileStream(filePath, FileMode.Create); PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(result)); encoder.Save(stream); stream.Close(); } } } } |
728x90
'C# > WPF' 카테고리의 다른 글
[WPF] DevExpress 컨트롤 이용한 Waiting/Progress Dialog 띄우기 (0) | 2022.03.21 |
---|---|
[WPF] Command snippet (자작) (0) | 2021.12.16 |
[WPF] ImageSource 를 jpg 로 저장하기 (0) | 2021.12.03 |
[WPF] 소숫점 둘째 짜리까지 표시하기 (0) | 2021.12.01 |
[WPF] Number Comma (금액 콤마) 나타내기 (0) | 2021.12.01 |