SHARE
TWEET

TweetImager

DavidRogers13s Sep 28th, 2014 (edited) 252 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Drawing.Imaging;
  8. using System.IO;
  9. using System.IO.Compression;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14.  
  15. namespace ImageCompression
  16. {
  17.     public partial class TweetImager : Form
  18.     {
  19.         private class PositonUnique
  20.         {
  21.             public int Position { get; set; }
  22.             public byte Data { get; set; }
  23.         }
  24.  
  25.         //I've hard coded the JPEG header by running several files, then pulling out shared elements with all of them
  26.         //This method isn't the preffered method in most cases(for better method see: http://www.w3.org/Graphics/JPEG/jfif3.pdf or something like this),
  27.         //but for this example I do believe it proves helpful as the goal is to quickly remove all duplicate data(more than just the header if possible)
  28.         private readonly PositonUnique[] _JPEGHeader = new PositonUnique[] { new PositonUnique { Data = 255, Position = 0 }, new PositonUnique { Data = 216, Position = 1 }, new PositonUnique { Data = 255, Position = 2 }, new PositonUnique { Data = 224, Position = 3 }, new PositonUnique { Data = 0, Position = 4 }, new PositonUnique { Data = 16, Position = 5 }, new PositonUnique { Data = 74, Position = 6 }, new PositonUnique { Data = 70, Position = 7 }, new PositonUnique { Data = 73, Position = 8 }, new PositonUnique { Data = 70, Position = 9 }, new PositonUnique { Data = 0, Position = 10 }, new PositonUnique { Data = 1, Position = 11 }, new PositonUnique { Data = 1, Position = 12 }, new PositonUnique { Data = 1, Position = 13 }, new PositonUnique { Data = 0, Position = 14 }, new PositonUnique { Data = 96, Position = 15 }, new PositonUnique { Data = 0, Position = 16 }, new PositonUnique { Data = 96, Position = 17 }, new PositonUnique { Data = 0, Position = 18 }, new PositonUnique { Data = 0, Position = 19 }, new PositonUnique { Data = 255, Position = 20 }, new PositonUnique { Data = 219, Position = 21 }, new PositonUnique { Data = 0, Position = 22 }, new PositonUnique { Data = 67, Position = 23 }, new PositonUnique { Data = 0, Position = 24 }, new PositonUnique { Data = 255, Position = 25 }, new PositonUnique { Data = 255, Position = 26 }, new PositonUnique { Data = 255, Position = 27 }, new PositonUnique { Data = 255, Position = 28 }, new PositonUnique { Data = 255, Position = 29 }, new PositonUnique { Data = 255, Position = 30 }, new PositonUnique { Data = 255, Position = 31 }, new PositonUnique { Data = 255, Position = 32 }, new PositonUnique { Data = 255, Position = 33 }, new PositonUnique { Data = 255, Position = 34 }, new PositonUnique { Data = 255, Position = 35 }, new PositonUnique { Data = 255, Position = 36 }, new PositonUnique { Data = 255, Position = 37 }, new PositonUnique { Data = 255, Position = 38 }, new PositonUnique { Data = 255, Position = 39 }, new PositonUnique { Data = 255, Position = 40 }, new PositonUnique { Data = 255, Position = 41 }, new PositonUnique { Data = 255, Position = 42 }, new PositonUnique { Data = 255, Position = 43 }, new PositonUnique { Data = 255, Position = 44 }, new PositonUnique { Data = 255, Position = 45 }, new PositonUnique { Data = 255, Position = 46 }, new PositonUnique { Data = 255, Position = 47 }, new PositonUnique { Data = 255, Position = 48 }, new PositonUnique { Data = 255, Position = 49 }, new PositonUnique { Data = 255, Position = 50 }, new PositonUnique { Data = 255, Position = 51 }, new PositonUnique { Data = 255, Position = 52 }, new PositonUnique { Data = 255, Position = 53 }, new PositonUnique { Data = 255, Position = 54 }, new PositonUnique { Data = 255, Position = 55 }, new PositonUnique { Data = 255, Position = 56 }, new PositonUnique { Data = 255, Position = 57 }, new PositonUnique { Data = 255, Position = 58 }, new PositonUnique { Data = 255, Position = 59 }, new PositonUnique { Data = 255, Position = 60 }, new PositonUnique { Data = 255, Position = 61 }, new PositonUnique { Data = 255, Position = 62 }, new PositonUnique { Data = 255, Position = 63 }, new PositonUnique { Data = 255, Position = 64 }, new PositonUnique { Data = 255, Position = 65 }, new PositonUnique { Data = 255, Position = 66 }, new PositonUnique { Data = 255, Position = 67 }, new PositonUnique { Data = 255, Position = 68 }, new PositonUnique { Data = 255, Position = 69 }, new PositonUnique { Data = 255, Position = 70 }, new PositonUnique { Data = 255, Position = 71 }, new PositonUnique { Data = 255, Position = 72 }, new PositonUnique { Data = 255, Position = 73 }, new PositonUnique { Data = 255, Position = 74 }, new PositonUnique { Data = 255, Position = 75 }, new PositonUnique { Data = 255, Position = 76 }, new PositonUnique { Data = 255, Position = 77 }, new PositonUnique { Data = 255, Position = 78 }, new PositonUnique { Data = 255, Position = 79 }, new PositonUnique { Data = 255, Position = 80 }, new PositonUnique { Data = 255, Position = 81 }, new PositonUnique { Data = 255, Position = 82 }, new PositonUnique { Data = 255, Position = 83 }, new PositonUnique { Data = 255, Position = 84 }, new PositonUnique { Data = 255, Position = 85 }, new PositonUnique { Data = 255, Position = 86 }, new PositonUnique { Data = 255, Position = 87 }, new PositonUnique { Data = 255, Position = 88 }, new PositonUnique { Data = 255, Position = 89 }, new PositonUnique { Data = 219, Position = 90 }, new PositonUnique { Data = 0, Position = 91 }, new PositonUnique { Data = 67, Position = 92 }, new PositonUnique { Data = 1, Position = 93 }, new PositonUnique { Data = 255, Position = 94 }, new PositonUnique { Data = 255, Position = 95 }, new PositonUnique { Data = 255, Position = 96 }, new PositonUnique { Data = 255, Position = 97 }, new PositonUnique { Data = 255, Position = 98 }, new PositonUnique { Data = 255, Position = 99 }, new PositonUnique { Data = 255, Position = 100 }, new PositonUnique { Data = 255, Position = 101 }, new PositonUnique { Data = 255, Position = 102 }, new PositonUnique { Data = 255, Position = 103 }, new PositonUnique { Data = 255, Position = 104 }, new PositonUnique { Data = 255, Position = 105 }, new PositonUnique { Data = 255, Position = 106 }, new PositonUnique { Data = 255, Position = 107 }, new PositonUnique { Data = 255, Position = 108 }, new PositonUnique { Data = 255, Position = 109 }, new PositonUnique { Data = 255, Position = 110 }, new PositonUnique { Data = 255, Position = 111 }, new PositonUnique { Data = 255, Position = 112 }, new PositonUnique { Data = 255, Position = 113 }, new PositonUnique { Data = 255, Position = 114 }, new PositonUnique { Data = 255, Position = 115 }, new PositonUnique { Data = 255, Position = 116 }, new PositonUnique { Data = 255, Position = 117 }, new PositonUnique { Data = 255, Position = 118 }, new PositonUnique { Data = 255, Position = 119 }, new PositonUnique { Data = 255, Position = 120 }, new PositonUnique { Data = 255, Position = 121 }, new PositonUnique { Data = 255, Position = 122 }, new PositonUnique { Data = 255, Position = 123 }, new PositonUnique { Data = 255, Position = 124 }, new PositonUnique { Data = 255, Position = 125 }, new PositonUnique { Data = 255, Position = 126 }, new PositonUnique { Data = 255, Position = 127 }, new PositonUnique { Data = 255, Position = 128 }, new PositonUnique { Data = 255, Position = 129 }, new PositonUnique { Data = 255, Position = 130 }, new PositonUnique { Data = 255, Position = 131 }, new PositonUnique { Data = 255, Position = 132 }, new PositonUnique { Data = 255, Position = 133 }, new PositonUnique { Data = 255, Position = 134 }, new PositonUnique { Data = 255, Position = 135 }, new PositonUnique { Data = 255, Position = 136 }, new PositonUnique { Data = 255, Position = 137 }, new PositonUnique { Data = 255, Position = 138 }, new PositonUnique { Data = 255, Position = 139 }, new PositonUnique { Data = 255, Position = 140 }, new PositonUnique { Data = 255, Position = 141 }, new PositonUnique { Data = 255, Position = 142 }, new PositonUnique { Data = 255, Position = 143 }, new PositonUnique { Data = 255, Position = 144 }, new PositonUnique { Data = 255, Position = 145 }, new PositonUnique { Data = 255, Position = 146 }, new PositonUnique { Data = 255, Position = 147 }, new PositonUnique { Data = 255, Position = 148 }, new PositonUnique { Data = 255, Position = 149 }, new PositonUnique { Data = 255, Position = 150 }, new PositonUnique { Data = 255, Position = 151 }, new PositonUnique { Data = 255, Position = 152 }, new PositonUnique { Data = 255, Position = 153 }, new PositonUnique { Data = 255, Position = 154 }, new PositonUnique { Data = 255, Position = 155 }, new PositonUnique { Data = 255, Position = 156 }, new PositonUnique { Data = 255, Position = 157 }, new PositonUnique { Data = 255, Position = 158 }, new PositonUnique { Data = 192, Position = 159 }, new PositonUnique { Data = 0, Position = 160 }, new PositonUnique { Data = 17, Position = 161 }, new PositonUnique { Data = 8, Position = 162 }, new PositonUnique { Data = 0, Position = 163 }, new PositonUnique { Data = 0, Position = 165 }, new PositonUnique { Data = 3, Position = 167 }, new PositonUnique { Data = 1, Position = 168 }, new PositonUnique { Data = 34, Position = 169 }, new PositonUnique { Data = 0, Position = 170 }, new PositonUnique { Data = 2, Position = 171 }, new PositonUnique { Data = 17, Position = 172 }, new PositonUnique { Data = 1, Position = 173 }, new PositonUnique { Data = 3, Position = 174 }, new PositonUnique { Data = 17, Position = 175 }, new PositonUnique { Data = 1, Position = 176 }, new PositonUnique { Data = 255, Position = 177 }, new PositonUnique { Data = 196, Position = 178 }, new PositonUnique { Data = 0, Position = 179 }, new PositonUnique { Data = 31, Position = 180 }, new PositonUnique { Data = 0, Position = 181 }, new PositonUnique { Data = 0, Position = 182 }, new PositonUnique { Data = 1, Position = 183 }, new PositonUnique { Data = 5, Position = 184 }, new PositonUnique { Data = 1, Position = 185 }, new PositonUnique { Data = 1, Position = 186 }, new PositonUnique { Data = 1, Position = 187 }, new PositonUnique { Data = 1, Position = 188 }, new PositonUnique { Data = 1, Position = 189 }, new PositonUnique { Data = 1, Position = 190 }, new PositonUnique { Data = 0, Position = 191 }, new PositonUnique { Data = 0, Position = 192 }, new PositonUnique { Data = 0, Position = 193 }, new PositonUnique { Data = 0, Position = 194 }, new PositonUnique { Data = 0, Position = 195 }, new PositonUnique { Data = 0, Position = 196 }, new PositonUnique { Data = 0, Position = 197 }, new PositonUnique { Data = 0, Position = 198 }, new PositonUnique { Data = 1, Position = 199 }, new PositonUnique { Data = 2, Position = 200 }, new PositonUnique { Data = 3, Position = 201 }, new PositonUnique { Data = 4, Position = 202 }, new PositonUnique { Data = 5, Position = 203 }, new PositonUnique { Data = 6, Position = 204 }, new PositonUnique { Data = 7, Position = 205 }, new PositonUnique { Data = 8, Position = 206 }, new PositonUnique { Data = 9, Position = 207 }, new PositonUnique { Data = 10, Position = 208 }, new PositonUnique { Data = 11, Position = 209 }, new PositonUnique { Data = 255, Position = 210 }, new PositonUnique { Data = 196, Position = 211 }, new PositonUnique { Data = 0, Position = 212 }, new PositonUnique { Data = 181, Position = 213 }, new PositonUnique { Data = 16, Position = 214 }, new PositonUnique { Data = 0, Position = 215 }, new PositonUnique { Data = 2, Position = 216 }, new PositonUnique { Data = 1, Position = 217 }, new PositonUnique { Data = 3, Position = 218 }, new PositonUnique { Data = 3, Position = 219 }, new PositonUnique { Data = 2, Position = 220 }, new PositonUnique { Data = 4, Position = 221 }, new PositonUnique { Data = 3, Position = 222 }, new PositonUnique { Data = 5, Position = 223 }, new PositonUnique { Data = 5, Position = 224 }, new PositonUnique { Data = 4, Position = 225 }, new PositonUnique { Data = 4, Position = 226 }, new PositonUnique { Data = 0, Position = 227 }, new PositonUnique { Data = 0, Position = 228 }, new PositonUnique { Data = 1, Position = 229 }, new PositonUnique { Data = 125, Position = 230 }, new PositonUnique { Data = 1, Position = 231 }, new PositonUnique { Data = 2, Position = 232 }, new PositonUnique { Data = 3, Position = 233 }, new PositonUnique { Data = 0, Position = 234 }, new PositonUnique { Data = 4, Position = 235 }, new PositonUnique { Data = 17, Position = 236 }, new PositonUnique { Data = 5, Position = 237 }, new PositonUnique { Data = 18, Position = 238 }, new PositonUnique { Data = 33, Position = 239 }, new PositonUnique { Data = 49, Position = 240 }, new PositonUnique { Data = 65, Position = 241 }, new PositonUnique { Data = 6, Position = 242 }, new PositonUnique { Data = 19, Position = 243 }, new PositonUnique { Data = 81, Position = 244 }, new PositonUnique { Data = 97, Position = 245 }, new PositonUnique { Data = 7, Position = 246 }, new PositonUnique { Data = 34, Position = 247 }, new PositonUnique { Data = 113, Position = 248 }, new PositonUnique { Data = 20, Position = 249 }, new PositonUnique { Data = 50, Position = 250 }, new PositonUnique { Data = 129, Position = 251 }, new PositonUnique { Data = 145, Position = 252 }, new PositonUnique { Data = 161, Position = 253 }, new PositonUnique { Data = 8, Position = 254 }, new PositonUnique { Data = 35, Position = 255 }, new PositonUnique { Data = 66, Position = 256 }, new PositonUnique { Data = 177, Position = 257 }, new PositonUnique { Data = 193, Position = 258 }, new PositonUnique { Data = 21, Position = 259 }, new PositonUnique { Data = 82, Position = 260 }, new PositonUnique { Data = 209, Position = 261 }, new PositonUnique { Data = 240, Position = 262 }, new PositonUnique { Data = 36, Position = 263 }, new PositonUnique { Data = 51, Position = 264 }, new PositonUnique { Data = 98, Position = 265 }, new PositonUnique { Data = 114, Position = 266 }, new PositonUnique { Data = 130, Position = 267 }, new PositonUnique { Data = 9, Position = 268 }, new PositonUnique { Data = 10, Position = 269 }, new PositonUnique { Data = 22, Position = 270 }, new PositonUnique { Data = 23, Position = 271 }, new PositonUnique { Data = 24, Position = 272 }, new PositonUnique { Data = 25, Position = 273 }, new PositonUnique { Data = 26, Position = 274 }, new PositonUnique { Data = 37, Position = 275 }, new PositonUnique { Data = 38, Position = 276 }, new PositonUnique { Data = 39, Position = 277 }, new PositonUnique { Data = 40, Position = 278 }, new PositonUnique { Data = 41, Position = 279 }, new PositonUnique { Data = 42, Position = 280 }, new PositonUnique { Data = 52, Position = 281 }, new PositonUnique { Data = 53, Position = 282 }, new PositonUnique { Data = 54, Position = 283 }, new PositonUnique { Data = 55, Position = 284 }, new PositonUnique { Data = 56, Position = 285 }, new PositonUnique { Data = 57, Position = 286 }, new PositonUnique { Data = 58, Position = 287 }, new PositonUnique { Data = 67, Position = 288 }, new PositonUnique { Data = 68, Position = 289 }, new PositonUnique { Data = 69, Position = 290 }, new PositonUnique { Data = 70, Position = 291 }, new PositonUnique { Data = 71, Position = 292 }, new PositonUnique { Data = 72, Position = 293 }, new PositonUnique { Data = 73, Position = 294 }, new PositonUnique { Data = 74, Position = 295 }, new PositonUnique { Data = 83, Position = 296 }, new PositonUnique { Data = 84, Position = 297 }, new PositonUnique { Data = 85, Position = 298 }, new PositonUnique { Data = 86, Position = 299 }, new PositonUnique { Data = 87, Position = 300 }, new PositonUnique { Data = 88, Position = 301 }, new PositonUnique { Data = 89, Position = 302 }, new PositonUnique { Data = 90, Position = 303 }, new PositonUnique { Data = 99, Position = 304 }, new PositonUnique { Data = 100, Position = 305 }, new PositonUnique { Data = 101, Position = 306 }, new PositonUnique { Data = 102, Position = 307 }, new PositonUnique { Data = 103, Position = 308 }, new PositonUnique { Data = 104, Position = 309 }, new PositonUnique { Data = 105, Position = 310 }, new PositonUnique { Data = 106, Position = 311 }, new PositonUnique { Data = 115, Position = 312 }, new PositonUnique { Data = 116, Position = 313 }, new PositonUnique { Data = 117, Position = 314 }, new PositonUnique { Data = 118, Position = 315 }, new PositonUnique { Data = 119, Position = 316 }, new PositonUnique { Data = 120, Position = 317 }, new PositonUnique { Data = 121, Position = 318 }, new PositonUnique { Data = 122, Position = 319 }, new PositonUnique { Data = 131, Position = 320 }, new PositonUnique { Data = 132, Position = 321 }, new PositonUnique { Data = 133, Position = 322 }, new PositonUnique { Data = 134, Position = 323 }, new PositonUnique { Data = 135, Position = 324 }, new PositonUnique { Data = 136, Position = 325 }, new PositonUnique { Data = 137, Position = 326 }, new PositonUnique { Data = 138, Position = 327 }, new PositonUnique { Data = 146, Position = 328 }, new PositonUnique { Data = 147, Position = 329 }, new PositonUnique { Data = 148, Position = 330 }, new PositonUnique { Data = 149, Position = 331 }, new PositonUnique { Data = 150, Position = 332 }, new PositonUnique { Data = 151, Position = 333 }, new PositonUnique { Data = 152, Position = 334 }, new PositonUnique { Data = 153, Position = 335 }, new PositonUnique { Data = 154, Position = 336 }, new PositonUnique { Data = 162, Position = 337 }, new PositonUnique { Data = 163, Position = 338 }, new PositonUnique { Data = 164, Position = 339 }, new PositonUnique { Data = 165, Position = 340 }, new PositonUnique { Data = 166, Position = 341 }, new PositonUnique { Data = 167, Position = 342 }, new PositonUnique { Data = 168, Position = 343 }, new PositonUnique { Data = 169, Position = 344 }, new PositonUnique { Data = 170, Position = 345 }, new PositonUnique { Data = 178, Position = 346 }, new PositonUnique { Data = 179, Position = 347 }, new PositonUnique { Data = 180, Position = 348 }, new PositonUnique { Data = 181, Position = 349 }, new PositonUnique { Data = 182, Position = 350 }, new PositonUnique { Data = 183, Position = 351 }, new PositonUnique { Data = 184, Position = 352 }, new PositonUnique { Data = 185, Position = 353 }, new PositonUnique { Data = 186, Position = 354 }, new PositonUnique { Data = 194, Position = 355 }, new PositonUnique { Data = 195, Position = 356 }, new PositonUnique { Data = 196, Position = 357 }, new PositonUnique { Data = 197, Position = 358 }, new PositonUnique { Data = 198, Position = 359 }, new PositonUnique { Data = 199, Position = 360 }, new PositonUnique { Data = 200, Position = 361 }, new PositonUnique { Data = 201, Position = 362 }, new PositonUnique { Data = 202, Position = 363 }, new PositonUnique { Data = 210, Position = 364 }, new PositonUnique { Data = 211, Position = 365 }, new PositonUnique { Data = 212, Position = 366 }, new PositonUnique { Data = 213, Position = 367 }, new PositonUnique { Data = 214, Position = 368 }, new PositonUnique { Data = 215, Position = 369 }, new PositonUnique { Data = 216, Position = 370 }, new PositonUnique { Data = 217, Position = 371 }, new PositonUnique { Data = 218, Position = 372 }, new PositonUnique { Data = 225, Position = 373 }, new PositonUnique { Data = 226, Position = 374 }, new PositonUnique { Data = 227, Position = 375 }, new PositonUnique { Data = 228, Position = 376 }, new PositonUnique { Data = 229, Position = 377 }, new PositonUnique { Data = 230, Position = 378 }, new PositonUnique { Data = 231, Position = 379 }, new PositonUnique { Data = 232, Position = 380 }, new PositonUnique { Data = 233, Position = 381 }, new PositonUnique { Data = 234, Position = 382 }, new PositonUnique { Data = 241, Position = 383 }, new PositonUnique { Data = 242, Position = 384 }, new PositonUnique { Data = 243, Position = 385 }, new PositonUnique { Data = 244, Position = 386 }, new PositonUnique { Data = 245, Position = 387 }, new PositonUnique { Data = 246, Position = 388 }, new PositonUnique { Data = 247, Position = 389 }, new PositonUnique { Data = 248, Position = 390 }, new PositonUnique { Data = 249, Position = 391 }, new PositonUnique { Data = 250, Position = 392 }, new PositonUnique { Data = 255, Position = 393 }, new PositonUnique { Data = 196, Position = 394 }, new PositonUnique { Data = 0, Position = 395 }, new PositonUnique { Data = 31, Position = 396 }, new PositonUnique { Data = 1, Position = 397 }, new PositonUnique { Data = 0, Position = 398 }, new PositonUnique { Data = 3, Position = 399 }, new PositonUnique { Data = 1, Position = 400 }, new PositonUnique { Data = 1, Position = 401 }, new PositonUnique { Data = 1, Position = 402 }, new PositonUnique { Data = 1, Position = 403 }, new PositonUnique { Data = 1, Position = 404 }, new PositonUnique { Data = 1, Position = 405 }, new PositonUnique { Data = 1, Position = 406 }, new PositonUnique { Data = 1, Position = 407 }, new PositonUnique { Data = 1, Position = 408 }, new PositonUnique { Data = 0, Position = 409 }, new PositonUnique { Data = 0, Position = 410 }, new PositonUnique { Data = 0, Position = 411 }, new PositonUnique { Data = 0, Position = 412 }, new PositonUnique { Data = 0, Position = 413 }, new PositonUnique { Data = 0, Position = 414 }, new PositonUnique { Data = 1, Position = 415 }, new PositonUnique { Data = 2, Position = 416 }, new PositonUnique { Data = 3, Position = 417 }, new PositonUnique { Data = 4, Position = 418 }, new PositonUnique { Data = 5, Position = 419 }, new PositonUnique { Data = 6, Position = 420 }, new PositonUnique { Data = 7, Position = 421 }, new PositonUnique { Data = 8, Position = 422 }, new PositonUnique { Data = 9, Position = 423 }, new PositonUnique { Data = 10, Position = 424 }, new PositonUnique { Data = 11, Position = 425 }, new PositonUnique { Data = 255, Position = 426 }, new PositonUnique { Data = 196, Position = 427 }, new PositonUnique { Data = 0, Position = 428 }, new PositonUnique { Data = 181, Position = 429 }, new PositonUnique { Data = 17, Position = 430 }, new PositonUnique { Data = 0, Position = 431 }, new PositonUnique { Data = 2, Position = 432 }, new PositonUnique { Data = 1, Position = 433 }, new PositonUnique { Data = 2, Position = 434 }, new PositonUnique { Data = 4, Position = 435 }, new PositonUnique { Data = 4, Position = 436 }, new PositonUnique { Data = 3, Position = 437 }, new PositonUnique { Data = 4, Position = 438 }, new PositonUnique { Data = 7, Position = 439 }, new PositonUnique { Data = 5, Position = 440 }, new PositonUnique { Data = 4, Position = 441 }, new PositonUnique { Data = 4, Position = 442 }, new PositonUnique { Data = 0, Position = 443 }, new PositonUnique { Data = 1, Position = 444 }, new PositonUnique { Data = 2, Position = 445 }, new PositonUnique { Data = 119, Position = 446 }, new PositonUnique { Data = 0, Position = 447 }, new PositonUnique { Data = 1, Position = 448 }, new PositonUnique { Data = 2, Position = 449 }, new PositonUnique { Data = 3, Position = 450 }, new PositonUnique { Data = 17, Position = 451 }, new PositonUnique { Data = 4, Position = 452 }, new PositonUnique { Data = 5, Position = 453 }, new PositonUnique { Data = 33, Position = 454 }, new PositonUnique { Data = 49, Position = 455 }, new PositonUnique { Data = 6, Position = 456 }, new PositonUnique { Data = 18, Position = 457 }, new PositonUnique { Data = 65, Position = 458 }, new PositonUnique { Data = 81, Position = 459 }, new PositonUnique { Data = 7, Position = 460 }, new PositonUnique { Data = 97, Position = 461 }, new PositonUnique { Data = 113, Position = 462 }, new PositonUnique { Data = 19, Position = 463 }, new PositonUnique { Data = 34, Position = 464 }, new PositonUnique { Data = 50, Position = 465 }, new PositonUnique { Data = 129, Position = 466 }, new PositonUnique { Data = 8, Position = 467 }, new PositonUnique { Data = 20, Position = 468 }, new PositonUnique { Data = 66, Position = 469 }, new PositonUnique { Data = 145, Position = 470 }, new PositonUnique { Data = 161, Position = 471 }, new PositonUnique { Data = 177, Position = 472 }, new PositonUnique { Data = 193, Position = 473 }, new PositonUnique { Data = 9, Position = 474 }, new PositonUnique { Data = 35, Position = 475 }, new PositonUnique { Data = 51, Position = 476 }, new PositonUnique { Data = 82, Position = 477 }, new PositonUnique { Data = 240, Position = 478 }, new PositonUnique { Data = 21, Position = 479 }, new PositonUnique { Data = 98, Position = 480 }, new PositonUnique { Data = 114, Position = 481 }, new PositonUnique { Data = 209, Position = 482 }, new PositonUnique { Data = 10, Position = 483 }, new PositonUnique { Data = 22, Position = 484 }, new PositonUnique { Data = 36, Position = 485 }, new PositonUnique { Data = 52, Position = 486 }, new PositonUnique { Data = 225, Position = 487 }, new PositonUnique { Data = 37, Position = 488 }, new PositonUnique { Data = 241, Position = 489 }, new PositonUnique { Data = 23, Position = 490 }, new PositonUnique { Data = 24, Position = 491 }, new PositonUnique { Data = 25, Position = 492 }, new PositonUnique { Data = 26, Position = 493 }, new PositonUnique { Data = 38, Position = 494 }, new PositonUnique { Data = 39, Position = 495 }, new PositonUnique { Data = 40, Position = 496 }, new PositonUnique { Data = 41, Position = 497 }, new PositonUnique { Data = 42, Position = 498 }, new PositonUnique { Data = 53, Position = 499 }, new PositonUnique { Data = 54, Position = 500 }, new PositonUnique { Data = 55, Position = 501 }, new PositonUnique { Data = 56, Position = 502 }, new PositonUnique { Data = 57, Position = 503 }, new PositonUnique { Data = 58, Position = 504 }, new PositonUnique { Data = 67, Position = 505 }, new PositonUnique { Data = 68, Position = 506 }, new PositonUnique { Data = 69, Position = 507 }, new PositonUnique { Data = 70, Position = 508 }, new PositonUnique { Data = 71, Position = 509 }, new PositonUnique { Data = 72, Position = 510 }, new PositonUnique { Data = 73, Position = 511 }, new PositonUnique { Data = 74, Position = 512 }, new PositonUnique { Data = 83, Position = 513 }, new PositonUnique { Data = 84, Position = 514 }, new PositonUnique { Data = 85, Position = 515 }, new PositonUnique { Data = 86, Position = 516 }, new PositonUnique { Data = 87, Position = 517 }, new PositonUnique { Data = 88, Position = 518 }, new PositonUnique { Data = 89, Position = 519 }, new PositonUnique { Data = 90, Position = 520 }, new PositonUnique { Data = 99, Position = 521 }, new PositonUnique { Data = 100, Position = 522 }, new PositonUnique { Data = 101, Position = 523 }, new PositonUnique { Data = 102, Position = 524 }, new PositonUnique { Data = 103, Position = 525 }, new PositonUnique { Data = 104, Position = 526 }, new PositonUnique { Data = 105, Position = 527 }, new PositonUnique { Data = 106, Position = 528 }, new PositonUnique { Data = 115, Position = 529 }, new PositonUnique { Data = 116, Position = 530 }, new PositonUnique { Data = 117, Position = 531 }, new PositonUnique { Data = 118, Position = 532 }, new PositonUnique { Data = 119, Position = 533 }, new PositonUnique { Data = 120, Position = 534 }, new PositonUnique { Data = 121, Position = 535 }, new PositonUnique { Data = 122, Position = 536 }, new PositonUnique { Data = 130, Position = 537 }, new PositonUnique { Data = 131, Position = 538 }, new PositonUnique { Data = 132, Position = 539 }, new PositonUnique { Data = 133, Position = 540 }, new PositonUnique { Data = 134, Position = 541 }, new PositonUnique { Data = 135, Position = 542 }, new PositonUnique { Data = 136, Position = 543 }, new PositonUnique { Data = 137, Position = 544 }, new PositonUnique { Data = 138, Position = 545 }, new PositonUnique { Data = 146, Position = 546 }, new PositonUnique { Data = 147, Position = 547 }, new PositonUnique { Data = 148, Position = 548 }, new PositonUnique { Data = 149, Position = 549 }, new PositonUnique { Data = 150, Position = 550 }, new PositonUnique { Data = 151, Position = 551 }, new PositonUnique { Data = 152, Position = 552 }, new PositonUnique { Data = 153, Position = 553 }, new PositonUnique { Data = 154, Position = 554 }, new PositonUnique { Data = 162, Position = 555 }, new PositonUnique { Data = 163, Position = 556 }, new PositonUnique { Data = 164, Position = 557 }, new PositonUnique { Data = 165, Position = 558 }, new PositonUnique { Data = 166, Position = 559 }, new PositonUnique { Data = 167, Position = 560 }, new PositonUnique { Data = 168, Position = 561 }, new PositonUnique { Data = 169, Position = 562 }, new PositonUnique { Data = 170, Position = 563 }, new PositonUnique { Data = 178, Position = 564 }, new PositonUnique { Data = 179, Position = 565 }, new PositonUnique { Data = 180, Position = 566 }, new PositonUnique { Data = 181, Position = 567 }, new PositonUnique { Data = 182, Position = 568 }, new PositonUnique { Data = 183, Position = 569 }, new PositonUnique { Data = 184, Position = 570 }, new PositonUnique { Data = 185, Position = 571 }, new PositonUnique { Data = 186, Position = 572 }, new PositonUnique { Data = 194, Position = 573 }, new PositonUnique { Data = 195, Position = 574 }, new PositonUnique { Data = 196, Position = 575 }, new PositonUnique { Data = 197, Position = 576 }, new PositonUnique { Data = 198, Position = 577 }, new PositonUnique { Data = 199, Position = 578 }, new PositonUnique { Data = 200, Position = 579 }, new PositonUnique { Data = 201, Position = 580 }, new PositonUnique { Data = 202, Position = 581 }, new PositonUnique { Data = 210, Position = 582 }, new PositonUnique { Data = 211, Position = 583 }, new PositonUnique { Data = 212, Position = 584 }, new PositonUnique { Data = 213, Position = 585 }, new PositonUnique { Data = 214, Position = 586 }, new PositonUnique { Data = 215, Position = 587 }, new PositonUnique { Data = 216, Position = 588 }, new PositonUnique { Data = 217, Position = 589 }, new PositonUnique { Data = 218, Position = 590 }, new PositonUnique { Data = 226, Position = 591 }, new PositonUnique { Data = 227, Position = 592 }, new PositonUnique { Data = 228, Position = 593 }, new PositonUnique { Data = 229, Position = 594 }, new PositonUnique { Data = 230, Position = 595 }, new PositonUnique { Data = 231, Position = 596 }, new PositonUnique { Data = 232, Position = 597 }, new PositonUnique { Data = 233, Position = 598 }, new PositonUnique { Data = 234, Position = 599 }, new PositonUnique { Data = 242, Position = 600 }, new PositonUnique { Data = 243, Position = 601 }, new PositonUnique { Data = 244, Position = 602 }, new PositonUnique { Data = 245, Position = 603 }, new PositonUnique { Data = 246, Position = 604 }, new PositonUnique { Data = 247, Position = 605 }, new PositonUnique { Data = 248, Position = 606 }, new PositonUnique { Data = 249, Position = 607 }, new PositonUnique { Data = 250, Position = 608 }, new PositonUnique { Data = 255, Position = 609 }, new PositonUnique { Data = 218, Position = 610 }, new PositonUnique { Data = 0, Position = 611 }, new PositonUnique { Data = 12, Position = 612 }, new PositonUnique { Data = 3, Position = 613 }, new PositonUnique { Data = 1, Position = 614 }, new PositonUnique { Data = 0, Position = 615 }, new PositonUnique { Data = 2, Position = 616 }, new PositonUnique { Data = 17, Position = 617 }, new PositonUnique { Data = 3, Position = 618 }, new PositonUnique { Data = 17, Position = 619 }, new PositonUnique { Data = 0, Position = 620 }, new PositonUnique { Data = 63, Position = 621 }, new PositonUnique { Data = 0, Position = 622 } };
  29.         private const int _JPEGHeaderHeightPositon = 164;
  30.         private const int _JPEGHeaderWidthPositon = 166;
  31.  
  32.         public TweetImager()
  33.         {
  34.             InitializeComponent();
  35.         }
  36.  
  37.         private void LoadImageButton_Click(object sender, EventArgs e)
  38.         {
  39.             ImageOpenFileDialog.ShowDialog();
  40.         }
  41.  
  42.         private void ImageOpenFileDialog_FileOk(object sender, CancelEventArgs e)
  43.         {
  44.             int resizeSize = 75;
  45.             using (Bitmap originalImage = new Bitmap(ImageOpenFileDialog.FileName))
  46.             using (MemoryStream ms = new MemoryStream())
  47.                 do
  48.                 {
  49.                     //Resize the image
  50.                     Image resized = (originalImage.Height > originalImage.Width) ?
  51.                         resized = resizeImage(originalImage, new Size(resizeSize, originalImage.Height / (originalImage.Width / resizeSize))) :
  52.                         resizeImage(originalImage, new Size((originalImage.Width / (originalImage.Height / resizeSize)), resizeSize));
  53.  
  54.                     //Use JPEG Max Compression                    
  55.                     EncoderParameters myEncoderParameters = new EncoderParameters(1);
  56.                     myEncoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 0L);
  57.  
  58.  
  59.                     //Clear the memorystream
  60.                     ms.SetLength(0);
  61.  
  62.                     //Save the file to a temp memory stream
  63.                     resized.Save(ms, GetEncoder(ImageFormat.Jpeg), myEncoderParameters);
  64.                     resized.Dispose();
  65.  
  66.                     //Get the ascii text from the image
  67.                     TweetRichTextBox.Text = EncodeBytesToAscii(
  68.                         GetUniqueList(ms.ToArray())
  69.                         .Where(a => !_JPEGHeader.Any(b => b.Position == a.Position)).Select(a => a.Data).ToArray());
  70.  
  71.                     //(decriment the resize counter each round to reduce the image further with each loop)                        
  72.                     resizeSize = resizeSize - 1;
  73.                 }
  74.                 //Reduce the image size until the output text is less than or equal to 140 characters(including height and width parameters)
  75.                 while (TweetRichTextBox.Text.ToCharArray().Length > 140);
  76.         }
  77.  
  78.         private void TranslateButton_Click(object sender, EventArgs e)
  79.         {
  80.             //TODO Will blow up if no file is selected, this all needs to be made better
  81.             var newFileName = ImageOpenFileDialog.FileName.Split('.')[0] + "1.png";
  82.  
  83.             //Now get the image from the ascii text
  84.             var data = DeencodeAsciiToBytes(TweetRichTextBox.Text).ToArray();
  85.             var height = new PositonUnique { Data = data[0], Position = _JPEGHeaderHeightPositon };
  86.             var width = new PositonUnique { Data = data[1], Position = _JPEGHeaderWidthPositon };
  87.             var file = _JPEGHeader.Concat(new PositonUnique[] { height, width }).OrderBy(a => a.Position).Select(a => a.Data).Concat(data.Skip(2)).ToArray();
  88.  
  89.             //Blow up the picture so its easier to see                
  90.             var enlaregedResult = new Bitmap(new Bitmap(new MemoryStream(file)), new Size { Height = height.Data * 4, Width = width.Data * 4 });
  91.             TweetImagePictureBox.Image = enlaregedResult;
  92.             if (SaveCheckBox.Checked) enlaregedResult.Save(newFileName, ImageFormat.Png);
  93.         }
  94.  
  95.  
  96.         //File Helper Functions
  97.         private IEnumerable<byte> FileToByteArray(string fileName)
  98.         {
  99.             byte[] result;
  100.             using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
  101.             using (BinaryReader br = new BinaryReader(fs))
  102.                 result = br.ReadBytes((int)new FileInfo(fileName).Length).ToArray();
  103.             return result;
  104.         }
  105.  
  106.  
  107.         //Image Helper
  108.         private ImageCodecInfo GetEncoder(ImageFormat format)
  109.         {
  110.             foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageDecoders())
  111.                 if (codec.FormatID == format.Guid)
  112.                     return codec;
  113.             return null;
  114.         }
  115.  
  116.         private static Image resizeImage(Image imgToResize, Size size)
  117.         {
  118.             return (Image)(new Bitmap(imgToResize, size));
  119.         }
  120.  
  121.  
  122.         //JPEG Header Helper      
  123.         private static IEnumerable<PositonUnique> GetUniqueList(byte[] input)
  124.         {
  125.             List<PositonUnique> result = new List<PositonUnique>();
  126.             for (int i = 0; i < input.Count(); i++)
  127.                 result.Add(new PositonUnique { Data = input[i], Position = i });
  128.             return result;
  129.         }
  130.  
  131.  
  132.         //Bit/Byte/Char converters
  133.         private static byte ConvertBitArraytoByte(bool[] bits)
  134.         {
  135.             if (bits.Length > 8)
  136.                 throw new ArgumentException("No arrays larger than 8 allowed!");
  137.             int result = 0;
  138.             double multiplier = Math.Pow(2, bits.Length - 1);
  139.             foreach (var element in bits)
  140.             {
  141.                 if (element) result += (int)multiplier;
  142.                 multiplier = multiplier / 2;
  143.             }
  144.             return (byte)result;
  145.         }
  146.  
  147.         private static IEnumerable<bool> GetBits(byte b)
  148.         {
  149.             for (int i = 0; i < 8; i++)
  150.             {
  151.                 yield return (b & 0x80) != 0;
  152.                 b *= 2;
  153.             }
  154.         }
  155.  
  156.         private static string EncodeBytesToAscii(byte[] input)
  157.         {
  158.             //Not sure this is exactly the most efficient way to store data within range 0-94, should at least be close(I hope)            
  159.             List<byte> output = new List<byte>();
  160.             var parsedToBits = input.SelectMany(GetBits).ToList();
  161.  
  162.             //Numbers between 94 and 64 should be 7, ones below should be 6, if a number is over 94 take 6 automatically making it less than 64
  163.             for (int i = 0; i < parsedToBits.Count(); )
  164.             {
  165.                 if (i + 6 < parsedToBits.Count())
  166.                 {
  167.                     if (ConvertBitArraytoByte(parsedToBits.GetRange(i, 7).ToArray()) > 94 || ConvertBitArraytoByte(parsedToBits.GetRange(i, 7).ToArray()) < 64)
  168.                     {
  169.                         output.Add(ConvertBitArraytoByte(parsedToBits.GetRange(i, 6).ToArray()));
  170.                         i = i + 6;
  171.                     }
  172.                     else
  173.                     {
  174.                         output.Add(ConvertBitArraytoByte(parsedToBits.GetRange(i, 7).ToArray()));
  175.                         i = i + 7;
  176.                     }
  177.                 }
  178.                 else
  179.                 {
  180.                     output.Add(ConvertBitArraytoByte(parsedToBits.GetRange(i, parsedToBits.Count() - i).ToArray()));
  181.                     break;
  182.                 }
  183.             }
  184.  
  185.             //Add 32 to each number and convert to a character, this should output ASCII characters now
  186.             return String.Join("", output.Select(a => (char)(a + 32)));
  187.         }
  188.  
  189.         private static IEnumerable<byte> DeencodeAsciiToBytes(string input)
  190.         {
  191.             //Pretty much the opposite of "EncodeBytesToAscii", based on number size pulls all bits from the inputed charcters,
  192.             //then flattens the output and splits by 8 to convert back to base 255
  193.             var result = new List<byte>();
  194.  
  195.             var inputList = input.ToCharArray()
  196.                 .Select(a => ((int)a - 32))
  197.                 .SelectMany(a => a >= 64 ? GetBits((byte)a).Skip(1).ToArray() : GetBits((byte)a).Skip(2).ToArray())
  198.                 .ToList();
  199.  
  200.             for (int i = 0; i < inputList.Count; i = i + 8)
  201.             {
  202.                 if (inputList.Count - i < 8) result.Add(ConvertBitArraytoByte(inputList.GetRange(i, inputList.Count - i).ToArray()));
  203.                 else result.Add(ConvertBitArraytoByte(inputList.GetRange(i, 8).ToArray()));
  204.             }
  205.  
  206.             return result.ToArray();
  207.         }
  208.     }
  209. }
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216. namespace ImageCompression
  217. {
  218.     partial class TweetImager
  219.     {
  220.         /// <summary>
  221.         /// Required designer variable.
  222.         /// </summary>
  223.         private System.ComponentModel.IContainer components = null;
  224.  
  225.         /// <summary>
  226.         /// Clean up any resources being used.
  227.         /// </summary>
  228.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  229.         protected override void Dispose(bool disposing)
  230.         {
  231.             if (disposing && (components != null))
  232.             {
  233.                 components.Dispose();
  234.             }
  235.             base.Dispose(disposing);
  236.         }
  237.  
  238.         #region Windows Form Designer generated code
  239.  
  240.         /// <summary>
  241.         /// Required method for Designer support - do not modify
  242.         /// the contents of this method with the code editor.
  243.         /// </summary>
  244.         private void InitializeComponent()
  245.         {
  246.             this.LoadImageButton = new System.Windows.Forms.Button();
  247.             this.ImageOpenFileDialog = new System.Windows.Forms.OpenFileDialog();
  248.             this.TweetImagePictureBox = new System.Windows.Forms.PictureBox();
  249.             this.TweetRichTextBox = new System.Windows.Forms.RichTextBox();
  250.             this.TweetLabel = new System.Windows.Forms.Label();
  251.             this.TranslateButton = new System.Windows.Forms.Button();
  252.             this.SaveCheckBox = new System.Windows.Forms.CheckBox();
  253.             ((System.ComponentModel.ISupportInitialize)(this.TweetImagePictureBox)).BeginInit();
  254.             this.SuspendLayout();
  255.             //
  256.             // LoadImageButton
  257.             //
  258.             this.LoadImageButton.Location = new System.Drawing.Point(58, 13);
  259.             this.LoadImageButton.Name = "LoadImageButton";
  260.             this.LoadImageButton.Size = new System.Drawing.Size(75, 23);
  261.             this.LoadImageButton.TabIndex = 0;
  262.             this.LoadImageButton.Text = "LoadImage";
  263.             this.LoadImageButton.UseVisualStyleBackColor = true;
  264.             this.LoadImageButton.Click += new System.EventHandler(this.LoadImageButton_Click);
  265.             //
  266.             // ImageOpenFileDialog
  267.             //
  268.             this.ImageOpenFileDialog.FileName = "ImageOpenFileDialog";
  269.             this.ImageOpenFileDialog.FileOk += new System.ComponentModel.CancelEventHandler(this.ImageOpenFileDialog_FileOk);
  270.             //
  271.             // TweetImagePictureBox
  272.             //
  273.             this.TweetImagePictureBox.Location = new System.Drawing.Point(12, 147);
  274.             this.TweetImagePictureBox.Name = "TweetImagePictureBox";
  275.             this.TweetImagePictureBox.Size = new System.Drawing.Size(250, 250);
  276.             this.TweetImagePictureBox.TabIndex = 3;
  277.             this.TweetImagePictureBox.TabStop = false;
  278.             //
  279.             // TweetRichTextBox
  280.             //
  281.             this.TweetRichTextBox.Location = new System.Drawing.Point(12, 41);
  282.             this.TweetRichTextBox.Name = "TweetRichTextBox";
  283.             this.TweetRichTextBox.Size = new System.Drawing.Size(250, 75);
  284.             this.TweetRichTextBox.TabIndex = 4;
  285.             this.TweetRichTextBox.Text = "";
  286.             //
  287.             // TweetLabel
  288.             //
  289.             this.TweetLabel.AutoSize = true;
  290.             this.TweetLabel.Location = new System.Drawing.Point(12, 22);
  291.             this.TweetLabel.Name = "TweetLabel";
  292.             this.TweetLabel.Size = new System.Drawing.Size(40, 13);
  293.             this.TweetLabel.TabIndex = 5;
  294.             this.TweetLabel.Text = "Tweet:";
  295.             //
  296.             // TranslateButton
  297.             //
  298.             this.TranslateButton.Location = new System.Drawing.Point(12, 122);
  299.             this.TranslateButton.Name = "TranslateButton";
  300.             this.TranslateButton.Size = new System.Drawing.Size(75, 23);
  301.             this.TranslateButton.TabIndex = 6;
  302.             this.TranslateButton.Text = "Translate";
  303.             this.TranslateButton.UseVisualStyleBackColor = true;
  304.             this.TranslateButton.Click += new System.EventHandler(this.TranslateButton_Click);
  305.             //
  306.             // SaveCheckBox
  307.             //
  308.             this.SaveCheckBox.AutoSize = true;
  309.             this.SaveCheckBox.Location = new System.Drawing.Point(93, 126);
  310.             this.SaveCheckBox.Name = "SaveCheckBox";
  311.             this.SaveCheckBox.Size = new System.Drawing.Size(51, 17);
  312.             this.SaveCheckBox.TabIndex = 7;
  313.             this.SaveCheckBox.Text = "Save";
  314.             this.SaveCheckBox.UseVisualStyleBackColor = true;
  315.             //
  316.             // TweetImager
  317.             //
  318.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  319.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  320.             this.ClientSize = new System.Drawing.Size(284, 411);
  321.             this.Controls.Add(this.SaveCheckBox);
  322.             this.Controls.Add(this.TranslateButton);
  323.             this.Controls.Add(this.TweetLabel);
  324.             this.Controls.Add(this.TweetRichTextBox);
  325.             this.Controls.Add(this.TweetImagePictureBox);
  326.             this.Controls.Add(this.LoadImageButton);
  327.             this.Name = "TweetImager";
  328.             this.Text = "TweetImager";
  329.             ((System.ComponentModel.ISupportInitialize)(this.TweetImagePictureBox)).EndInit();
  330.             this.ResumeLayout(false);
  331.             this.PerformLayout();
  332.  
  333.         }
  334.  
  335.         #endregion
  336.  
  337.         private System.Windows.Forms.Button LoadImageButton;
  338.         private System.Windows.Forms.OpenFileDialog ImageOpenFileDialog;
  339.         private System.Windows.Forms.PictureBox TweetImagePictureBox;
  340.         private System.Windows.Forms.RichTextBox TweetRichTextBox;
  341.         private System.Windows.Forms.Label TweetLabel;
  342.         private System.Windows.Forms.Button TranslateButton;
  343.         private System.Windows.Forms.CheckBox SaveCheckBox;
  344.     }
  345. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top