forked from kemot90/APO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImagePreview.cs
162 lines (140 loc) · 4.81 KB
/
ImagePreview.cs
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace IMGProc {
public partial class ImagePreview : Form {
private FastBitmap bmp;
private Graphics graphics;
private String path;
private int count;
private bool changed = false;
private bool grayscale = false;
public bool Grayscale {
get { return grayscale; }
set { grayscale = value; }
}
public string FileName {
get { return Path.GetFileName(path);}
}
public bool Changed {
get { return changed; }
set {
changed = value;
UpdateText();
}
}
public FastBitmap Image {
get { return bmp; }
set { bmp = value; }
}
public String FilePath {
get { return path; }
set { path = value; }
}
public int Count {
get { return count; }
set {
count = value;
UpdateText();
}
}
public ImagePreview(String path) {
InitializeComponent();
this.path = path;
StreamReader reader = new StreamReader(path);
Bitmap bmpTemp = (Bitmap)Bitmap.FromStream(reader.BaseStream);
Bitmap bmpTemp2 = new Bitmap(bmpTemp.Size.Width, bmpTemp.Size.Height);
bmpTemp2.SetResolution(bmpTemp.HorizontalResolution, bmpTemp.VerticalResolution);
Graphics gr = Graphics.FromImage(bmpTemp2);
gr.DrawImage(bmpTemp, new Point());
bmpTemp.Dispose();
reader.Close();
bmp = new FastBitmap(bmpTemp2);
bmp.Bitmap.SetResolution(96, 96);
Text = path;
ClientSize = bmp.Size;
graphicsPanel.Left = 0;
graphicsPanel.Top = 0;
graphicsPanel.Size = bmp.Size;
graphics = this.graphicsPanel.CreateGraphics();
}
public ImagePreview(FastBitmap tempBmp) {
InitializeComponent();
FastBitmap newBmp = new FastBitmap(tempBmp.Size.Width, tempBmp.Size.Height);
for (int i = 0; i < tempBmp.Size.Width; i++)
{
for (int j = 0; j < tempBmp.Size.Height; j++)
{
newBmp[i, j] = tempBmp[i, j];
}
}
//this.path = path;
this.bmp = newBmp;
Text = "Nowy";
ClientSize = tempBmp.Size;
graphicsPanel.Left = 0;
graphicsPanel.Top = 0;
graphicsPanel.Size = tempBmp.Size;
graphics = this.graphicsPanel.CreateGraphics();
}
public void Redraw() {
//graphics.DrawImage(bmp, new Point());
bmp.Draw(graphics, 0, 0);
}
private void graphicsPanel_Paint(object sender, PaintEventArgs e) {
Redraw();
}
public void Save() {
Save(path);
}
public void Save(string filePath) {
StreamWriter writer = new StreamWriter(filePath);
ImageFormat format;
switch (Path.GetExtension(filePath).ToLower()) {
case ".jpg":
case ".jpeg":
format = ImageFormat.Jpeg;
break;
case ".gif":
format = ImageFormat.Gif;
break;
case ".png":
format = ImageFormat.Png;
break;
case ".tiff":
format = ImageFormat.Tiff;
break;
default:
format = ImageFormat.Bmp;
break;
}
bmp.Save(writer.BaseStream, format);
writer.Close();
}
public void UpdateText() {
Text = Path.GetFileName(path) + ((count > 0) ? ("(" + count.ToString() + ")") : "") + (changed ? " *" : "");
}
private void ImageForm_FormClosing(object sender, FormClosingEventArgs e) {
if (changed) {
switch (MessageBox.Show("Image \"" + Path.GetFileName(path) + "\" was changed. Save changes?", "Warning", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)) {
case DialogResult.Yes:
Save();
break;
case DialogResult.Cancel:
e.Cancel = true;
break;
default:
break;
}
}
}
}
}