10

I am loading an image using

OpenFileDialog open = new OpenFileDialog();

After I select the file, "open" is populated with several items, including the path.

Now I would like to load the file into a filestream (or something similar) to be sent via a webservice... is this possible?

thanks

mouthpiec
  • 3,923
  • 18
  • 54
  • 73
  • Yes, it is possible. Any other questions? – Oded Jul 24 '10 at 16:02
  • At the risk of straining my psychic abilities, I suspect that mouthpiec might actually be looking for some hint as to how to load the file. If that were the case, they might want to look here: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/e2f183aa-307e-463d-b846-1ad6418848a3 – Steven Sudit Jul 24 '10 at 16:04
  • :) steven, i managed to load the file, eg I managed to load an image into a picture box, but I would like some code of how to load it onto a filestream – mouthpiec Jul 24 '10 at 16:05

3 Answers3

10

You can open the file with FileStream:

FileStream file = new FileStream("path to file", FileMode.Open);

You can then pass this through to the web service http context Response.OutputStream property. You will still need to set the correct mime type and various headers, but this works well:

HttpContext.Current.Response.OutputStream = file;

Having said that, the easiest way to send a file from a web service (or web app) is to use the Response.WriteFile method:

Response.WriteFile("Path To File");
Oded
  • 489,969
  • 99
  • 883
  • 1,009
7

try this:

byte[] buff = System.IO.File.ReadAllBytes(open.FileName);
System.IO.MemoryStream ms = new System.IO.MemoryStream(buff);
Arash
  • 203
  • 1
  • 8
2

Yes it is possible to create an image

var img = Image.FromFile(/*path*/);

or into a stream

var file = new FileStream("path to file", FileMode.Open);

But hot it should be send it is up to You to decide

sendToWs(img)

  • if the image will be a bitmap, will it be saved in a bitmap? or if it is a jpeg, fill it be saved in a jpeg, and so on? – mouthpiec Jul 24 '10 at 16:10
  • Image is an abstract class that provides functionality for the Bitmap and Metafile. For saving image You use the satatic method save Image.Save(path,format) where format is form ImageFormat – Damian Leszczyński - Vash Jul 24 '10 at 16:27