scottxu

导航

Silverlight打印图片不全的问题

前两天在使用Silverlight的打印功能,正好电脑连着打印机,就随手打印,一看和原来预想的有些不一样,只打印出来了一半。

代码如下:

            PrintDocument pd = new PrintDocument();
            pd.PrintPage += (s , e1) => 
            {
                e1.PageVisual = MyMap; 
                mytest5.Text = "Height : " + e1.PrintableArea.Height.ToString() + "Width : " + e1.PrintableArea.Width.ToString(); 
            };
            pd.Print("MyMap");

效果如图(当然不是彩色的,这只是一个截图,示意一下):

只打出来一半,我要的是全幅。后来又试了其他的图片,打印也不全。

后来上网找,终于找到解决方案了。要写一个扩展类。其实一看就明白了,方法挺好。

代码:

1 public static class Extensions
2 {
3 public static void Print(this FrameworkElement element ,
4 string Document , HorizontalAlignment HorizontalAlignment ,
5 VerticalAlignment VerticalAlignment , Thickness PageMargin ,
6 bool PrintLandscape , bool ShrinkToFit , Action OnPrintComplete)
7 {
8 Print(new List<FrameworkElement>() { element } , Document ,
9 HorizontalAlignment , VerticalAlignment , PageMargin ,
10 PrintLandscape , ShrinkToFit , OnPrintComplete);
11 }
12
13 //public static void Print(this UIElementCollection elements , string Document ,
14 // HorizontalAlignment HorizontalAlignment ,
15 // VerticalAlignment VerticalAlignment , Thickness PageMargin ,
16 // bool PrintLandscape , bool ShrinkToFit , Action OnPrintComplete)
17 //{
18 // Print(elements.ToList() , Document , HorizontalAlignment ,
19 // VerticalAlignment , PageMargin , PrintLandscape ,
20 // ShrinkToFit , OnPrintComplete);
21 //}
22  
23 public static void Print<T>(this List<T> elements ,
24 string Document , HorizontalAlignment HorizontalAlignment ,
25 VerticalAlignment VerticalAlignment , Thickness PageMargin ,
26 bool PrintLandscape , bool ShrinkToFit , Action OnPrintComplete)
27 {
28 PrintDocument printDocument = new PrintDocument();
29 PageMargin = PageMargin == null ? new Thickness(10) : PageMargin;
30 Document = ( string.IsNullOrEmpty(Document) ) ? "Print Document" : Document;
31 int currentItemIndex = 0;
32 printDocument.PrintPage += (s , e) =>
33 {
34 if (!typeof(FrameworkElement).IsAssignableFrom(
35 elements[currentItemIndex].GetType()))
36 {
37 throw new Exception("Element must be an " +
38 "object inheriting from FrameworkElement");
39 }
40
41 FrameworkElement element = elements[currentItemIndex] as FrameworkElement;
42
43 if (element.Parent == null || element.ActualWidth == double.NaN ||
44 element.ActualHeight == double.NaN)
45 {
46 throw new Exception("Element must be rendered, " +
47 "and must have a parent in order to print.");
48 }
49
50 TransformGroup transformGroup = new TransformGroup();
51
52 //First move to middle of page...
53   transformGroup.Children.Add(new TranslateTransform()
54 {
55 X = ( e.PrintableArea.Width - element.ActualWidth ) / 2 ,
56 Y = ( e.PrintableArea.Height - element.ActualHeight ) / 2
57 });
58 double scale = 1;
59 if (PrintLandscape)
60 {
61 //Then, rotate around the center
62 transformGroup.Children.Add(new RotateTransform()
63 {
64 Angle = 90 ,
65 CenterX = e.PrintableArea.Width / 2 ,
66 CenterY = e.PrintableArea.Height / 2
67 });
68
69 if (ShrinkToFit)
70 {
71 if (( element.ActualWidth + PageMargin.Left +
72 PageMargin.Right ) > e.PrintableArea.Height)
73 {
74 scale = Math.Round(e.PrintableArea.Height /
75 ( element.ActualWidth + PageMargin.Left + PageMargin.Right ) , 2);
76 }
77 if (( element.ActualHeight + PageMargin.Top + PageMargin.Bottom ) >
78 e.PrintableArea.Width)
79 {
80 double scale2 = Math.Round(e.PrintableArea.Width /
81 ( element.ActualHeight + PageMargin.Top + PageMargin.Bottom ) , 2);
82 scale = ( scale2 < scale ) ? scale2 : scale;
83 }
84 }
85 }
86 else if (ShrinkToFit)
87 {
88 //Scale down to fit the page + margin
89
90 if (( element.ActualWidth + PageMargin.Left +
91 PageMargin.Right ) > e.PrintableArea.Width)
92 {
93 scale = Math.Round(e.PrintableArea.Width /
94 ( element.ActualWidth + PageMargin.Left + PageMargin.Right ) , 2);
95 }
96 if (( element.ActualHeight + PageMargin.Top + PageMargin.Bottom ) >
97 e.PrintableArea.Height)
98 {
99 double scale2 = Math.Round(e.PrintableArea.Height /
100 ( element.ActualHeight + PageMargin.Top + PageMargin.Bottom ) , 2);
101 scale = ( scale2 < scale ) ? scale2 : scale;
102 }
103 }
104
105 //Scale down to fit the page + margin
106 if (scale != 1)
107 {
108 transformGroup.Children.Add(new ScaleTransform()
109 {
110 ScaleX = scale ,
111 ScaleY = scale ,
112 CenterX = e.PrintableArea.Width / 2 ,
113 CenterY = e.PrintableArea.Height / 2
114 });
115 }
116
117 if (VerticalAlignment == VerticalAlignment.Top)
118 {
119 //Now move to Top
120 if (PrintLandscape)
121 {
122 transformGroup.Children.Add(new TranslateTransform()
123 {
124 X = 0 ,
125 Y = PageMargin.Top - ( e.PrintableArea.Height -
126 ( element.ActualWidth * scale ) ) / 2
127 });
128 }
129 else
130 {
131 transformGroup.Children.Add(new TranslateTransform()
132 {
133 X = 0 ,
134 Y = PageMargin.Top - ( e.PrintableArea.Height -
135 ( element.ActualHeight * scale ) ) / 2
136 });
137 }
138 }
139 else if (VerticalAlignment == VerticalAlignment.Bottom)
140 {
141 //Now move to Bottom
142 if (PrintLandscape)
143 {
144 transformGroup.Children.Add(new TranslateTransform()
145 {
146 X = 0 ,
147 Y = ( ( e.PrintableArea.Height -
148 ( element.ActualWidth * scale ) ) / 2 ) - PageMargin.Bottom
149 });
150 }
151 else
152 {
153 transformGroup.Children.Add(new TranslateTransform()
154 {
155 X = 0 ,
156 Y = ( ( e.PrintableArea.Height -
157 ( element.ActualHeight * scale ) ) / 2 ) - PageMargin.Bottom
158 });
159 }
160 }
161
162 if (HorizontalAlignment == HorizontalAlignment.Left)
163 {
164 //Now move to Left
165 if (PrintLandscape)
166 {
167 transformGroup.Children.Add(new TranslateTransform()
168 {
169 X = PageMargin.Left - ( e.PrintableArea.Width -
170 ( element.ActualHeight * scale ) ) / 2 ,
171 Y = 0
172 });
173 }
174 else
175 {
176 transformGroup.Children.Add(new TranslateTransform()
177 {
178 X = PageMargin.Left - ( e.PrintableArea.Width -
179 ( element.ActualWidth * scale ) ) / 2 ,
180 Y = 0
181 });
182 }
183 }
184 else if (HorizontalAlignment == HorizontalAlignment.Right)
185 {
186 //Now move to Right
187 if (PrintLandscape)
188 {
189 transformGroup.Children.Add(new TranslateTransform()
190 {
191 X = ( ( e.PrintableArea.Width -
192 ( element.ActualHeight * scale ) ) / 2 ) - PageMargin.Right ,
193 Y = 0
194 });
195 }
196 else
197 {
198 transformGroup.Children.Add(new TranslateTransform()
199 {
200 X = ( ( e.PrintableArea.Width -
201 ( element.ActualWidth * scale ) ) / 2 ) - PageMargin.Right ,
202 Y = 0
203 });
204 }
205 }
206
207 e.PageVisual = element;
208 e.PageVisual.RenderTransform = transformGroup;
209
210 //Increment to next item,
211 currentItemIndex++;
212
213 //If the currentItemIndex is less than the number of elements, keep printing
214 e.HasMorePages = currentItemIndex < elements.Count;
215 };
216
217 printDocument.EndPrint += delegate(object sender , EndPrintEventArgs e)
218 {
219 foreach (var item in elements)
220 {
221 FrameworkElement element = item as FrameworkElement;
222 //Reset everything...
223 TransformGroup transformGroup = new TransformGroup();
224 transformGroup.Children.Add(
225 new ScaleTransform() { ScaleX = 1 , ScaleY = 1 });
226 transformGroup.Children.Add(new RotateTransform() { Angle = 0 });
227 transformGroup.Children.Add(
228 new TranslateTransform() { X = 0 , Y = 0 });
229 element.RenderTransform = transformGroup;
230 }
231
232 //Callback to complete
233 if (OnPrintComplete != null)
234 {
235 OnPrintComplete();
236 }
237 };
238
239 printDocument.Print(Document);
240 }
241 }

再调用这个扩展类就行了。

            Extensions.Print(MyMap , "MyMap" ,
                HorizontalAlignment.Center , VerticalAlignment.Top ,
                new Thickness(10) , true , true , null);

这样就能打印全了,是倒过来的,不过也没关系,反正是打全了,哈哈:

参考blog地址:http://www.codeproject.com/KB/silverlight/SilverlightEasyPrint.aspx

posted on 2011-02-17 16:27  scottxu  阅读(2975)  评论(3编辑  收藏  举报