将图像加载到文件流

发布于 2024-09-10 22:17:05 字数 181 浏览 1 评论 0原文

我正在使用加载图像

OpenFileDialog open = new OpenFileDialog();

选择文件后,“打开”中填充了多个项目,包括路径。

现在我想将文件加载到文件流(或类似的东西)中以通过网络服务发送......这可能吗?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

兔姬 2024-09-17 22:17:05

您可以使用 FileStream 打开该文件:

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

然后,您可以将其传递到 Web 服务 http 上下文 Response .OutputStream 属性。您仍然需要设置正确的 mime 类型和各种标头,但这效果很好:

HttpContext.Current.Response.OutputStream = file;

话虽如此,从 Web 服务(或 Web 应用程序)发送文件的最简单方法是使用 Response.WriteFile 方法:

Response.WriteFile("Path To File");

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");
三五鸿雁 2024-09-17 22:17:05

试试这个:

byte[] buff = System.IO.File.ReadAllBytes(open.FileName);
System.IO.MemoryStream ms = new System.IO.MemoryStream(buff);

try this:

byte[] buff = System.IO.File.ReadAllBytes(open.FileName);
System.IO.MemoryStream ms = new System.IO.MemoryStream(buff);
記憶穿過時間隧道 2024-09-17 22:17:05

是的,可以创建图像

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

或放入流中

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

,但是应该发送热图像,这由您决定

sendToWs(img)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文