【发布时间】:2015-12-07 01:06:59
【问题描述】:
我有一些代码行。我只想将灰度图像转换为二进制图像。它看起来很简单,但我不明白哪里错了! 你能告诉我哪里错了吗。 就是这样:
private void convert()
{
try
{
OpenFileDialog op = new OpenFileDialog();
op.InitialDirectory = "D:/";
op.Filter = "All Files|*.*|JPEGs|*.jpg|Bitmaps|*.bmp|GIFs|*.gif";
op.FilterIndex = 1;
if (op.ShowDialog() == DialogResult.OK)
{
pictureBox3.Image = Image.FromFile(op.FileName);
pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox3.BorderStyle = BorderStyle.Fixed3D;
Bitmap img = new Bitmap(op.FileName);
int width = img.Width;
int height = img.Height;
string t = "";
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
if (img.GetPixel(j, i).A > 100 && img.GetPixel(j, i).B > 100 && img.GetPixel(j, i).G > 100 && img.GetPixel(j, i).R > 100)
{
t = t + "0";
}
else
{
t = t + "1";
}
}
t = t + "\r\n";
}
textBox1.Text = t;
}
}
catch { };
}
谢谢大家!
【问题讨论】:
-
忽略 Alpha 通道。选择列出的公式之一here
-
它在什么方面不起作用?
-
@Martheen,你为什么要忽略 Alpha 通道?
-
除非图像是 PNG 或 GIF 格式并实际利用透明度,否则将毫无意义。
-
投票以“不清楚你在问什么”作为结束投票,因为@Learning_English 没有解释它在什么方面不起作用。