1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
using System;
using System.IO;
class image2src
{
public static void Main( string[] args )
{
if ( args.Length <= 0 ) {
Console.WriteLine( "error; input file was not specified" );
return;
}
string file = args[0];
if ( !File.Exists( file ) ) {
Console.WriteLine( "error; file not found" );
return;
}
string name = Path.GetFileNameWithoutExtension( file );
Console.WriteLine( "Image " + name + "()" );
Console.WriteLine( "{" );
Console.WriteLine( " try{" );
Console.Write( " return ImageIO.read( new ByteArrayInputStream( new byte[]{" );
using ( FileStream fs = new FileStream( file, FileMode.Open, FileAccess.Read ) ) {
const int BUFLEN = 1024;
const int COLUMN = 10;
byte[] buf = new byte[BUFLEN];
long length = fs.Length;
long remain = length;
int count = COLUMN - 1;
while ( remain > 0 ) {
int amount = (remain > BUFLEN) ? BUFLEN : (int)remain;
fs.Read( buf, 0, amount );
for ( int i = 0; i < amount; i++ ) {
string s = "(byte)" + buf[i] + ",";
count++;
if( count != COLUMN - 1 ){
for ( int j = s.Length; j < "(byte)255, ".Length; j++ ) {
s += " ";
}
}
if ( count == COLUMN ) {
Console.WriteLine();
Console.Write( " " );
count = 0;
}
Console.Write( s );
}
remain -= amount;
}
}
Console.WriteLine( "} ) );" );
Console.WriteLine( " }catch( Exception ex ){" );
Console.WriteLine( " return null;" );
Console.WriteLine( " }" );
Console.WriteLine( "}" );
}
}
|