Advertisement
kyrathasoft

JpgOrPng2Base64

Jun 1st, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.92 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Drawing;
  4.  
  5. /* online pastes of source code
  6.     + http://pasted.co/bdbe18be
  7.     + https://gist.github.com/kyrathasoft/b05c96d8b0896868c30531d01567c9d5
  8.     +
  9. */
  10.  
  11. namespace ConsoleApplication {
  12.  
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             Console.Clear();
  18.             Console.ForegroundColor = ConsoleColor.Yellow;
  19.             Console.WriteLine("\n Convert image to Base64 string\n\n");
  20.             Console.ForegroundColor = ConsoleColor.White;
  21.             if(args.Length == 0){
  22.                
  23.                 if(File.Exists("pic.jpg")){ File.Delete("pic.jpg");}
  24.                 if(File.Exists("pic2.png")){ File.Delete("pic2.png");}
  25.                 if(File.Exists("picstring.txt")){ File.Delete("picstring.txt");}
  26.                 if(File.Exists("pic2string.txt")){ File.Delete("pic2string.txt");}
  27.                
  28.                 Console.WriteLine("\n I want to:\n");
  29.                 Console.WriteLine("\tConvert (J)PG to Base64 string");
  30.                 Console.WriteLine("\tConvert (P)NG to Base64 string");
  31.                 Console.WriteLine("\tConvert (B)ase64 string to JPG");
  32.                 Console.Write("\tConvert B(A)se64 string to PNG > ");
  33.                 ConsoleKeyInfo ki = Console.ReadKey();
  34.                
  35.                 if(ki.Key == ConsoleKey.J){
  36.                     Console.WriteLine("\n We'll try to convert JPG to Base64 string...");
  37.                     string converted = ImageToBase64(Image.FromFile(".\\temp\\pic.jpg"));
  38.                     using(StreamWriter sw = new StreamWriter("picstring.txt")){
  39.                         sw.Write(converted);
  40.                     }
  41.                 }else{
  42.                     if(ki.Key == ConsoleKey.B){                        
  43.                         Console.WriteLine("\n We'll try to convert Base64 string to JPG...");
  44.                         string data = string.Empty;
  45.                         using(StreamReader sr = new StreamReader(".\\temp\\picstring.txt")){
  46.                             data = sr.ReadToEnd();
  47.                         }
  48.                         Bitmap b = Base64ToBitmap(data);  
  49.                         b.Save("pic.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);                                                  
  50.                     }else{
  51.                         if(ki.Key == ConsoleKey.P){
  52.                             Console.WriteLine("\n We'll try to convert PNG to Base64 string...");
  53.                             string converted2 = ImageToBase64(Image.FromFile(".\\temp\\pic2.png"));
  54.                             using(StreamWriter sw = new StreamWriter("pic2string.txt")){
  55.                                 sw.Write(converted2);
  56.                             }
  57.                         }else{
  58.                             if(ki.Key == ConsoleKey.A){
  59.                                 Console.WriteLine("\n We'll try to convert Base64 string to PNG...");
  60.                                 string data2 = string.Empty;
  61.                                 using(StreamReader sr2 = new StreamReader(".\\temp\\pic2string.txt")){
  62.                                     data2 = sr2.ReadToEnd();
  63.                                 }
  64.                                 Bitmap b2 = Base64ToBitmap(data2);
  65.                                 b2.Save("pic2.png", System.Drawing.Imaging.ImageFormat.Png);
  66.                             }else{
  67.                                 Console.WriteLine("\n You didn't select a valid choice.");
  68.                             }
  69.                         }
  70.                        
  71.                     }
  72.                 }
  73.             }
  74.         }  
  75.        
  76.         private static bool EndsWithImageFileFormatExtension(string filepath){
  77.             bool result = false;
  78.            
  79.             if(filepath.EndsWith(".png")){ result = true; }
  80.             if(filepath.EndsWith(".jpg")){ result = true; }
  81.             if(filepath.EndsWith(".jpeg")){ result = true; }
  82.             if(filepath.EndsWith(".bmp")){ result = true; }
  83.            
  84.             return result;
  85.         }
  86.        
  87.         private static string ImageToBase64(Image image)
  88.         {
  89.             try
  90.             {
  91.                 var imageStream = new MemoryStream();
  92.                 image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Bmp);
  93.                 imageStream.Position = 0;
  94.                 var imageBytes = imageStream.ToArray();
  95.                 var ImageBase64 = Convert.ToBase64String(imageBytes);
  96.                 return ImageBase64;
  97.             }
  98.             catch
  99.             {
  100.                 return "Error converting image to base64!";
  101.             }
  102.         }
  103.        
  104.         private static Bitmap Base64ToBitmap(string ImageText)
  105.         {
  106.             Byte[] bitmapData = Convert.FromBase64String(FixBase64ForImage(ImageText));
  107.             System.IO.MemoryStream streamBitmap = new System.IO.MemoryStream(bitmapData);
  108.             Bitmap bitImage = new Bitmap((Bitmap)Image.FromStream(streamBitmap));
  109.             return bitImage;
  110.         }
  111.  
  112.         private static string FixBase64ForImage(string Image) {
  113.             System.Text.StringBuilder sbText = new System.Text.StringBuilder(Image,Image.Length);
  114.             sbText.Replace("\r\n", String.Empty); sbText.Replace(" ", String.Empty);
  115.             return sbText.ToString();
  116.         }      
  117.  
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement