FastReport

Форк
0
224 строки · 6.9 Кб
1
using Avalonia.Controls;
2
using Avalonia.Input;
3
using Avalonia.Interactivity;
4
using Avalonia.Markup.Xaml;
5
using FastReport;
6
using FastReport.Export.Image;
7
using System;
8
using System.Collections.Generic;
9
using System.IO;
10
using System.Linq;
11

12
namespace Viewer
13
{
14
    public class MainWindow : Window
15
    {
16
        public MainWindow()
17
        {
18
            InitializeComponent();
19
            imHeight = img.Height;
20
            imWidth = img.Width;
21
        }
22

23
        private Button OpenBtn;
24
        private Image img;
25
        private TextBox PageNumber;
26

27
        private void InitializeComponent()
28
        {
29
            AvaloniaXamlLoader.Load(this);
30
            OpenBtn = this.FindControl<Button>("Open");
31
            OpenBtn.Click += OpenFile;
32

33
            this.img = this.FindControl<Image>("img");
34
            this.PageNumber = this.FindControl<TextBox>("PageNumber");
35
        }
36

37
        public List<Avalonia.Media.Imaging.Bitmap> pages = new List<Avalonia.Media.Imaging.Bitmap>();
38
        public Report Report
39
        {
40
            get { return report; }
41
            set
42
            {
43
                ex = new FastReport.Export.Image.ImageExport();
44
                ex.HasMultipleFiles = true;
45
                report = value;
46
                SetContent(report);
47
                SetImage();
48
            }
49
        }
50

51
        public int CurrentPage
52
        {
53
            get { return currentPage; }
54
            set
55
            {
56
                if (value >= 0 && value < pages.Count)
57
                    currentPage = value;
58
            }
59
        }
60

61
        private void SetContent(Report report)
62
        {
63
            DeleteTempFiles();
64
            ex.ImageFormat = ImageExportFormat.Png;
65
            ex.Resolution = 96;
66
            Random rnd = new Random();
67
            ex.Export(report, Directory.GetCurrentDirectory() + "/test." + rnd.Next(100) + ".png");
68
            foreach (string file in ex.GeneratedFiles)
69
            {
70
                //Avalonia.Media.Imaging.Bitmap image = new Avalonia.Media.Imaging.Bitmap();
71
                //image.BeginInit();
72
                //image.CacheOption = BitmapCacheOption.OnLoad;
73
                //image.UriSource = new Uri(file);
74
                //image.EndInit();
75
                pages.Add(new Avalonia.Media.Imaging.Bitmap(file));
76
            }
77
            CurrentPage = 0;
78

79

80
        }
81

82
        public void DeleteTempFiles()
83
        {
84
            FileInfo[] path = new DirectoryInfo(Directory.GetCurrentDirectory()).GetFiles("*test*", SearchOption.AllDirectories);
85
            pages.Clear();
86
            foreach (FileInfo file in path)
87
            {
88
                File.Delete(file.FullName);
89
            }
90
        }
91

92
        public void SetImage()
93
        {
94
            //if (CurrentPage >= 0 && CurrentPage < pages.Count())
95
            //{
96
            //    im.Source = null;
97
            img.Source = pages[CurrentPage];
98
            //}
99
            img.Height = imHeight;
100
            img.Width = imWidth;
101
            PageNumber.Text = (CurrentPage + 1).ToString();
102
        }
103

104
        public void FirstClick(object sender, Avalonia.Interactivity.RoutedEventArgs e)
105
        {
106
            if (pages.Any())
107
            {
108
                CurrentPage = 0;
109
                SetImage();
110
            }
111
        }
112

113
        public void Prev_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
114
        {
115
            if (currentPage > 0)
116
            {
117
                CurrentPage--;
118
                SetImage();
119
            }
120
        }
121

122
        public void Next_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
123
        {
124
            if (CurrentPage >= 0 && CurrentPage < pages.Count())
125
            {
126
                CurrentPage++;
127
                SetImage();
128
            }
129
        }
130

131
        public void Last_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
132
        {
133
            if (pages.Any())
134
            {
135
                CurrentPage = pages.Count - 1;
136
                SetImage();
137
            }
138
        }
139

140
        public void PageNumber_KeyDown(object sender, KeyEventArgs e)
141
        {
142

143
            if (e.Key == Key.Enter)
144
            {
145
                if (int.Parse(PageNumber.Text) > 0)
146
                {
147
                    CurrentPage = int.Parse(PageNumber.Text) - 1;
148
                    SetImage();
149
                }
150
            }
151
        }
152

153
        private async void OpenFile(object sender, RoutedEventArgs e)
154
        {
155
            OpenFileDialog myDialog = new OpenFileDialog();
156
            myDialog.Filters.Add(new FileDialogFilter() { Name = "�������������� ������(*.FPX)", Extensions = new List<string> { "fpx" } });
157
            myDialog.AllowMultiple = false;
158

159
            var result = await myDialog.ShowAsync(this);
160

161
            if (result != null && result.Length > 0)
162
                LoadReport(result[0]);
163
        }
164

165
        void LoadReport(string report_name)
166
        {
167
            Report rep = new Report();
168
            rep.LoadPrepared(report_name);
169
            Report = rep;
170
        }
171

172

173
        public void Zoom_in_Click(object sender, RoutedEventArgs e)
174
        {
175
            if (ex != null)
176
            {
177
                ex.ImageFormat = ImageExportFormat.Png;
178
                ex.Resolution += 25;
179
                ex.PageNumbers = (CurrentPage + 1).ToString();
180
                //ex.Zoom += 0.25f;
181
                ex.Export(Report, Directory.GetCurrentDirectory() + "/testZoom.png");
182
                //Report.Export(ex, stream);
183

184
                if (CurrentPage >= 0 && CurrentPage < pages.Count)
185
                    img.Source = LoadImage(Directory.GetCurrentDirectory() + "/testZoom.png");
186
                //new BitmapImage(new Uri(Directory.GetCurrentDirectory() + "/testZoom.png"));
187
                PageNumber.Text = (CurrentPage + 1).ToString();
188
            }
189
            img.Width += 50;
190
            img.Height += 50;
191
        }
192

193
        private static Avalonia.Media.Imaging.Bitmap LoadImage(string file)
194
        {
195
            return new Avalonia.Media.Imaging.Bitmap(file);
196
        }
197
        public void Zoom_out_Click(object sender, RoutedEventArgs e)
198
        {
199

200
            if (ex != null)
201
            {
202
                ex.ImageFormat = ImageExportFormat.Png;
203
                ex.Resolution -= 25;
204
                ex.PageNumbers = (CurrentPage + 1).ToString();
205
                //ex.Zoom += 0.25f;
206
                ex.Export(Report, Directory.GetCurrentDirectory() + "/testZoom.png");
207
                //Report.Export(ex, stream);
208

209
                if (CurrentPage >= 0 && CurrentPage < pages.Count)
210
                    img.Source = LoadImage(Directory.GetCurrentDirectory() + "/testZoom.png");
211
                //new BitmapImage(new Uri(Directory.GetCurrentDirectory() + "/testZoom.png"));
212
                PageNumber.Text = (CurrentPage + 1).ToString();
213
            }
214
            img.Width -= 50;
215
            img.Height -= 50;
216
        }
217

218
        private Report report;
219
        private FastReport.Export.Image.ImageExport ex;
220
        private int currentPage = 0;
221
        private readonly double imHeight;
222
        private readonly double imWidth;
223
    }
224
}

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.