Last active
October 11, 2016 05:53
-
-
Save TonyMooori/adb0dc3ab5e0748e93c0927f8d6e15e0 to your computer and use it in GitHub Desktop.
文字列をpngに入れ込むプログラム
This file contains hidden or 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.Drawing; | |
| using System.Text; | |
| class Program | |
| { | |
| static void Main() | |
| { | |
| Console.WriteLine("1:\t文字列を変換"); | |
| Console.WriteLine("2:\t文字列に戻す"); | |
| Console.WriteLine("3:\tファイルを変換"); | |
| Console.WriteLine("4:\tファイルに戻す"); | |
| var cmd = int.Parse(Console.ReadLine()); | |
| if( cmd == 1 ) | |
| { | |
| Console.WriteLine("変換したい文字列を入力"); | |
| var img = PngConverter.StringToPng(Console.ReadLine()); | |
| Console.WriteLine("出力先を入力"); | |
| img.Save(Console.ReadLine()+".png"); | |
| } | |
| else if( cmd == 2 ) | |
| { | |
| Console.WriteLine("PNG画像のファイル名を入力"); | |
| Console.WriteLine(PngConverter.PngToString(new Bitmap(Console.ReadLine()))); | |
| } | |
| else if( cmd == 3 ) | |
| { | |
| Console.WriteLine("変換したいファイル名を入力"); | |
| var filename = Console.ReadLine(); | |
| var img = PngConverter.FileToPng(filename); | |
| Console.WriteLine("出力先を入力"); | |
| img.Save(Console.ReadLine()+".png"); | |
| } | |
| else if( cmd == 4 ) | |
| { | |
| Console.WriteLine("PNG画像のファイル名を入力"); | |
| var img = new Bitmap(Console.ReadLine()); | |
| Console.WriteLine("出力先を入力"); | |
| var filename = Console.ReadLine(); | |
| PngConverter.PngToFile(img,filename); | |
| } | |
| } | |
| } | |
| class PngConverter | |
| { | |
| public static byte[] FileToBytes(string filename) | |
| { | |
| var fs = new FileStream( | |
| filename, | |
| System.IO.FileMode.Open, | |
| System.IO.FileAccess.Read); | |
| var bs = new byte[fs.Length]; | |
| fs.Read(bs, 0, bs.Length); | |
| fs.Close(); | |
| return bs; | |
| } | |
| public static Bitmap FileToPng(string filename) | |
| { | |
| var bs = FileToBytes(filename); | |
| return BytesToPng(bs); | |
| } | |
| public static void PngToFile(Bitmap img,string filename) | |
| { | |
| var bs = PngToBytes(img); | |
| BytesToFile(bs,filename); | |
| } | |
| public static void BytesToFile(byte[] bs,string filename) | |
| { | |
| System.IO.FileStream fs = new FileStream( | |
| filename, | |
| System.IO.FileMode.Create, | |
| System.IO.FileAccess.Write); | |
| fs.Write(bs, 0, bs.Length); | |
| fs.Close(); | |
| } | |
| public static Bitmap StringToPng(string str) | |
| { | |
| var bs = StringToBytes(str); | |
| return BytesToPng(bs); | |
| } | |
| public static Bitmap BytesToPng(byte[] bs) | |
| { | |
| bs = AddLengthData(bs); // dataを整形 | |
| // Bitmapのインスタンスを生成 | |
| var img_wh = (int)( 1 + Math.Sqrt( bs.Length / 4 )); | |
| var img = new Bitmap(img_wh,img_wh); | |
| // 書き込む | |
| for(int i = 0 ; i < bs.Length / 4 ; i++ ) | |
| { | |
| int a = (int)bs[ 4*i + 0 ]; | |
| int r = (int)bs[ 4*i + 1 ]; | |
| int g = (int)bs[ 4*i + 2 ]; | |
| int b = (int)bs[ 4*i + 3 ]; | |
| img.SetPixel( i % img_wh , i / img_wh , Color.FromArgb(a,r,g,b) ); | |
| } | |
| return img; | |
| } | |
| private static byte[] AddLengthData(byte[] bs) | |
| { | |
| var len = 8 + bs.Length; // Int64用の8byte + そのものの長さ | |
| len = len + 4 - (len%4); // 4の倍数にする | |
| var ret = new byte[len]; // 大きさを調節した配列 | |
| // dataの長さの情報とdataを書き込む | |
| Array.Copy(PngConverter.Int64ToBytes((Int64)bs.Length), 0, ret, 0, 8); | |
| Array.Copy(bs,0,ret,8, bs.Length); | |
| return ret; | |
| } | |
| private static byte[] RemoveLengthData(byte[] bs) | |
| { | |
| // dataの本来の配列の長さを取り出す | |
| var length_bytes = new byte[8]; | |
| Array.Copy(bs,0,length_bytes,0,8); | |
| var len = BytesToInt64(length_bytes); | |
| var ret = new byte[len]; // もとの長さの配列を生成 | |
| Array.Copy(bs,8,ret,0,ret.Length); // 必要な情報だけ取り出す | |
| return ret; | |
| } | |
| public static byte[] PngToBytes(Bitmap img) | |
| { | |
| var bs = new byte[img.Width * img.Height * 4]; | |
| for(int i = 0 ; i < bs.Length / 4 ; i++ ) | |
| { | |
| var px = img.GetPixel( i % img.Width , i / img.Width ); | |
| bs[4*i + 0] = (byte)px.A; | |
| bs[4*i + 1] = (byte)px.R; | |
| bs[4*i + 2] = (byte)px.G; | |
| bs[4*i + 3] = (byte)px.B; | |
| } | |
| return RemoveLengthData(bs); | |
| } | |
| public static byte[] StringToBytes(string str) | |
| { | |
| return Encoding.UTF8.GetBytes(str); | |
| } | |
| public static byte[] Int64ToBytes(Int64 n) | |
| { | |
| var ret = new byte[8]; | |
| for(int i = 0 ; i < 8 ; i++ ) | |
| { | |
| ret[i] = (byte)( n & 0xff ); | |
| n = n >> 8; | |
| } | |
| return ret; | |
| } | |
| public static Int64 BytesToInt64(byte[] bs) | |
| { | |
| Int64 ret = 0; | |
| for(int i = 0 ; i < 8 ; i++ ) | |
| ret += ((Int64)bs[i]) << (i*8); | |
| return ret; | |
| } | |
| public static string BytesToString(byte[] bs) | |
| { | |
| return Encoding.UTF8.GetString(bs); | |
| } | |
| public static string PngToString(Bitmap img) | |
| { | |
| var bs = PngToBytes(img); | |
| return BytesToString(bs); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment