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)'

Nga Đỗ
  • 29
  • 7
  • `imgToByteArray` method is a non static method, you can't access without creating an instance of a class. Try new Program().imgToByteArray(...);` – Hari Prasad May 18 '16 at 03:02
  • also please tell me how i can display this byte array on screen – Idrees Mughal May 18 '16 at 03:03
  • Possible duplicate of [Error: "an object reference is required for the non-static field, method or property..."](http://stackoverflow.com/questions/2505181/error-an-object-reference-is-required-for-the-non-static-field-method-or-prop) –  May 18 '16 at 03:10

2 Answers2

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);
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
  • I don't think that suggesting option 2 for a coder who has to ask a question like that is a good idea – Peuczynski May 18 '16 at 05:29
  • @Peuczyński It is good to aware of all possible options :-), anyways thanks for suggestion. – Hari Prasad May 18 '16 at 05:40
  • 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 – Peuczynski May 18 '16 at 09:03
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.

Peuczynski
  • 4,591
  • 1
  • 19
  • 33