元の bpp で画像を処理して保存する必要があります (1 - BnW の場合、8 - グレーの場合、24 - カラー)。処理後、私は常に24bppを持っています(Aforge.Netで処理しているため)。元の bpp を保存関数に渡し、次のコードを保存に使用しています。
private static Bitmap changePixelFormat(Bitmap input, PixelFormat format)
{
Bitmap retval = new Bitmap(input.Width, input.Height, format);
retval.SetResolution(input.HorizontalResolution, input.VerticalResolution);
Graphics g = Graphics.FromImage(retval);
g.DrawImage(input, 0, 0);
g.Dispose();
return retval;
}
私が通過するとき:
PixelFormat.Format8bppIndexed
次の行で、「グラフィックス オブジェクトはインデックス付きピクセル形式で画像を作成できません」という例外が発生します。
Graphics g = Graphics.FromImage(retval);
次のコードを試しました:
Bitmap s = new Bitmap("gray.jpg");
Bitmap NewPicture = s.Clone(new Rectangle(0, 0, s.Width, s.Height), PixelFormat.Format8bppIndexed);
この「NewImage」の後、PixelFormat 8bppIndexed がありますが、NewImage を保存すると、次のようになります。
NewPicture.Save("hello.jpg", ImageFormat.Jpeg);
hello.jpg は 24bpp です。
元の画像の bpp と画像形式を維持する必要があります。
Bitmap.Save が Bitmap の PixelFormat を無視するのはなぜですか?
================================================== =====
みんなありがとう、私は解決策を見つけました: http://freeimage.sourceforge.net/license.html
この無料ライブラリの助けを借りて、画像 (特に JPEG ) をグレースケール 8 ビットで保存できます。