본문 바로가기
카테고리 없음

WPF Control 이미지 저장하기

by 생속지 2021. 5. 20.
반응형

WPF의 Control을 이미지를 저장하는 방법입니다.

 

1. 아래와 같이 Grid에 Image, TextBox로 구성했습니다.

   Grid내의 Control을 이미지로 저장 할 겁니다.

 

XAML

<Grid x:Name="grStamp" HorizontalAlignment="Left" Height="350" Margin="0,0,0,0" VerticalAlignment="Top" Width="350">
	<Image x:Name="imgStamp" HorizontalAlignment="Left" Height="350" VerticalAlignment="Top" Width="350" Source="stamp.png" Stretch="Fill"/>
	<TextBox x:Name="tbDate" HorizontalAlignment="Left" Height="55" Margin="0,150,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="350" FontSize="36" TextAlignment="Center" Background="{x:Null}" BorderBrush="{x:Null}"/>
	<TextBox x:Name="tbName" HorizontalAlignment="Left" Height="38" Margin="0,239,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="350" FontSize="24" TextAlignment="Center" Background="{x:Null}" BorderBrush="{x:Null}"/>
</Grid>

 

2. Grid Control인 grStamp를 이미지로 저장하는 소스입니다.

RenderTargetBitmap rtb = new RenderTargetBitmap((int)grStamp.ActualWidth, (int)grStamp.ActualHeight, 96, 96, PixelFormats.Pbgra32);
rtb.Render(grStamp);

PngBitmapEncoder png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(rtb));
MemoryStream stream = new MemoryStream();
png.Save(stream);
System.Drawing.Image image = System.Drawing.Image.FromStream(stream);

string stampFileName = DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".png";
string stampPath = GetType().Assembly.Location.Replace(GetType().Assembly.ManifestModule.Name, "") + stampFileName;

image.Save(stampPath);

 

반응형

댓글