Getpixel对于任何位图都返回0 [英] Getpixel returns 0 for any bitmap

查看:204
本文介绍了Getpixel对于任何位图都返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


对不起,我的巴西英语.
我试图做一个小程序来创建一个二维数组,如果有颜色则返回1,如果没有则返回0,但是返回0.有人可以帮助我吗?我用一个列表框查看退货

Hi,
sorry my braziliam english.
im trying to do a little program that create a 2d array that returns 1 if have color or 0 if no but ever return 0. can anybody help me? i used a listbox to see the returns

Bitmap newBitmap = new Bitmap(img.Width, img.Height);
 
byte[,] buffer = new byte[100,100]; 
 
for(int y=0;y<img.Height;y++) 
{ 
  for(int x=0;x<img.width;x++)>
  { 
   Color c = newBitmap.GetPixel(x,y); 
   int lumi = Convert.ToByte((int)(c.R*0.3 + c.G*0.59 + c.B*0.11));
   buffer[x,y] = Convert.ToByte((lumi  > 127) ? 1 : 0);
   
   listBox1.Items.Add(lumi);
  } 
} 

推荐答案

位图 [^ ]使用
Bitmap newBitmap = new Bitmap(img.Width, img.Height);

具有默认的黑色背景.因此2D数组中的所有值都将为0.

开始检查颜色之前,您需要在位图中放置彩色图片.

注意:
1-删除 Convert.ToByte [

has a default black background. So all the values in your 2D array will be 0.

Before you start checking for colour you will need to place a colour picture in your bitmap.

Notes:
1 - Remove Convert.ToByte[^] from

int lumi = Convert.ToByte((int)(c.R*0.3 + c.G*0.59 + c.B*0.11));

没有用.

2-如果img.Widthimg.Height大于100,则您的程序将崩溃.

it serves no purpose.

2 - Your program will crash if either img.Width or img.Height is larger then 100.


这篇关于Getpixel对于任何位图都返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆