電子書籍の厳選無料作品が豊富!

また分からないことが出来ましたので、よろしくお願いいたします。
今、openFileDialogで画像を読取、その画像の黒色を白色に変更してpicutreBoxに表示するプログラムを作成しています。
[C#]
Bitmap img = new Bitmap(openFileDialog.FileName);
Graphics g = Graphics.FromImage(img);

System.Drawing.Imaging.ColorMap[] cms =
new System.Drawing.Imaging.ColorMap[]
{new System.Drawing.Imaging.ColorMap(),
new System.Drawing.Imaging.ColorMap()};

cms[0].OldColor = Color.Black;
cms[0].NewColor = Color.White;

System.Drawing.Imaging.ImageAttributes ia =
new System.Drawing.Imaging.ImageAttributes();

ia.SetRemapTable(cms);

g.DrawImage(img, new Rectangle(img.Width + 10, 0, img.Width, img.Height),
0, 0, img.Width, img.Height, GraphicsUnit.Pixel,ia);

g.Dispose();

PictureBox1.Image = img;

という風に作成したのですが、色が変更されませんでした。
どこが違うのか分かられる方がいらっしゃいましたら、よろしくお願いいたします。

A 回答 (4件)

色の比較が Color.Blackではうまくいかない場合があるようです



if ( img.GetPixel(x,y) == Color.FromaArgb(255,0,0,0) )
といった具合のほうがいいようです …

当方が実験したコードを記載します

  Bitmap bmp = new Bitmap(255, 255);
  Graphics g1;
  g1 = Graphics.FromImage(bmp);
  pictureBox1.Image = null;

  // リニアグラデーションブラシで 黒からシアンを作成
  LinearGradientBrush br = new LinearGradientBrush(
    new Point(0, 128), new Point(255, 128),
    Color.FromArgb(0, 0,0), Color.FromArgb(0, 255,255));
  // 矩形を描画
  g1.FillRectangle(br, new Rectangle(0, 0, 255, 255));
  // シアンの円を2つ描画
  g1.FillEllipse(new SolidBrush(Color.FromArgb(0, 128, 128)), new Rectangle(20, 20, 180, 180));
  g1.FillEllipse(new SolidBrush(Color.FromArgb(0, 255, 255)), new Rectangle(40, 40, 140, 140));
  g1.Dispose();

  bmp.Save("sample01.jpg");
  ImageAttributes ia = new ImageAttributes();
  // 閾値を0.7にしてマスクを作成
  ia.SetThreshold(0.7F);
  Bitmap img = (Bitmap)Bitmap.FromFile("sample01.jpg");
  g1 = Graphics.FromImage(img);
  g1.DrawImage(img, new Rectangle(0, 0, 255, 255), 0, 0, 255, 255, GraphicsUnit.Pixel, ia);
  g1.Dispose();

  pictureBox1.Image = img;
  img.Save("Sample02.jpg");

    for (int y = 0; y < img.Height; y++)
    {
      for (int x = 0; x < img.Width; x++)
      {
        // マスクをチェック
        if (img.GetPixel(x, y) == Color.FromArgb(255,0,0,0))
        {
          bmp.SetPixel(x, y, Color.White);
        }
      }
    }
    pictureBox2.Image = bmp;
    bmp.Save("sample03.jpg");
  }
といった具合です
    • good
    • 0
この回答へのお礼

お礼遅くなって申し訳ございません。
私の場合、Color.FromArgb(255,0,0,0)では正確でないので、これでは出来ませんでした。

int P = bitmap.GetPixel(X, Y).ToArgb();
if(P == 色変更したいところのP)
bitmap2.SetPixel(X, Y, Color.FromArgb(0xffffff));
else
bitmap2.SetPixel(X, Y, Color.FromArgb(P));
というふうにして解決できました。
何度も回答していただき、ありがとうございました。
また、質問がでてきた際はよろしくお願いいたします。

お礼日時:2008/03/12 11:55

マスク画像を作成してこれを使って BitmapのGetPixel/SetPixelで処理かなぁ


# もっとスマートなやり方があると思いますが …

ImageAttributes Ia = new ImageAttributes();
Ia.SetThreshold(0.7F);

g.DrawImage( img, new Rectatngle( 0, 0, img.Width, img.Height),
0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia );
g.Dispose();

for( y = 0; y < img.Height; y++ ) {
  for ( x = 0; x < img.Width; x++ ) {
    if ( img.GetPixel( x, y ) == Color.Black ) {
      bmp.SetPixel( x, y, Color.White );
    }
  }
}
といった具合かなぁ …

この回答への補足

回答ありがとうございます。
記述とおり、プログラムしてみたのですが、
色が薄くなっただけで、黒色が白色には変わりませんでした。
どうしたらよろしいでしょうか?

補足日時:2008/02/27 10:12
    • good
    • 0

一部訂正


Bitmap bmp = new Bitmap( img.Width * 2 + 20, img.Height );
でした

この回答への補足

回答ありがとうございます。
web上の画像を保存し、それを読み込んで色変換することは出来ました。
しかし、スキャンした画像を読み込んで、色の変更をしようとすると、
スキャンした画像の色が同じ黒でもRGBが違っていて、
cms[0].OldColor = Color.Black;
では、判断してくれないようです。
RGBがこの範囲からこの範囲までの色を白色にしたいときは、どうすればよろしいでしょうか?

補足日時:2008/02/26 08:28
    • good
    • 0

g.DrawImageで描画している位置がおかしいと思います



元もと gを Graphics.FromImageで作成しているので
img.Widthの幅しかありません
img.Width + 10が左端とした場合 クリッピングされて描画自体しないでしょう

2つの描画結果得たいのであれば
Bitmap bmp = new Bitmap( img.Width + 20, img.Height );
Graphics g = Graphics.FromImage( bmp );

といった具合でグラフィックスオブジェクトを作成して

g.DrawImage( img, 0, 0, img.Width, img. Height );
g.DrawImage( New Rectangle( img.Width + 10, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia );

といった具合で描画しましょう
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!