C# 使用未分配的局部变量';p';

C# 使用未分配的局部变量';p';,c#,C#,我认为if与我的if声明有关 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace dotheaven

我认为if与我的if声明有关

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace dotheaven
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        //image path
        string img = "";

        //read image
        Bitmap bmp = new Bitmap(img);


        //get image dimension
        int width = bmp.Width;
        int height = bmp.Height;

        //3 bitmap for red green blue image
        Bitmap pic = new Bitmap(bmp);

        //red green blue image
        Color p;
        int t = 15;
        int x;
        int y;
        int a;
        int r;
        int g;
        int b;
        //if green
        int rl;
        int rh;
        int gl;
        int gh;
        int bl;
        int bh;
        for (y = 0; y < height; y++)
        {
            for (x = 0; x < width; x++)
                //get pixel value
                p = bmp.GetPixel(x, y);
            //extract ARGB value from p
            a = p.A;
            r = p.R;
            g = p.G;
            b = p.B;
            //if green
            rl = 64 - t;
            rh = 64 + t;
            gl = 108 + t;
            gh = 108 + t;
            bl = 111 - t;
            bh = 111 + t;
              // the if statement is chopped off, thats not reason of error
            if ((rl < r) && (rh > r) && (gl < g) && (gh > g) && (gl < g) )
            {
                pic.SetPixel(x, y, Color.FromArgb(a, 64, 108, 111));
            }
            else
            {
                pic.SetPixel(x, y, Color.FromArgb(200, 255, 255, 255));
            }
            //rbmp.SetPixel(x, y, Color.FromArgb(a, r, 0, 0));
            //gmbp.SetPixel(x, y, Color.FromArgb(a, 0, g, 0));
            // bbmp.SetPixel(x, y, Color.FromArgb(a, 0, 0, b));
        }
    }
    //load images
    // pictureBox1.Image = pic;
    //pic.Save("enter path");
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
天堂
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
//图像路径
字符串img=“”;
//读取图像
位图bmp=新位图(img);
//获取图像维度
int width=bmp.width;
int-height=bmp.height;
//3红绿蓝图像的位图
位图pic=新位图(bmp);
//红绿蓝图像
颜色p;
int t=15;
int x;
int-y;
INTA;
INTR;
int g;
int b;
//如果绿色
int rl;
int-rh;
int gl;
int-gh;
int bl;
内特波黑;
对于(y=0;y<高度;y++)
{
对于(x=0;xr)&&(glg)&&(gl
您只是在循环中分配
p
(并覆盖其先前的值,这几乎肯定是错误的,但没关系,我想这将是您下一个stackoverflow问题的主题),但您假设
宽度将大于0,因此,您假设您的循环将至少执行一个。编译器很乐意向您指出,以您的名义做出这样的假设是不安全的。

您只是在循环中赋值
p
(并覆盖它以前的值,这几乎肯定是错误的,但没关系,我想这将是您下一个stackoverflow问题的主题,)但是您假设
width
将大于0,因此您假设您的循环将至少执行一个。编译器很乐意向您指出,以您的名义做出这样的假设是不安全的。

您声明了一个变量,但没有为它赋值:

Color p;
所以它的默认值是
null
。然后,您可以根据编译时未知的运行时条件为其分配:

for (x = 0; x < width; x++)
    p = bmp.GetPixel(x, y);

编译器会告诉您它可能没有被赋值。因此出现了这样的消息。

您声明了一个变量,但不为其赋值:

Color p;
所以它的默认值是
null
。然后,您可以根据编译时未知的运行时条件为其分配:

for (x = 0; x < width; x++)
    p = bmp.GetPixel(x, y);

编译器会告诉您它可能没有被赋值。因此,如果
x
为false,则
p=bmp.GetPixel(x,y)
可能永远不会运行,从而使
p
未分配。您可以将分配更改为
Color p=default(Color)
。如果
x
为false,则
p=bmp.GetPixel(x,y)
可能永远不会运行,而保持
p
未分配。您可以将分配更改为
Color p=default(Color)