1

I'm new to C#.

Basically, I'm trying to convert an image with byte type from database into a readable image format (displaying the byte as an actual Image). I've wrote the following code, but don't know how to call the class method.

public Image byteArrayToImage(byte[] byteArrayIn)
{
    MemoryStream ms = new MemoryStream(byteArrayIn);
    Image returnImage = Image.FromStream(ms);
    return returnImage;
}

And, I'm calling it like below but it has red lines under byteArrayToImage(ImageData);:

public Image Img = byteArrayToImage(ImageData);

Any idea how and why?

Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
azie
  • 11
  • 1
  • 1
    What is the error message you're getting with the red lines in visual studio? – Steven Lemmens May 22 '15 at 09:23
  • @StevenLemmens it has no error yet because the public Image Img = byteArrayToImage(ImageData); has syntax error, redlines underneath. – azie May 22 '15 at 09:25
  • 2
    it want's an instance of the class you are defining `byteArrayToImage` in - but you can make it `public static Image byteArrayToImage...` and only have to write the class-name instead – Random Dev May 22 '15 at 09:25
  • 1
    Are you serious? Well if the method is located in your current class and the class is not static you call it `this.byteArrayToImage(data)` But anyway, I guess there is a major lack of knowledge among the language you are using. – ckruczek May 22 '15 at 09:25
  • 1
    @azie it will, you will have a compiler error. Probably *A field initializer cannot reference the non-static field, method, or property* – Charles Mager May 22 '15 at 09:25
  • hover over that red line and you will see the compilation error. – Jenish Rabadiya May 22 '15 at 09:27
  • @Carsten you mean public static Image Img = byteArrayToImage(ImageData); ? it still has syntax errors – azie May 22 '15 at 09:27
  • possible duplicate of [A field initializer cannot reference the non-static field, method, or property](http://stackoverflow.com/questions/15204420/a-field-initializer-cannot-reference-the-non-static-field-method-or-property) – Charles Mager May 22 '15 at 09:28
  • make the byteArrayToImage function static! – Marc Wittmann May 22 '15 at 09:57

3 Answers3

0

You may want to mark you method as static

public class YourClass
{
    public static Image byteArrayToImage(byte[] byteArrayIn)
    {
     //Your code   
    }
}

Then call it this way outside of its class

public Image Img = YourClass.byteArrayToImage(ImageData);
Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
0

If your class is like this

public class MyClass
{
     //some variables, methods, etc.
     public Image byteArrayToImage(byte[] byteArrayIn)
     {
       MemoryStream ms = new MemoryStream(byteArrayIn);
       Image returnImage = Image.FromStream(ms);
       return returnImage;
     }
}

call it as follows,

public Image Img = new MyClass().byteArrayToImage(ImageData);

Why?

Because to call a non-static method of the class you need object/instance of that class.

Abhishek
  • 6,912
  • 14
  • 59
  • 85
0
  1. Maybe ImageData is not a byte[]
  2. You are calling a non static method in a global field initializer

It should be:

public Image byteArrayToImage(byte[] byteArrayIn)
{
    MemoryStream ms = new MemoryStream(byteArrayIn);
    Image returnImage = Image.FromStream(ms);
    return returnImage;
}

public void MyMethod()
{
    byte[] myData = <something>
    Image Img = byteArrayToImage(myData);
}