无法从jpg图片中获取像素颜色
Hellow
i我试图使用c sharp程序获取字符串像素的颜色
字符串是在画笔程序中创建的并且等于+ -1234567890
我选择了文本(大资本A图标)并选择黑色作为白色背景上的前景色,文字大小为16
然后我写了+ -1234567890
并将其保存在名为MyNumbers.jpg的文件中
在下面的程序中我找不到油漆刷程序中的黑色颜色
我的程序的目的是存储(在文件中)黑色像素(前景)为1和白色像素(背景)为0
i不能这样做
以下是完整的程序...请帮助
我尝试过的事情:
Hellow
i am trying to get the color of pixels of a string using c sharp program
the string is created in paint brush program and equal +-1234567890
in paint brush i selected text (the big capital A icon)and choosed black as a foreground color on white background the size of text was 16
then i wrote +-1234567890
and saved it in file named MyNumbers.jpg
in the program below i could not find the black color which was made in paint brush program
the purpose of my program is to store (in file)the black pixels(the foreground) as 1 and the white pixel(the background) as 0
i could not do that
the following is the complete program ... please help
What I have tried:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace ScalImage
{
public partial class Form1 : Form
{
public FileStream fsw;
public StreamWriter sw;
//----------------------------
Pen p;
int x, y;
int start_x, start_y;
Bitmap _bmp;
Color co;
PictureBox MyPictureBox1;
Graphics gr;
public Form1()
{
InitializeComponent();
}
private void FormLoad(object sender, EventArgs e)
{
fsw = new FileStream("MyDebug.txt", FileMode.Truncate, FileAccess.Write);
sw = new StreamWriter(fsw);
//-------------------------------------------
MyPictureBox1 = new PictureBox();
p = new Pen(Color.Red, 1);
//-------------------------------
MyPictureBox1.Parent = this;
MyPictureBox1.Enabled = true;
MyPictureBox1.Left = 1;
MyPictureBox1.Top = 1;
MyPictureBox1.Width = 160;
MyPictureBox1.Height = 64;
MyPictureBox1.Image = Image.FromFile("MyNumbers.jpg");//"MyNumbers.bmp");//"MyNumbers.gif");
MyPictureBox1.Show();
//-----------------------------------------------------
_bmp = new Bitmap(MyPictureBox1.Image, MyPictureBox1.Size);
//Graphics objGraphics = Graphics.FromImage(_bmp);
gr = this.MyPictureBox1.CreateGraphics();
textBox1.Text = MyPictureBox1.Width.ToString();//ReadOnly
textBox2.Text = MyPictureBox1.Height.ToString();//ReadOnly
}
private void button1_Click(object sender, EventArgs e)
{
int f1,f2,f3;
for (y = 1; y < MyPictureBox1.Size.Height; y++)
{
for (x = 1; x < MyPictureBox1.Size.Width; x++)
{
co = _bmp.GetPixel(x, y);
//sw.Write(pixelColor[x, y].R.ToString()+","+ pixelColor[x, y].G.ToString()+","+ pixelColor[x, y].B.ToString()+" ");
f1=0; f2=0; f3=0;
if (co.R != 255 ) f1=1;
if (co.G != 255 ) f2=1;
if (co.B != 255 ) f3=1;
;
if(f1==1 && f2==1 && f3==1) sw.Write("1");
else sw.Write("0");
}
sw.Write("\r\n");
}
start_x = int.Parse(textBox3.Text);//you may need this
start_y = int.Parse(textBox4.Text);//you may need this
gr.DrawLine(p, start_x, start_y, start_x + MyPictureBox1.Width, start_y);//you may need this
}
//-----------------------------------------
protected override void OnClosing(CancelEventArgs e)
{
if (sw != null) sw.Close();
if (fsw != null) fsw.Close();
}
//-----------------------------------------
}
}
你想将黑色像素渲染为1,但程序中的逻辑完全相反。
实际上你可以简化整个事情通过替换
You want to rendre a black pixel as 1 but the logic in your program does exactly the contrary.
In fact you can simplify the whole thing by replacing
f1=0; f2=0; f3=0;
if (co.R != 255 ) f1=1;
if (co.G != 255 ) f2=1;
if (co.B != 255 ) f3=1;
if (f1 == 1 && f2 == 1 && f3 == 1)
sw.Write("1");
else
sw.Write("0");
with
with
sw.Write((co == Color.Black) ? "1" : "0");
我严重质疑操作的真正兴趣,但是:) />
您还应该尽早采用与您的代码块相同的方式。看看基本的调试技术也很有用。
你永远不会把图像加载到你的_bmp变量。
也许试试
I seriously question the real interest of the operation, though :)
You should also adopt as early as possible some consistency in the way your are identing your code blocks. And having a look at basic debugging techniques could be quite useful, too.
You never load the image to your _bmp variable.
Maybe try
_bmp = Image.FromFile("MyNumbers.jpg");
初始化变量。
to initialize your variable.
你不应该使用GetPixel
来进行大规模的像素操作;这是非常慢的操作。只有你需要阅读几个像素中的一个才能接受。
这是正确的方法: Bitmap.LockBits方法(System.Drawing) [ ^ ]。
另外,我不确定你真的需要使用PictireBox
。如果你需要在屏幕上渲染一些图形,你根本不需要它,它可能没有帮助。如果你解释你的最终目标,你想要实现什么,我们可以讨论它。-SA
You should never useGetPixel
for massive pixel operations; it's prohibitively slow operation. It can only be acceptable if you need to read one of just few pixels.
This is the correct approach: Bitmap.LockBits Method (System.Drawing)[^].
Also, I'm not sure you really need to usePictireBox
. If you need to render some graphics on screen, you don't need it at all, it may be not helpful. We can discuss it if you explain your ultimate goals, what you want to achieve.—SA
我已经更改了它并且它可以工作
i have change this and it works
//MyPictureBox1.Image = Image.FromFile("aa.bmp");//"MyNumbers.gif");
//MyPictureBox1.Show();
//-----------------------------------------------------
//_bmp = new Bitmap(MyPictureBox1.Image, MyPictureBox1.Size);
_bmp = (Bitmap)Image.FromFile("aa.bmp");
我写的这个循环中的
in the loop i wrote this
for (y = 1; y < _bmp.Height; y++)
{
for (x = 1; x < _bmp.Width; x++)
{
co = _bmp.GetPixel(x, y);
//sw.Write(pixelColor[x, y].R.ToString()+","+ pixelColor[x, y].G.ToString()+","+ pixelColor[x, y].B.ToString()+" ");
f1=0; f2=0; f3=0;
if (co.R != 255 ) f1=1;
if (co.G != 255 ) f2=1;
if (co.B != 255 ) f3=1;
;
if(f1==1 && f2==1 && f3==1) sw.Write("1");
else sw.Write("0");
}
sw.Write("\r\n");
}