Posted By: malti | Jan 11th @ 11:06 PM
page 1 of 1
Comments: 5 | Views: 668
We are getting a Run-Time Error ::

Picturebox of a form is inaccessible by a class due to Protection Level Fault within a Same Application.......


Cud u juz help us to eliminate this error................


Our Code ::

class--Operator.cs

 

 

using System;

using System.Drawing;

using System.Drawing.Imaging;

using System.Windows.Forms;

using System.Collections.Generic;

using System.Text;

 

namespace WindowsApplication7

{

    public class Operator

    {

 

        private Bitmap bmpimg;

        public Operator()

        {

       

        }

        public void setImage(Bitmap bmp)

        {

           bmpimg = (Bitmap)bmp.Clone();

          //  PictureBox1.Image(bmpimg);   

        }

        public Bitmap getImage()

        {

            return (Bitmap)bmpimg.Clone();

          //  bmpimg = Form1.pb_color.Image;

        }

 

        public void calcHisto(Form1 form)

        {

           

            BitmapData data= bmpimg.LockBits(new System.Drawing.Rectangle(0,0,bmpimg.Width,bmpimg.Height),ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb);

          

            unsafe

            {

                byte* ptr= (byte*)data.Scan0;

 

                int remain=data.Stride - data.Width*3;

                int[] histogram =new int[256];

 

                for(int i=0;i<histogram.Length;i++)

                {

                    histogram[i]=0;

                }

 

                for(int i=0;i<data.Height;i++)

                {

                    for(int j=0;j<data.Width;j++)

                    {

                        int mean=ptr[0]+ptr[1]+ptr[2];

                        mean=mean/3;

                        histogram[mean]++;

                        ptr +=3;

                    }

                    ptr +=remain;

                }

                drawHistogram(histogram , form);

            }

            bmpimg.UnlockBits(data);

        }

 

        public void drawHistogram (int []histogram , Form1 form)

        {

            Bitmap bmp = new Bitmap (histogram.Length+10, 310);

            form.pb_color.Image = bmp;

            BitmapData data = bmpimg.LockBits(new System.Drawing.Rectangle(0, 0, bmpimg.Width, bmpimg.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

 

            unsafe

            {

                byte* ptr = (byte*)data.Scan0;

 

                int remain = data.Stride - data.Width * 3;

        

             

        

                for (int i = 0; i < data.Height; i++)

                {

                    for (int j = 0; j < data.Width; j++)

                    {

                        ptr[0] = ptr[1] = ptr[2] = 150;

                        ptr += 3;

                    }

                    ptr += remain;

                }

                int max = 0;

 

                for (int i = 0; i < histogram.Length; i++)

                {

                    if (max < histogram[i])

                        max = histogram[i];

                }

 

                for (int i = 0; i < histogram.Length; i++)

                {

                    ptr = (byte*)data.Scan0;

                    ptr += data.Stride * (305) + (i + 5) * 3;

 

                    int length = (int)(1.0 * histogram[i] * 300 / max);

 

                    for (int j = 0; j < length; j++)

                    {

                        ptr[0] = 255;

                        ptr[1] = ptr[2] = 0;

                        ptr -= data.Stride;

                    }

                }

            }

            bmpimg.UnlockBits(data);

        }

        }

    

}

 

 

 

 

form-------------Form1

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

 

namespace WindowsApplication7

{

    public partial class Form1 : Form

    {

 

        private Operator processing;

        public Form1()

        {

            InitializeComponent();

            processing = new Operator();  

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            processingToolStripMenuItem.Enabled = false;

            saveToolStripMenuItem.Enabled = false;

        }

 

        private void grayScaleToolStripMenuItem_Click(object sender, EventArgs e)

        {

            Bitmap grays = (Bitmap)pb_color.Image;

            int width = grays.Size.Width;

            int height = grays.Size.Height;

           

            for (int j = 0; j < height; j++)

            {

                for (int i = 0; i < width; i++)

                {

                    Color col;

                    col = grays.GetPixel(i, j);

                    int A = (col.R + col.G + col.B )/ 3;

                    grays.SetPixel(i, j, Color.FromArgb(A, A, A));

                }

            }

            pb_color.Image = grays;

            MessageBox.Show("PROCESSING COMPLETE!!");

            saveToolStripMenuItem.Enabled = true;

        }

 

        private void exitToolStripMenuItem1_Click(object sender, EventArgs e)

        {

            this.Close();

        }  

 

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)

        {

            Bitmap b = (Bitmap)pb_color.Image;

            SaveFileDialog save = new SaveFileDialog();

            save.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";

            if (save.ShowDialog() == DialogResult.OK)

            {

                b.Save(save.FileName);

            }

 

        }

 

        private void binaryToolStripMenuItem_Click(object sender, EventArgs e)

        {

            int threshold = 220;

           

 

            Bitmap binary = (Bitmap)pb_color.Image;

            int width = binary.Size.Width;

            int height = binary.Size.Height;

            for (int j = 0; j < height; j++)

            {

                for (int i = 0; i < width; i++)

                {

                    Color col;

                    col = binary.GetPixel(i, j);

                    int A = (col.R + col.G + col.B) / 3;

                    if (A <= threshold)

                    {

                        binary.SetPixel(i, j, Color.FromArgb(0,0,0));

                    }

                    else

                    {

                        binary.SetPixel(i, j, Color.FromArgb(255,255,255));

                    }

                }

            }

            pb_color.Image = binary;

            MessageBox.Show("PROCESSING COMPLETE !!");

            saveToolStripMenuItem.Enabled = true;

 

        }

 

        private void openToolStripMenuItem_Click(object sender, EventArgs e)

        {

 

            OpenFileDialog open = new OpenFileDialog();

                                                open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";

            if (open.ShowDialog() == DialogResult.OK)

            {

                pb_color.Image = new Bitmap(open.FileName);

            }

            processingToolStripMenuItem.Enabled = true;

 

            /*OpenFileDialog filechooser = new OpenFileDialog();

            filechooser.RestoreDirectory = true;

 

            if (filechooser.ShowDialog() == DialogResult.OK)

            {

                pb_color.Image = new Bitmap(new Bitmap(filechooser.FileName), 300, 300);

                this.Invalidate();

            }

            this.processing.setImage(new Bitmap(this.pb_color.Image));

            this.Invalidate();*/

           

        }

 

 

        private void button1_Click_1(object sender, EventArgs e)

        {

            Form2 frm2 = new Form2();

            frm2.Show();

        }

 

        private void textBox1_TextChanged(object sender, EventArgs e)

          {

            }

 

        private void pb_color_Click(object sender, EventArgs e)

        {

 

        }      

 

        private void histogramToolStripMenuItem_Click(object sender, EventArgs e)

        {

            this.processing.calcHisto(this);

            this.pictureBox1.Image = this.processing.getImage();

            this.Invalidate();

        }

 

        private void processingToolStripMenuItem_Click(object sender, EventArgs e)

        {

 

        }

 

     }

}

// Offender suspect from Operator.drawHistogram()

form.pb_color.Image = bmp;

I'd advise remove all reference to Form1 in Operator class before debugging...

P.S.: Something deep inside me have a feeling that naming a class with a keyword is asking for trouble in the future...
Operator is not a keyword, operator is
True (otherwise C# won't let him compile it) but it IS a keyword in case insensitive languages like VB.NET. (That's the reason I said "in the future")

Someone writing programs that add reference to your module will probably thank him for this... and I suspect there's other similar poorly chosen names hiding in the modules...

Yes, but C# is compiled to IL (like other managed languages does too), it's not like you'll use the C# source file directly in VB, you'll use the compiled assembly, in which the "operators" are converted to methods with op_%special_name% name format (and I don't imagine the compiler teams will change the name it in such a way that it will break if you use Operator), and thats what languages like VB or others use. Just saying.... Smiley

I don't know about it. Thanks for telling me. (I've learnt to evade from names that I believe to be error_prone when coding, so haven't got chance to see it before.)

But this doesn't prevent the name be a poor choice, does it? Wink

page 1 of 1
Comments: 5 | Views: 668
Microsoft Communities