Loading Image into Memory for manipulation

martynball

Member
Joined
Jul 8, 2014
Messages
14
Programming Experience
Beginner
Hey, new to C#, going try to teach myself as I go. Now i'm trying to load an image into memory, all i'm getting on Google is:

C#:
Image myimg = Image.FromFile("image location");

Along them lines, obviously gotta define it as an Image object and then load the data into it, but Visual Studio is saying that the first word needs arguments.

Give me a hand here guys?

Code:
C#:
Public Class Form1


    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Image myimg = Image.FromFile("C:\Users\Martyn Ball\Desktop\10502092_657871814300163_3690168410002388536_n.jpg");
    End Sub
End Class

Errors:
C#:
Error	1	'Image' is a type and cannot be used as an expression.	c:\users\martyn ball\documents\visual studio 2013\Projects\JamSnaps\JamSnaps\Form1.vb	4	9	JamSnaps

Error	2	Method arguments must be enclosed in parentheses.	c:\users\martyn ball\documents\visual studio 2013\Projects\JamSnaps\JamSnaps\Form1.vb	4	15	JamSnaps

Error	3	'myimg' is not declared. It may be inaccessible due to its protection level.	c:\users\martyn ball\documents\visual studio 2013\Projects\JamSnaps\JamSnaps\Form1.vb	4	15	JamSnaps
Error	4	Character is not valid.	c:\users\martyn ball\documents\visual studio 2013\Projects\JamSnaps\JamSnaps\Form1.vb	4	120	JamSnaps
 
Last edited:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,273
Location
Sydney, Australia
Programming Experience
10+
Visual Studio is saying that the first word needs arguments.
No it isn't. Please provide the actual error message rather than a vague interpretation because specifics are important. The only issue I can see with the `Image` type there is that you haven't imported the namespace that it's a member of. The MSDN documentation has a topic for every type and it will tell you what assembly the tye is declared in and what namespace it's a member of.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,273
Location
Sydney, Australia
Programming Experience
10+
This is a C# forum, for questions on C# programming. This:
Image myimg = Image.FromFile("C:\Users\Martyn Ball\Desktop\10502092_657871814300163_3690168410002388536_n.jpg");
is C# code (almost anyway). This:
C#:
Public Class Form1


    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    End Sub
End Class
is VB code. Are you trying to code in VB or C#?
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,273
Location
Sydney, Australia
Programming Experience
10+
Trying to code in C#, it seems to selected the wrong project to start!

I think that it's safe to say that it was actually YOU who selected the wrong project, not IT. :) What version and edition of VS are you using? It's worth noting that in 2010 and earlier, VB Express and C# Express were separate but, from 2012, VS Express for Windows Desktop supports both VB and C#.

Not that I said that this:
Image myimg = Image.FromFile("C:\Users\Martyn Ball\Desktop\10502092_657871814300163_3690168410002388536_n.jpg");
was almost C# code because, in C#, the backslash is an escape character. As such you must either escape it:
Image myimg = Image.FromFile("C:\\Users\\Martyn Ball\\Desktop\\10502092_657871814300163_3690168410002388536_n.jpg");
or, preferably in the case of paths, use a verbatim string literal:
Image myimg = Image.FromFile(@"C:\Users\Martyn Ball\Desktop\10502092_657871814300163_3690168410002388536_n.jpg");
The @ indicates that all backslashes are considered literal characters and not escape characters.
 

martynball

Member
Joined
Jul 8, 2014
Messages
14
Programming Experience
Beginner
I think that it's safe to say that it was actually YOU who selected the wrong project, not IT. :) What version and edition of VS are you using? It's worth noting that in 2010 and earlier, VB Express and C# Express were separate but, from 2012, VS Express for Windows Desktop supports both VB and C#.

Not that I said that this:
Image myimg = Image.FromFile("C:\Users\Martyn Ball\Desktop\10502092_657871814300163_3690168410002388536_n.jpg");
was almost C# code because, in C#, the backslash is an escape character. As such you must either escape it:
Image myimg = Image.FromFile("C:\\Users\\Martyn Ball\\Desktop\\10502092_657871814300163_3690168410002388536_n.jpg");
or, preferably in the case of paths, use a verbatim string literal:
Image myimg = Image.FromFile(@"C:\Users\Martyn Ball\Desktop\10502092_657871814300163_3690168410002388536_n.jpg");
The @ indicates that all backslashes are considered literal characters and not escape characters.

My bad, I meant to put that "I" started the wrong project, and thanks! :)
 
Top Bottom