회사에서 일을 하던중에 이미지 파일의 인쇄가 프로그램을 만들게 되었습니다.
웹을 뒤져보니까 윈폼용을 있지만 WPF용으로는 없는 것 같아서 이렇게 블로깅을 하게 되었네요!
매우 간추리자면 PridDialog를 사용하면 됩니다! 아시는 문들은 MSDN에 가서 보시면 되시겠구요. 저는 버튼을 누르면 이미지 출력!
인쇄를 누르면 인쇄 하는 것 까지 만들어 보겠습니다.
파일 -> 새로 만들기 -> 프로젝트
먼저 Xaml은
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <Window x:Class="WPFImagePrintTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WPFImagePrintTest" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid> <Image Name="image" Height="250" Width="425" Margin="82,10,10,59"></Image> <Button x:Name="button1" Content="이미지!" HorizontalAlignment="Left" Margin="10,279,0,10" Width="75" Click="button1_Click"/> <Button x:Name="button" Content="인쇄" HorizontalAlignment="Left" Margin="110,279,0,10" Width="75" Click="button_Click"/> </Grid> </Window> | cs |
버튼 2개 이미지뷰 한개를 만들어서 올려줍니다. 어차피 간단한 테스트 프로그램이니 대충~ 원하는 대로!
먼저 이미지를 불러오는 함수를 만들어 줍시다.
1 2 3 4 5 6 7 8 9 | private ImageSource imageLoad() // 이미지를 불러오는 부분 { var bi = new BitmapImage(); bi.BeginInit(); bi.CacheOption = BitmapCacheOption.OnLoad; bi.UriSource = new Uri(@"..\Assets\images.png", UriKind.Relative); bi.EndInit(); return bi; } | cs |
비트뱁 이미지를 생성후에 Uri를 통해서 이미지를 가져옵니다.
먼저 이미지 소스에 이미지를 넣는 버튼 이벤트를 해보겠습니다.
1 2 3 4 | private void button1_Click(object sender, RoutedEventArgs e) // 이미지 보여주는 함수 { image.Source = imageLoad(); } | cs |
네 참 쉽죠? 그냥 이미지의 속성중에 소스라는 곳에 비트맵을 넣어주면 됩니다.
실행해보면?
네 잘 나옵니다!
자 이제 정말 중요한 인쇄 부분을 보겠습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | private void button_Click(object sender, RoutedEventArgs e) // 이미지를 인쇄하는 부분 { var bi = imageLoad(); var vis = new DrawingVisual(); var dc = vis.RenderOpen(); dc.DrawImage(bi, new Rect { Width = bi.Width, Height = bi.Height });// 이것을 움직이면 크기를 바꿀 수 있습니다. dc.Close(); var pdialog = new PrintDialog(); if (pdialog.ShowDialog() == true) { pdialog.PrintVisual(vis, "Smile Image"); } } | cs |
비트맥에 이미지 로딩하는 부분을 넣은후
그 비트맵을 Visual이라는 형식으로 바꾸는 작업이 필요합니다. 그렇게 바뀌어진 Visual을 PrintDialog.PrintVisual에 넣으면 완성!
짠~ 전 pdf와 저의 인쇄기로 테스트해봤는데 잘 됩니다!
풀 소스와 프로젝트파일 까지 올려 놓을테니 많이 많이 도움이 되시길 바랍니다!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void button_Click(object sender, RoutedEventArgs e) // 이미지를 인쇄하는 부분 { var bi = imageLoad(); var vis = new DrawingVisual(); var dc = vis.RenderOpen(); dc.DrawImage(bi, new Rect { Width = bi.Width, Height = bi.Height });// 이것을 움직이면 크기를 바꿀 수 있습니다. dc.Close(); var pdialog = new PrintDialog(); if (pdialog.ShowDialog() == true) { pdialog.PrintVisual(vis, "Smile Image"); } } private void button1_Click(object sender, RoutedEventArgs e) // 이미지 보여주는 함수 { image.Source = imageLoad(); } private ImageSource imageLoad() // 이미지를 불러오는 부분 { var bi = new BitmapImage(); bi.BeginInit(); bi.CacheOption = BitmapCacheOption.OnLoad; bi.UriSource = new Uri(@"..\Assets\images.png", UriKind.Relative); bi.EndInit(); return bi; } } | cs |
참고: http://stackoverflow.com/questions/265062/load-image-from-file-and-print-it-using-wpf-how