프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / KCOM / Events / Export.cs @ a9a82876

이력 | 보기 | 이력해설 | 다운로드 (8.85 KB)

1 787a4489 KangIngu
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 86bff326 taeseongkim
        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 a9a82876 taeseongkim
        public bool ExportingFile(Canvas visual, double pageWidth, double pageHeight, string fileName)
162 77cdac33 taeseongkim
        {
163
            bool result = false;
164
165
            try
166
            {
167 a9a82876 taeseongkim
                RenderOptions.SetBitmapScalingMode(visual, BitmapScalingMode.HighQuality);
168 77cdac33 taeseongkim
                JpegBitmapEncoder png = new JpegBitmapEncoder();
169 a9a82876 taeseongkim
                png.QualityLevel = 100;
170 77cdac33 taeseongkim
171 a9a82876 taeseongkim
                double dpi = 96d;
172
173
                RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)pageWidth, (int)pageHeight, dpi, dpi, PixelFormats.Pbgra32);
174
175
                //DrawingVisual drawingVisual = new DrawingVisual();
176
                //DrawingContext drawingContext = drawingVisual.RenderOpen();
177
                ////VisualBrush visualBrush = new VisualBrush(visual);
178
                //drawingContext.DrawRectangle(visual, null, new System.Windows.Rect(new System.Windows.Point(0, 0), new System.Windows.Point(pageWidth, pageHeight)));
179
                //drawingContext.Close();
180 77cdac33 taeseongkim
181 a9a82876 taeseongkim
                renderBitmap.Render(visual);
182 77cdac33 taeseongkim
                png.Frames.Add(BitmapFrame.Create(renderBitmap));
183
184
                using (FileStream fileStream = File.Create(fileName))
185
                {
186
                    png.Save(fileStream);
187
188
                    //encoder = null;
189
                    //renderBitmap.Clear();
190
                    //drawingVisual = null;
191
                    //drawingContext.Close();
192
                    //visualBrush = null;
193
                    //fileStream.Close();
194
                    //fileStream.Dispose();
195
196
                    result = true;
197
                }
198
            }
199
            catch (Exception ex)
200
            {
201
                throw new Exception("ExportingFile Error ", ex);
202
            }
203
            finally
204
            {
205
                GC.Collect();
206
                GC.WaitForPendingFinalizers();
207
                GC.Collect();
208
            }
209
210
            return result;
211
        }
212
213 86bff326 taeseongkim
        public System.Drawing.Image Exporting_old(Canvas canvas, System.Drawing.Image img)
214 787a4489 KangIngu
        {
215
            System.Drawing.Image Resultimg = new System.Drawing.Bitmap(img, img.Width, img.Height);
216
217
            Size size = new Size((int)img.Width, (int)img.Height);
218
219
            BitmapEncoder encoder = new PngBitmapEncoder();
220
            RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)canvas.Width, (int)canvas.Height, 96d, 96d, PixelFormats.Default);
221
222
            renderBitmap.Render(canvas);
223
            MemoryStream mystream = new MemoryStream();
224
            encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
225
            encoder.Save(mystream);
226
227
            System.Drawing.Image myImage;
228
            myImage = System.Drawing.Image.FromStream(mystream);
229
230
            using (Graphics gr = Graphics.FromImage(Resultimg))
231
            {
232 e8f03842 djkim
                //Graphics 객체에 Image 를 그릴때 Rectangle x,y 값을 추가 입력  
233 86bff326 taeseongkim
                gr.DrawImage(myImage, new Rectangle(0, 0, img.Width, img.Height));
234 787a4489 KangIngu
            }
235
236
            //image = RasterImageConverter.ConvertFromImage(Resultimg, ConvertFromImageOptions.None);
237
238
            return Resultimg;
239
        }
240
    }
241
}
클립보드 이미지 추가 (최대 크기: 500 MB)