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

3
  • Yes, it is possible. Any other questions?
    – Oded
    Jul 24, 2010 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: social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/… Jul 24, 2010 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, 2010 at 16:05

3 Answers 3

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");
2
  • What should I do if that "path to file" is an embedded BMP?
    – warmth
    Jan 9, 2014 at 17:30
  • @warmth - access the embedded file as a stream and pass that to the output stream.
    – Oded
    Jan 9, 2014 at 18:07
7

try this:

byte[] buff = System.IO.File.ReadAllBytes(open.FileName);
System.IO.MemoryStream ms = new System.IO.MemoryStream(buff);
2
  • is it possible to recreate the document from buff?
    – mouthpiec
    Jul 24, 2010 at 17:04
  • i managed to convert to bitmap by using the normal "new Bitmap()" constructor
    – mouthpiec
    Jul 24, 2010 at 17:31
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)

2
  • 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, 2010 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 Jul 24, 2010 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.