none
How to create a property for an image? RRS feed

  • Question

  • Hi, i am new with C#.

    I created some filters for image processing operations. Then, i moved my filters to a class. And my filters are working. But i think i can't use property (which i created for my image)  properly. And i need some support. How should use them?

    This is from Form1.cs 

         private void sliderKernel_MouseUp(object sender, MouseEventArgs e)
            {
                Filtreler f1 = new Filtreler();
    
                Bitmap Orj = new Bitmap(pBox_SOURCE.Image);
    
                f1.Imge = Orj;
    
                int SablonBoyutu = 
                int.Parse(sliderKernel.Value.ToString());
    
                
                if (SablonBoyutu % 2 == 1)
                {
                    f1.addnoise(f1.Imge);
                    pictureBoxNoisyImg.Image = f1.Imge;
                    Bitmap MeanImg = (Bitmap)f1.Imge.Clone();
                    f1.meanfilter(SablonBoyutu, MeanImg);
                    pBox_PROCESSED.Image = MeanImg;
                }
    
                else
                {
                //doing something 
                }
            }

    and this part is from my class file (i think this is the weird part)

      #region imge property
             
            public Bitmap Imge { get; set;}
           
            #endregion 

    and this is my addnoise() method

     #region Gürültü Ekleme
            public void addnoise(Bitmap GirisResmi)
            {
                int ResimGenisligi = Imge.Width;
                int ResimYuksekligi = Imge.Height;
                Random rand = new Random();
                int randpixelr, randpixelg, randpixelb;
                for (int i = 0; i < ResimGenisligi; i++)
                {
                    for (int j = 0; j < ResimYuksekligi; j++)
                    {
                        if ((i * i + j) % 100 == 0) 
                        {
                            randpixelr = rand.Next(0, 255);
                            randpixelg = rand.Next(0, 255);
                            randpixelb = rand.Next(0, 255);
    
    GirisResmi.SetPixel(i, j,Color.FromArgb(randpixelr, randpixelg, randpixelb));
                        }
                    }
                }
                return;
            }
            #endregion 





    Wednesday, July 24, 2019 2:12 PM

All replies

  • The code there all looks OK.  What isn't working?  Are you getting an error?

    I don't quite understand why you would store the bitmap into the class, if you then have to pass the bitmap into addnoise.  Why not have addnoise use the bitmap already stored in "resim"?

    This line seems silly:

     int SablonBoyutu = int.Parse(sliderKernel.Value.ToString());

    Why would you convert the integer to a string, and then convert it back to integer?  Just do:

        int SablonBoyutu = sliderKernel.Value;


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Wednesday, July 24, 2019 10:15 PM
  • Hi MustafaUysal, 

    Thank you for posting here.

    Based on your code, your use of property is correct.

    The only thing you need to pay attention is the name of property:

            private Bitmap img;
            public Bitmap Img
            {
                get { return img; }
                set { img = value; }
            }

    Besides, you can use auto-implemented properties which make property-declaration more concise when no additional logic is required in the property accessors in C#3.0 and later.

            public Bitmap Imge1 { get; set; }

    you can refer the following document for more information about properties:

    Using Properties (C# Programming Guide)

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, July 25, 2019 6:50 AM
    Moderator
  • The code there all looks OK.  What isn't working?  Are you getting an error?

    I don't quite understand why you would store the bitmap into the class, if you then have to pass the bitmap into addnoise.  Why not have addnoise use the bitmap already stored in "resim"?



    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    No i don't get anny error.

    Actually i'm trying to use bitmap stored in "resim". But i don't know how to do that. i would be appreciated if you show me how to do.

    Saturday, July 27, 2019 9:00 AM
  • Hi MustafaUysal,

    Thanks for your feedback.

    I note that 'resim' in your code is a property's name.

    >> I'm trying to use bitmap stored in "resim"

    What does it mean? Could you describe it clearly about your problem?

    It will help us to make a test.

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Monday, July 29, 2019 9:58 AM
    Moderator
  • Hi MustafaUysal,

    Thanks for your feedback.

    I note that 'resim' in your code is a property's name.

    >> I'm trying to use bitmap stored in "resim"

    What does it mean? Could you describe it clearly about your problem?

    It will help us to make a test.

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Thank you,

    i edited my code. I don't get any error. But i don't know whether my code is regular or not. Because i am using Properties for first time. Please take a look.

    Best regards.
    Monday, July 29, 2019 6:07 PM
  • > Actually I'm trying to use bitmap stored in "resim".

    When you create an auto property like

        public Bitmap Imge { get; set; }

    the compiler automatically creates a hidden bitmap variable for you.  You don't need to store it in resim, instead just replace those instances with Imge.

    If you really want to keep it as resim for some reason, then you just need

        public Bitmap Imge {
            get { return resim; }
            put { resim = value; }
       }


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Monday, July 29, 2019 7:51 PM