且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法从jpg图片中获取像素颜色

更新时间:2023-01-28 20:14:55

你想将黑色像素渲染为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 use GetPixel 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 use PictireBox. 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");
}