非常简单的c#访问修饰符VS形式
本文关键字:VS 形式 访问 简单 非常 | 更新日期: 2023-09-27 18:27:01
这是针对VS中的windows窗体的。它表示"newpic"在当前上下文中不存在。我尝试使用load事件处理程序方法或您调用的任何方法将访问修饰符设置为public。但这对我没有帮助。我确信这是我不知道的简单事情。如果我能把"newpic"保存在"资源"中,那就太好了
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 flexland
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
Bitmap bmp=new Bitmap(Properties.Resources.pic1in);
Bitmap newpic = new Bitmap(bmp);
int width=bmp.Width;
int height=bmp.Height;
Color c=default(Color);
byte t=10;
byte t2=10;
byte rc=50;
byte gc=50;
byte bc=50;
byte rs=10;
byte gs=10;
byte bs=10;
byte rc2=100;
byte gc2=100;
byte bc2=100;
byte rs2=100;
byte gs2=100;
byte bs2=100;
byte rs3=200;
byte gs3=200;
byte bs3=200;
for(int y=0; y<height; y++)
{
for(int x=0; x<width; x++)
{
c=bmp.GetPixel(x, y);
byte a=c.A;
byte r=c.R;
byte g=c.G;
byte b=c.B;
int rl=rc-t;
int rh=rc+t;
int gl=gc-t;
int gh=gc+t;
int bl=bc-t;
int bh=bc+t;
int rl2=rc2-t2;
int rh2=rc2+t2;
int gl2=gc2-t2;
int gh2=gc2+t2;
int bl2=bc2-t2;
int bh2=bc2+t2;
if((r>rl&&r<rh)&&(r>rl&&r<rh)&&(b>bl&&b<bh))
{
newpic.SetPixel(x, y, Color.FromArgb(a, rs, gs, bs));
}
else if((r>rl&&r<rh)&&(r>rl&&r<rh)&&(b>bl&&b<bh))
{
newpic.SetPixel(x, y, Color.FromArgb(a, rs2, gs2, bs2));
}
else
{
newpic.SetPixel(x, y, Color.FromArgb(a, rs3, gs3, bs3));
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
ictureBox1.Image =newpic; ////******PROBLEM*****newpic red underline
}
}
}
您应该将变量newpic
定义为Form1
实例本身的变量,而不是事件Form1_Load
的变量,从而使button1_Click
事件可以访问该变量
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 flexland
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Bitmap newpic; //Put newpic here!
public void Form1_Load(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(Properties.Resources.pic1in);
newpic = new Bitmap(bmp); //don't declare newpic here!
.... //all other initializations
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = newpic; //It should fix the problem
}
}
}