■ FrameworkElement 클래스 : 이미지 저장하기
------------------------------------------------------------------------------------------------------------------------
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
#region 이미지 저장하기 - SaveImage(element, filePath)
/// <summary>
/// 이미지 저장하기
/// </summary>
/// <param name="element">프레임워크 엘리먼트</param>
/// <param name="filePath">파일 경로</param>
public void SaveImage(FrameworkElement element, string filePath)
{
Rect elementRectangle = VisualTreeHelper.GetDescendantBounds(element);
DrawingVisual visual = new DrawingVisual();
using(DrawingContext context = visual.RenderOpen())
{
VisualBrush brush = new VisualBrush(element);
context.DrawRectangle(brush, null, new Rect(elementRectangle.Size));
}
int width = (int)element.ActualWidth;
int height = (int)element.ActualHeight;
RenderTargetBitmap bitmap = new RenderTargetBitmap
(
width,
height,
96,
96,
PixelFormats.Pbgra32
);
bitmap.Render(visual);
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmap));
using(FileStream stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
encoder.Save(stream);
}
}
#endregion
------------------------------------------------------------------------------------------------------------------------
'C# > WPF' 카테고리의 다른 글
[C#/WPF] 팔면체(Octahedron) 사용하기 (0) | 2018.12.31 |
---|---|
[C#/WPF] 정육면체(Cube) 사용하기 (0) | 2018.12.31 |
[C#/WPF] 사면체(Tetrahedron) 사용하기 (0) | 2018.12.31 |
[C#/WPF] 설치 폰트 샘플 표시하기 (0) | 2018.12.30 |
[C#/WPF] 색상 리스트 표시하기 (0) | 2018.12.30 |
[C#/WPF] FrameworkElement 클래스 : 이미지 저장하기 (0) | 2018.12.30 |
[C#/WPF] 외곽선 경로 구하기 (0) | 2018.12.30 |
[C#/WPF] 대칭 복합 선 그리기 (0) | 2018.12.30 |
[C#/WPF] WriteableBitmap 클래스 : 비트맵 픽셀 설정하기 (0) | 2018.12.29 |
[C#/WPF] RenderTargetBitmap 클래스 : 텍스트 그리기 (0) | 2018.12.27 |
[C#/WPF] 회전 텍스트 그리기 (0) | 2018.12.27 |
댓글을 달아 주세요