private void convertImageToTextButton_Click2(object sender, EventArgs eventArgs) { _ColorDictionary = new ColorDictionaryEncoder(compressionRateTrackBar.Value); bool doEncryption = false; string tempFilePath = ""; Bitmap img; if (string.IsNullOrWhiteSpace(_ImportImageFilePath)) { MessageBox.Show(this, "No output path", "Choose a file to output to.", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (compressCheckBox.Checked) { // compress if checked try { // get temp folder output path var importPath = Path.GetDirectoryName(_ImportImageFilePath); var tempFileName = Path.GetFileNameWithoutExtension(_ImportImageFilePath) + Guid.NewGuid().ToString() + Path.GetExtension(_ImportImageFilePath); var newFilePath = Path.Combine(importPath, tempFileName); tempFilePath = newFilePath; Encoder.CompressImage(_ImportImageFilePath, newFilePath, compressionRateTrackBar.Value); _ImportImageFilePath = newFilePath; } catch (Exception e) { //unable to compress, ignore } } try { img = new Bitmap(_ImportImageFilePath); } catch (Exception bitmapException) { MessageBox.Show(this, "Invalid Image Type", "Unsupported image type. Try another file", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (img.Width > short.MaxValue) { MessageBox.Show(this, "Image Too Large", "Image is too large(too wide). Try another file.", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (img.Height > short.MaxValue) { MessageBox.Show(this, "Image Too Large", "Image is too large(too tall). Try another file.", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrWhiteSpace(_OutputTextFilePath)) { MessageBox.Show(this, "No output path", "Choose a file to output to.", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } List <byte> bytesToWriteToFile = new List <byte>(); // first byte contains whether it's encrpyed or not byte isEncryptedByte = Convert.ToByte(doEncryption); bytesToWriteToFile.Add(isEncryptedByte); // next 4 bytes contains the width var widthByteArray = Utilities.ConvertIntToByteArray(img.Width); bytesToWriteToFile.Add(widthByteArray[0]); bytesToWriteToFile.Add(widthByteArray[1]); bytesToWriteToFile.Add(widthByteArray[2]); bytesToWriteToFile.Add(widthByteArray[3]); // next four bytes contains the height var heightByteArray = Utilities.ConvertIntToByteArray(img.Height); bytesToWriteToFile.Add(heightByteArray[0]); bytesToWriteToFile.Add(heightByteArray[1]); bytesToWriteToFile.Add(heightByteArray[2]); bytesToWriteToFile.Add(heightByteArray[3]); var imageExtension = Path.GetExtension(_ImportImageFilePath); ImageType imageTypeEnum = ImageTypeDictionary.GetEnumFromString(imageExtension); byte imageType = ImageTypeDictionary.GetIntFromImageType(imageTypeEnum); bytesToWriteToFile.Add(imageType); // add ignore alpha channel byte to file byte ignoreAlpha = Convert.ToByte(ignoreAlphaChannelCheckbox.Checked); bytesToWriteToFile.Add(ignoreAlpha); //next byte is the compression rate var compressionRateDecimal = compressionRateTrackBar.Value / 100.0; byte bitsToCompressTo = (byte)Math.Floor(compressionRateDecimal * 8); if (bitsToCompressTo == 0) { bitsToCompressTo = 1; } // not adding compression rate right now //bytesToWriteToFile.Add(bitsToCompressTo); for (int i = 0; i < img.Width; i++) { for (int j = 0; j < img.Height; j++) { Color pixel = img.GetPixel(i, j); byte a = pixel.A; byte b = pixel.B; byte g = pixel.G; byte r = pixel.R; _ColorDictionary.AddEntry(a, b, g, r); } int progress = (int)((i * 1.0 / img.Width) * 100); progressBar.Value = progress; progressLabel.Text = progress.ToString() + " %"; progressLabel.Refresh(); } bytesToWriteToFile.AddRange(_ColorDictionary.GetDictionaryByteValues()); for (int i = 0; i < img.Width; i++) { for (int j = 0; j < img.Height; j++) { Color pixel = img.GetPixel(i, j); byte a = pixel.A; byte b = pixel.B; byte g = pixel.G; byte r = pixel.R; byte byteToWrite = _ColorDictionary.GetBytesForColors(a, b, g, r); bytesToWriteToFile.Add(byteToWrite); } int progress = (int)((i * 1.0 / img.Width) * 100); progressBar.Value = progress; progressLabel.Text = progress.ToString() + " %"; progressLabel.Refresh(); } var uncompressed = bytesToWriteToFile.ToArray(); var compressed = Compression.Compress(uncompressed); File.WriteAllBytes(_OutputTextFilePath, compressed); // Requires System.IO progressBar.Value = 0; progressLabel.Text = "Done"; _ImportImageFilePath = inputFileTextBox.Text; if (!string.IsNullOrEmpty(tempFilePath)) { img.Dispose(); //File.Delete(tempFilePath); } }