Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Drawing;
- /* online pastes of source code
- + http://pasted.co/bdbe18be
- + https://gist.github.com/kyrathasoft/b05c96d8b0896868c30531d01567c9d5
- +
- */
- namespace ConsoleApplication {
- class Program
- {
- static void Main(string[] args)
- {
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine("\n Convert image to Base64 string\n\n");
- Console.ForegroundColor = ConsoleColor.White;
- if(args.Length == 0){
- if(File.Exists("pic.jpg")){ File.Delete("pic.jpg");}
- if(File.Exists("pic2.png")){ File.Delete("pic2.png");}
- if(File.Exists("picstring.txt")){ File.Delete("picstring.txt");}
- if(File.Exists("pic2string.txt")){ File.Delete("pic2string.txt");}
- Console.WriteLine("\n I want to:\n");
- Console.WriteLine("\tConvert (J)PG to Base64 string");
- Console.WriteLine("\tConvert (P)NG to Base64 string");
- Console.WriteLine("\tConvert (B)ase64 string to JPG");
- Console.Write("\tConvert B(A)se64 string to PNG > ");
- ConsoleKeyInfo ki = Console.ReadKey();
- if(ki.Key == ConsoleKey.J){
- Console.WriteLine("\n We'll try to convert JPG to Base64 string...");
- string converted = ImageToBase64(Image.FromFile(".\\temp\\pic.jpg"));
- using(StreamWriter sw = new StreamWriter("picstring.txt")){
- sw.Write(converted);
- }
- }else{
- if(ki.Key == ConsoleKey.B){
- Console.WriteLine("\n We'll try to convert Base64 string to JPG...");
- string data = string.Empty;
- using(StreamReader sr = new StreamReader(".\\temp\\picstring.txt")){
- data = sr.ReadToEnd();
- }
- Bitmap b = Base64ToBitmap(data);
- b.Save("pic.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
- }else{
- if(ki.Key == ConsoleKey.P){
- Console.WriteLine("\n We'll try to convert PNG to Base64 string...");
- string converted2 = ImageToBase64(Image.FromFile(".\\temp\\pic2.png"));
- using(StreamWriter sw = new StreamWriter("pic2string.txt")){
- sw.Write(converted2);
- }
- }else{
- if(ki.Key == ConsoleKey.A){
- Console.WriteLine("\n We'll try to convert Base64 string to PNG...");
- string data2 = string.Empty;
- using(StreamReader sr2 = new StreamReader(".\\temp\\pic2string.txt")){
- data2 = sr2.ReadToEnd();
- }
- Bitmap b2 = Base64ToBitmap(data2);
- b2.Save("pic2.png", System.Drawing.Imaging.ImageFormat.Png);
- }else{
- Console.WriteLine("\n You didn't select a valid choice.");
- }
- }
- }
- }
- }
- }
- private static bool EndsWithImageFileFormatExtension(string filepath){
- bool result = false;
- if(filepath.EndsWith(".png")){ result = true; }
- if(filepath.EndsWith(".jpg")){ result = true; }
- if(filepath.EndsWith(".jpeg")){ result = true; }
- if(filepath.EndsWith(".bmp")){ result = true; }
- return result;
- }
- private static string ImageToBase64(Image image)
- {
- try
- {
- var imageStream = new MemoryStream();
- image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Bmp);
- imageStream.Position = 0;
- var imageBytes = imageStream.ToArray();
- var ImageBase64 = Convert.ToBase64String(imageBytes);
- return ImageBase64;
- }
- catch
- {
- return "Error converting image to base64!";
- }
- }
- private static Bitmap Base64ToBitmap(string ImageText)
- {
- Byte[] bitmapData = Convert.FromBase64String(FixBase64ForImage(ImageText));
- System.IO.MemoryStream streamBitmap = new System.IO.MemoryStream(bitmapData);
- Bitmap bitImage = new Bitmap((Bitmap)Image.FromStream(streamBitmap));
- return bitImage;
- }
- private static string FixBase64ForImage(string Image) {
- System.Text.StringBuilder sbText = new System.Text.StringBuilder(Image,Image.Length);
- sbText.Replace("\r\n", String.Empty); sbText.Replace(" ", String.Empty);
- return sbText.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement