0

here is my program that i have written

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Data;
namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {

            Image img = Image.FromFile("C:\\images.JPG");
            byte[] bArr = imgToByteArray(img);

        }
        public byte[] imgToByteArray(System.Drawing.Image Imagein)
        {
            byte[] data = null;using (System.IO.MemoryStream ms = new MemoryStream())
            {
                Imagein.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                data = ms.ToArray();

            }
            return data;
        }
    }
}

now when i build the program it shows error

an object reference is required for the non static field, method or property 'Program.imgToByteArray(Image)'

3
  • imgToByteArray method is a non static method, you can't access without creating an instance of a class. Try new Program().imgToByteArray(...);` Commented May 18, 2016 at 3:02
  • also please tell me how i can display this byte array on screen Commented May 18, 2016 at 3:03
  • Possible duplicate of Error: "an object reference is required for the non-static field, method or property..." Commented May 18, 2016 at 3:10

2 Answers 2

1

The error is pretty clear, you can't access non static methods in a static context (method).

You have two options to fix this issue.

Option 1

Make your function/method a static function.

public static byte[] imgToByteArray(System.Drawing.Image Imagein)
{
   ...
}

Option 2:

Create an instance of Program and access the method.

new Program().imgToByteArray(img);

Since you want to print byte array in console (not sure why?) you could do something like this.

Console.WriteLine(string.Join(",", bytearray);
Sign up to request clarification or add additional context in comments.

3 Comments

I don't think that suggesting option 2 for a coder who has to ask a question like that is a good idea
@Peuczyński It is good to aware of all possible options :-), anyways thanks for suggestion.
I've made a seperate answer so people wouldn't follow this weird fetish you provided in option 2. Forgive me, but I disagree very much on this matter with you. Some beginner coders searching for answers would come here and see a case looking just like theirs (which is entry point of console app) and won't think about the design guidelines as CA1822 and may use option 2 which is what I am afraid of
0

Make the imgToByteArray method static. There is really no other rational option.

Regarding Option 2 from @Hari Prasad answer you considered an "possible option": You would be creating new class instance to call a member of this instance from a static class member that is the main entry point of the application which is pretty hardcore and given the code design guildelines i.e. https://msdn.microsoft.com/en-us/library/ms245046.aspx it is something you shouldn't.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.