私は将来のプロジェクトのためにいくつかのテストを行っています。このプロジェクトでは、イメージの暗号化を作成します。現時点では、ビットマップをバイト配列に変換し、それをテキスト ファイルに保存し、再読み込みして、別の名前で再保存する方法をまとめたいと思っていました。動作するようになりました...その後、ファイルサイズに問題があります。私の変換された画像(画像を作成するためにバイト配列から読み取ったもの)は、元の画像よりも大きなファイルサイズを示しています。これが私のコードです:
public class Program
{
public static void Main( string[] args )
{
/**
* Load the bitmap and convert it to a byte array
* then save the file to the desktop
*/
byte[] imageBytes = ImageToByte( new Bitmap( "C:/Users/Krythic/Desktop/NovaEngine.png" ) );
File.WriteAllBytes( "C:/Users/Krythic/Desktop/NovaImageData.txt" , imageBytes );
/**
* Load the saved image bytes, then convert them back into an image and save it to the
* desktop under a new name.
*/
byte[] convertedImageBytes = File.ReadAllBytes("C:/Users/Krythic/Desktop/NovaImageData.txt");
Bitmap image = ConvertToBitmap(convertedImageBytes);
image.Save("C:/Users/Krythic/Desktop/ConvertedImage.png");
}
public static byte[] ImageToByte( Bitmap img )
{
ImageConverter converter = new ImageConverter();
return ( byte[] )converter.ConvertTo( img , typeof( byte[] ) );
}
private static Bitmap ConvertToBitmap( byte[] imagesSource )
{
ImageConverter imageConverter = new ImageConverter();
Image image = ( Image )imageConverter.ConvertFrom( imagesSource );
return new Bitmap( image );
}
}
問題は image.Save(); によるものだと思います。関数...これは...画像に最適な圧縮を選択していないと思います。多分私は間違っていますか?両方のイメージのプロパティ ページの図を次に示します。
また、元のイメージの保存されたバイト配列バージョンのファイル サイズが大きくなっていることがわかります。どうしてこれなの?ファイルのサイズは、変換全体にわたって一定のままであるべきではありませんか?
更新: 画像を変換するために使用している関数は、不十分な変換手法を使用していると確信しています。これは、元の png のサイズが、等しいはずのバイト配列ファイル バージョンと異なる理由を説明します。したがって、これを解決するには、これら 2 つの関数と同じことを行う効率的または正しい方法が必要です。