How to load an image in C#

I browsed through some online tutorials.

Lets say I have an image with the filepath images/pic.gif. How do I tell C# to load the image?

Is it:


[font=Courier New]System.Drawing.Graphics.DrawImage("images/pic.gif" );[/font]
 

Or something else?

This link ought to help:

http://www.codeproject.com/cs/media/quickview.asp

[Wow. I just tried to answer my first C# question!!! … ]

This is it in VB.NET


Dim loadImage As System.Drawing.Image
loadImage = loadImage.FromFile(Full Physical Path)

:slight_smile:

Wow, that picture of the guy that wrote that article is a lil scary…

Yo yo

ha det så trevligt!

lil? you have a strange definition of this word “lil” :wink:

Umm, I already looked at that article, it didn’t really help. I don’t actually want o draw anything, I just want to load an image from my harddrive.

The line I supplied will do it, I’ll post some code to send it to a browser, I’ll even write it in c#, just for you! :stuck_out_tongue:

Second time posting this, I got disconnected last time!! Fecking dialup!
I wrote two ways for ya, and in C#… don’t say I’m not nice :smiley:
Fitst way simply loads a local file on the server:


System.Drawing.Image loadImg = System.Drawing.Image.FromFile("c:\\\\DSC01307.JPG");
loadImg.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

Simply replace the “c:\\DSC01307.JPG” is the phyiscal path of an image on the server.

The second way allows you to supply and image using a HtmlInputFile, allowing you to send a file from your PC to the server.

Heres the .aspx code first


<form id="WebForm2" method="post" runat="server" enctype="multipart/form-data">
 <INPUT id="File2" type="file" name="File1" runat="server"><asp:Button id="Button1" runat="server" Text="Button></asp:Button>
</form>

Now the C#


protected System.Web.UI.HtmlControls.HtmlInputFile File2;
protected System.Web.UI.WebControls.Button Button1;
 
private void InitializeComponent()
 {	
  this.Button1.Click += new System.EventHandler(this.Button1_Click);
 }
 
private void Button1_Click(object sender, System.EventArgs e)
 {
  HttpPostedFile MyFile = File2.PostedFile;
  System.Drawing.Image loadImg = System.Drawing.Image.FromStream(MyFile.InputStream);
  loadImg.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
 }

I used VS.NET, so there might be some C# your not going to need.
Hope thats what your looking for, if its not, let me know.

:slight_smile:

(hey hey, that was my second ever C#!)

System.Drawing.Image loadImg = System.Drawing.Image.FromFile(“c:\\DSC01307.JPG” ); loadImg.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

okay I integrated that code into my own but now I get a compilation error that says:

[font=Courier New]

[/font]
[size=1]c:\visualprojects\classlibrary1\class1.cs(14,17): error CS0246: The type or namespace name ‘Response’ could not be found (are you missing a using directive or an assembly reference?)

I added the system.drawing reference always and I’m using system.drawing.

What am I missing?
[/size]