-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvtoText.cs
198 lines (179 loc) · 6.92 KB
/
ConvtoText.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ExtBloq
{
public partial class ConvtoText : Form
{
private string filepath;
public ConvtoText()
{
InitializeComponent();
}
public ConvtoText(string filepath):this()
{
this.filepath = filepath;
}
private void ConvtoText_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(filepath)) // Check if filepath is not null or empty
{
try
{
pictureBox1.Image = Image.FromFile(filepath); // Load the image into pictureBox1
}
catch (Exception ex)
{
MessageBox.Show("Error loading image: " + ex.Message);
}
}
}
private void buttonEnBase64_Click(object sender, EventArgs e)
{
// Convert image to Base64
try
{
using (MemoryStream memoryStream = new MemoryStream())
{
pictureBox1.Image.Save(memoryStream, pictureBox1.Image.RawFormat);
byte[] imageBytes = memoryStream.ToArray();
string base64String = Convert.ToBase64String(imageBytes);
progressBar1.Maximum = (int)Math.Ceiling((double)base64String.Length / 4096); // Set the maximum value based on the length of Base64 string
outputTextBox.Text = base64String;
}
}
catch (Exception ex)
{
MessageBox.Show("Error encoding image to Base64: " + ex.Message);
}
}
private void buttonOpen_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "Image Files|*.jpg;*.jpeg;*.png;*.bmp;*.gif";
openFileDialog1.Title = "Select an Image File";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
filepath = openFileDialog1.FileName;
pictureBox1.Image = Image.FromFile(filepath);
}
}
private void buttonEnHex_Click(object sender, EventArgs e)
{
try
{
// Convert image to HEX
using (MemoryStream memoryStream = new MemoryStream())
{
pictureBox1.Image.Save(memoryStream, pictureBox1.Image.RawFormat);
byte[] imageBytes = memoryStream.ToArray();
progressBar1.Maximum = imageBytes.Length;
StringBuilder hexString = new StringBuilder();
for (int i = 0; i < imageBytes.Length; i++)
{
hexString.Append(imageBytes[i].ToString("X2"));
progressBar1.Value = i;
}
outputTextBox.Text = hexString.ToString();
}
}
catch(Exception ex)
{
MessageBox.Show("Error encoding HEX string: " + ex.Message);
}
}
private void buttonDecBase64_Click(object sender, EventArgs e)
{
// Convert Base64 string to image
try
{
progressBar1.Value = 0;
byte[] imageBytes = Convert.FromBase64String(outputTextBox.Text);
progressBar1.Maximum = imageBytes.Length;
using (MemoryStream memoryStream = new MemoryStream(imageBytes))
{
pictureBox1.Image = Image.FromStream(memoryStream);
}
}
catch (Exception ex)
{
MessageBox.Show("Error decoding Base64 string: " + ex.Message);
}
}
private void buttonDecHex_Click(object sender, EventArgs e)
{
// Convert HEX string to image
try
{
progressBar1.Value = 0;
string hexString = outputTextBox.Text;
byte[] imageBytes = new byte[hexString.Length / 2];
for (int i = 0; i < imageBytes.Length; i++)
{
imageBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
progressBar1.Value = i;
}
using (MemoryStream memoryStream = new MemoryStream(imageBytes))
{
pictureBox1.Image = Image.FromStream(memoryStream);
}
}
catch (Exception ex)
{
MessageBox.Show("Error decoding HEX string: " + ex.Message);
}
}
private void buttonReset_Click(object sender, EventArgs e)
{
pictureBox1.Image = null;
}
private void button5_Click(object sender, EventArgs e)
{
if (pictureBox1.Image != null)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "JPEG Image|*.jpg|PNG Image|*.png|Bitmap Image|*.bmp|GIF Image|*.gif";
saveFileDialog1.Title = "Save an Image File";
saveFileDialog1.ShowDialog();
// If the file name is not an empty string, open it for saving.
if (saveFileDialog1.FileName != "")
{
// Saves the Image via a FileStream created by the OpenFile method.
System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog1.OpenFile();
// Saves the Image in the appropriate format based upon the extension.
switch (saveFileDialog1.FilterIndex)
{
case 1:
pictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case 2:
pictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Png);
break;
case 3:
pictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Bmp);
break;
case 4:
pictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Gif);
break;
}
fs.Close();
}
}
else
{
MessageBox.Show("No image to save.");
}
}
private void buttonCopyAll_Click(object sender, EventArgs e)
{
Clipboard.SetDataObject(outputTextBox.Text);
}
}
}