Created
March 31, 2013 01:58
-
-
Save AlbertoMonteiro/5279163 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.IO.Compression; | |
using System.Text; | |
using System.Windows.Media.Imaging; | |
namespace ConsoleApplication2 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
const string imageFlePath = @"C:\Users\Alberto\SkyDrive\TVShow-backup.jpg"; | |
var comments = "{nome:'Alberto'}"; | |
string jpegDirectory = Path.GetDirectoryName(imageFlePath); | |
string jpegFileName = Path.GetFileNameWithoutExtension(imageFlePath); | |
BitmapDecoder decoder = null; | |
BitmapFrame bitmapFrame = null; | |
BitmapMetadata metadata = null; | |
FileInfo originalImage = new FileInfo(imageFlePath); | |
if (File.Exists(imageFlePath)) | |
{ | |
// load the jpg file with a JpegBitmapDecoder | |
using (Stream jpegStreamIn = File.Open(imageFlePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) | |
{ | |
decoder = new JpegBitmapDecoder(jpegStreamIn, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); | |
} | |
bitmapFrame = decoder.Frames[0]; | |
metadata = (BitmapMetadata)bitmapFrame.Metadata; | |
if (bitmapFrame != null) | |
{ | |
BitmapMetadata metaData = (BitmapMetadata)bitmapFrame.Metadata.Clone(); | |
if (metaData != null) | |
{ | |
// modify the metadata | |
//metaData.SetQuery("/app1/ifd/exif:{uint=40092}", comments); | |
var query = metaData.GetQuery("/app1/ifd/exif:{uint=40092}"); | |
// get an encoder to create a new jpg file with the new metadata. | |
JpegBitmapEncoder encoder = new JpegBitmapEncoder(); | |
encoder.Frames.Add(BitmapFrame.Create(bitmapFrame, bitmapFrame.Thumbnail, metaData, bitmapFrame.ColorContexts)); | |
//string jpegNewFileName = Path.Combine(jpegDirectory, "JpegTemp.jpg"); | |
// Delete the original | |
originalImage.Delete(); | |
// Save the new image | |
using (Stream jpegStreamOut = File.Open(imageFlePath, FileMode.CreateNew, FileAccess.ReadWrite)) | |
{ | |
encoder.Save(jpegStreamOut); | |
} | |
} | |
} | |
} | |
/*var str = new StreamReader(@"C:\Users\Alberto\Documents\Visual Studio 2012\Projects\ConsoleApplication2\ConsoleApplication2\TextFile1.txt").ReadToEnd(); | |
var compress = Compress(str); | |
Console.WriteLine(compress.Length); | |
Console.WriteLine(str.Length);*/ | |
} | |
public static String Compress(String decompressed) | |
{ | |
byte[] data = Encoding.UTF8.GetBytes(decompressed); | |
using (var input = new MemoryStream(data)) | |
using (var output = new MemoryStream()) | |
{ | |
using (var gzip = new GZipStream(output, CompressionLevel.Optimal)) | |
{ | |
input.CopyTo(gzip); | |
} | |
return Convert.ToBase64String(output.ToArray()); | |
} | |
} | |
public static String Decompress(String compressed) | |
{ | |
byte[] data = Convert.FromBase64String(compressed); | |
using (MemoryStream input = new MemoryStream(data)) | |
using (GZipStream gzip = new GZipStream(input, CompressionMode.Decompress)) | |
using (MemoryStream output = new MemoryStream()) | |
{ | |
gzip.CopyTo(output); | |
StringBuilder sb = new StringBuilder(); | |
return Encoding.UTF8.GetString(output.ToArray()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment