/*------------------------------------------------------------------------------ * Author - Rob (http://stackoverflow.com/users/1185/rob) * ----------------------------------------------------------------------------- * Notes * - This program provides one example of fitting an image into 140 characters, * as per the challenge on StackOverflow at the following URL: * http://stackoverflow.com/questions/891643/twitter-image-encoding-challenge * ----------------------------------------------------------------------------- * Sources / References * - http://www.bobpowell.net/grayscale.htm * - http://porg.es/blog/what-can-we-fit-in-140-characters * ---------------------------------------------------------------------------*/ using System; using System.Drawing; using System.IO; using System.Text; using System.Diagnostics; namespace Twitter_Image { public class DataPoint { public byte Luma { get; set; } public byte Count { get; set; } } class Program { #region " About Message " private const string About = @"Twitter Image - A StackOverflow Demostration Author - http://stackoverflow.com/users/1185/rob"; #endregion #region " Information Message " private const string Info = @"Usage tweetimage encode [input] [output] tweetimage decode [imput] [output] Notes - Encoded output will be text - Decoded output will follow extension if supported"; #endregion static void Main(string[] args) { string[] images = { "cornell-box", "cornell-box-large", "lena", "lena-large", "mona-lisa", "mona-lisa-large", "stackoverflow-logo"}; for (int ndx = 0; ndx < images.Length; ndx++) { Stopwatch timer = new Stopwatch(); // Encode the image Console.WriteLine(images[ndx]); timer.Start(); char[] encoded = TwitterImage.EncodeImage(new Bitmap(images[ndx] + ".png")); timer.Stop(); SaveData(encoded, images[ndx] + ".txt"); Console.WriteLine("Encoded Size: " + encoded.Length); Console.WriteLine("Runtime: " + timer.ElapsedMilliseconds.ToString() + "ms"); // Decode the image Bitmap image = TwitterImage.ReadEncodedImage(images[ndx] + ".txt"); image.Save(images[ndx] + ".bmp"); } Console.WriteLine("Press any key to continue"); Console.ReadKey(); } /// /// Save the character data provided to the file. /// private static void SaveData(char[] data, string fileName) { byte[] bytes = new byte[data.Length * sizeof(char)]; Buffer.BlockCopy(data, 0, bytes, 0, bytes.Length); FileStream file = new FileStream(fileName, FileMode.Create); StreamWriter stream = new StreamWriter(file, Encoding.Unicode); stream.Write(Encoding.Unicode.GetString(bytes)); stream.Close(); file.Close(); } } }