2015-12-16 5 views
0

Это для формы окна в VS. В нем говорится, что «newpic» не существует в текущем контексте. Я попытался установить модификатор доступа для общего доступа с помощью метода обработчика событий загрузки или того, что вы его называете. Но это не для меня. Я уверен, что это что-то простое, о чем я не знаю. Было бы замечательно, если бы я мог получить «newpic» должны быть сохранены в «ресурсы»Очень простые модификаторы доступа C# VS form

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 
     } 
    } 
} 
+0

Его определяют в форме метод загрузки, а не на уровне класса. он не существует вне метода, который вы его определяете (или, точнее, вне '' и '}' скобок, которые вы его определяете). –

ответ

1

Вы должны сделать переменную newpic доступной для button1_Click события, определив его как переменную самого Form1 экземпляра, а не переменная события Form1_Load

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 
     }  
    } 
} 
Смежные вопросы