markus / KCOM / Events / Export.cs @ 95c73392
이력 | 보기 | 이력해설 | 다운로드 (8.69 KB)
1 |
using System; |
---|---|
2 |
using System.IO; |
3 |
using System.Collections.Generic; |
4 |
using System.Linq; |
5 |
using System.Text; |
6 |
using System.Threading.Tasks; |
7 |
using System.Drawing; |
8 |
using System.Drawing.Printing; |
9 |
using System.Collections; |
10 |
using System.ComponentModel; |
11 |
using System.Data; |
12 |
using System.Windows.Controls; |
13 |
using System.Windows.Media.Imaging; |
14 |
using System.Windows.Media; |
15 |
using System.Windows.Forms; |
16 |
//강인구 추가 |
17 |
namespace KCOM.Event |
18 |
{ |
19 |
public class Export |
20 |
{ |
21 |
//public RasterImage image |
22 |
//{ |
23 |
// get; |
24 |
// set; |
25 |
//} |
26 |
//public Image image |
27 |
//{ |
28 |
// get; |
29 |
// set; |
30 |
//} |
31 |
|
32 |
#region 원본 |
33 |
//public RasterImage Exporting(Canvas canvas, System.Drawing.Image img) |
34 |
//{ |
35 |
// //canvas.Children.Clear(); |
36 |
|
37 |
// //System.Windows.Controls.Image image_test; |
38 |
// //image_test = new System.Windows.Controls.Image(); |
39 |
|
40 |
// //canvas.Children.Add(image_test); |
41 |
|
42 |
|
43 |
// System.Drawing.Image Resultimg = new Bitmap(img, img.Width, img.Height); |
44 |
|
45 |
|
46 |
// //System.Drawing.Bitmap bmap = new System.Drawing.Bitmap(100, 100); |
47 |
// //System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img); |
48 |
|
49 |
// //Canvas에서 이미지로 변환시 테두리 보임 |
50 |
// //Size size = new Size((int)canvas.Width, (int)canvas.Height); |
51 |
// Size size = new Size((int)img.Width, (int)img.Height); |
52 |
|
53 |
|
54 |
// //System.Drawing.Bitmap bm = new Bitmap(img.Width, img.Height, img.PixelFormat); |
55 |
// //bm.SetResolution(92, 92); |
56 |
|
57 |
|
58 |
|
59 |
// BitmapEncoder encoder = new PngBitmapEncoder(); |
60 |
// RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)canvas.Width, (int)canvas.Height, 96d, 96d, PixelFormats.Default); |
61 |
// //RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96d, 96d, PixelFormats.Default); |
62 |
|
63 |
// renderBitmap.Render(canvas); |
64 |
// MemoryStream mystream = new MemoryStream(); |
65 |
// encoder.Frames.Add(BitmapFrame.Create(renderBitmap)); |
66 |
// encoder.Save(mystream); |
67 |
|
68 |
// System.Drawing.Image myImage; |
69 |
// myImage = System.Drawing.Image.FromStream(mystream); |
70 |
|
71 |
// //System.Drawing.Image img = System.Drawing.Bitmap.FromFile("d:\\hoonslogo.gif"); |
72 |
// //System.Drawing.Image img2 = System.Drawing.Bitmap.FromFile("d:\\btn_individual.gif"); |
73 |
// //g.DrawImage(img, 0, 0); |
74 |
// //g.DrawImage(myImage, 0, 0); |
75 |
|
76 |
// //g.Save(); |
77 |
|
78 |
// using (Graphics gr = Graphics.FromImage(Resultimg)) |
79 |
// { |
80 |
// gr.DrawImage(myImage, new System.Drawing.Point(0, 0)); |
81 |
// //gr.DrawImage(img, new Point(0, 0)); |
82 |
// } |
83 |
|
84 |
|
85 |
// //myImage.Save("C:\\Users\\강인구님\\Desktop\\test1.png"); |
86 |
// //img.Save("C:\\Users\\강인구님\\Desktop\\test2.png"); |
87 |
// //Resultimg.Save("C:\\Users\\강인구님\\Desktop\\test3.png"); |
88 |
|
89 |
// //myImage.Save("C:\\Users\\강인구님\\Desktop\\test3.png"); |
90 |
|
91 |
// image = RasterImageConverter.ConvertFromImage(Resultimg, ConvertFromImageOptions.None); |
92 |
|
93 |
|
94 |
|
95 |
// return image; |
96 |
//} |
97 |
#endregion |
98 |
|
99 |
#region LeadTools빼기 |
100 |
|
101 |
//코멘트가 있는 페이지 |
102 |
//public RasterImage Exporting(Canvas canvas, System.Drawing.Image img) |
103 |
//{ |
104 |
// System.Drawing.Image Resultimg = new System.Drawing.Bitmap(img, img.Width, img.Height); |
105 |
|
106 |
// Size size = new Size((int)img.Width, (int)img.Height); |
107 |
|
108 |
// BitmapEncoder encoder = new PngBitmapEncoder(); |
109 |
// RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)canvas.Width, (int)canvas.Height, 96d, 96d, PixelFormats.Default); |
110 |
|
111 |
// renderBitmap.Render(canvas); |
112 |
// MemoryStream mystream = new MemoryStream(); |
113 |
// encoder.Frames.Add(BitmapFrame.Create(renderBitmap)); |
114 |
// encoder.Save(mystream); |
115 |
|
116 |
// System.Drawing.Image myImage; |
117 |
// myImage = System.Drawing.Image.FromStream(mystream); |
118 |
|
119 |
// using (Graphics gr = Graphics.FromImage(Resultimg)) |
120 |
// { |
121 |
// gr.DrawImage(myImage, new System.Drawing.Point(0, 0)); |
122 |
// } |
123 |
// image = RasterImageConverter.ConvertFromImage(Resultimg, ConvertFromImageOptions.None); |
124 |
|
125 |
// return image; |
126 |
//} |
127 |
#endregion |
128 |
|
129 |
//코멘트가 있는 페이지 |
130 |
public System.Drawing.Image Exporting(Visual visual,double pageWidth,double pageHeight) |
131 |
{ |
132 |
BitmapEncoder encoder = new PngBitmapEncoder(); |
133 |
RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)pageWidth, (int)pageHeight, 96d, 96d, PixelFormats.Default); |
134 |
|
135 |
DrawingVisual drawingVisual = new DrawingVisual(); |
136 |
using (DrawingContext drawingContext = drawingVisual.RenderOpen()) |
137 |
{ |
138 |
VisualBrush visualBrush = new VisualBrush(visual); |
139 |
drawingContext.DrawRectangle(visualBrush, null, new System.Windows.Rect(new System.Windows.Point(0,0), new System.Windows.Point(pageWidth, pageHeight))); |
140 |
} |
141 |
|
142 |
renderBitmap.Render(drawingVisual); |
143 |
MemoryStream mystream = new MemoryStream(); |
144 |
encoder.Frames.Add(BitmapFrame.Create(renderBitmap)); |
145 |
encoder.Save(mystream); |
146 |
|
147 |
System.Drawing.Image myImage; |
148 |
myImage = System.Drawing.Image.FromStream(mystream); |
149 |
|
150 |
//using (Graphics gr = Graphics.FromImage(Resultimg)) |
151 |
//{ |
152 |
// //Graphics 객체에 Image 를 그릴때 Rectangle x,y 값을 추가 입력 |
153 |
// gr.DrawImage(myImage, new Rectangle(0,0,img.Width,img.Height)); |
154 |
//} |
155 |
|
156 |
//image = RasterImageConverter.ConvertFromImage(Resultimg, ConvertFromImageOptions.None); |
157 |
|
158 |
return myImage; |
159 |
} |
160 |
|
161 |
public bool ExportingFile(Visual visual, double pageWidth, double pageHeight, string fileName) |
162 |
{ |
163 |
bool result = false; |
164 |
|
165 |
try |
166 |
{ |
167 |
JpegBitmapEncoder png = new JpegBitmapEncoder(); |
168 |
RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)pageWidth, (int)pageHeight, 96d, 96d, PixelFormats.Default); |
169 |
|
170 |
DrawingVisual drawingVisual = new DrawingVisual(); |
171 |
DrawingContext drawingContext = drawingVisual.RenderOpen(); |
172 |
VisualBrush visualBrush = new VisualBrush(visual); |
173 |
drawingContext.DrawRectangle(visualBrush, null, new System.Windows.Rect(new System.Windows.Point(0, 0), new System.Windows.Point(pageWidth, pageHeight))); |
174 |
drawingContext.Close(); |
175 |
|
176 |
renderBitmap.Render(drawingVisual); |
177 |
png.Frames.Add(BitmapFrame.Create(renderBitmap)); |
178 |
|
179 |
using (FileStream fileStream = File.Create(fileName)) |
180 |
{ |
181 |
png.Save(fileStream); |
182 |
|
183 |
//encoder = null; |
184 |
//renderBitmap.Clear(); |
185 |
//drawingVisual = null; |
186 |
//drawingContext.Close(); |
187 |
//visualBrush = null; |
188 |
//fileStream.Close(); |
189 |
//fileStream.Dispose(); |
190 |
|
191 |
result = true; |
192 |
} |
193 |
} |
194 |
catch (Exception ex) |
195 |
{ |
196 |
throw new Exception("ExportingFile Error ", ex); |
197 |
} |
198 |
finally |
199 |
{ |
200 |
GC.Collect(); |
201 |
GC.WaitForPendingFinalizers(); |
202 |
GC.Collect(); |
203 |
} |
204 |
|
205 |
return result; |
206 |
} |
207 |
|
208 |
public System.Drawing.Image Exporting_old(Canvas canvas, System.Drawing.Image img) |
209 |
{ |
210 |
System.Drawing.Image Resultimg = new System.Drawing.Bitmap(img, img.Width, img.Height); |
211 |
|
212 |
Size size = new Size((int)img.Width, (int)img.Height); |
213 |
|
214 |
BitmapEncoder encoder = new PngBitmapEncoder(); |
215 |
RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)canvas.Width, (int)canvas.Height, 96d, 96d, PixelFormats.Default); |
216 |
|
217 |
renderBitmap.Render(canvas); |
218 |
MemoryStream mystream = new MemoryStream(); |
219 |
encoder.Frames.Add(BitmapFrame.Create(renderBitmap)); |
220 |
encoder.Save(mystream); |
221 |
|
222 |
System.Drawing.Image myImage; |
223 |
myImage = System.Drawing.Image.FromStream(mystream); |
224 |
|
225 |
using (Graphics gr = Graphics.FromImage(Resultimg)) |
226 |
{ |
227 |
//Graphics 객체에 Image 를 그릴때 Rectangle x,y 값을 추가 입력 |
228 |
gr.DrawImage(myImage, new Rectangle(0, 0, img.Width, img.Height)); |
229 |
} |
230 |
|
231 |
//image = RasterImageConverter.ConvertFromImage(Resultimg, ConvertFromImageOptions.None); |
232 |
|
233 |
return Resultimg; |
234 |
} |
235 |
} |
236 |
} |