为System.Drawing.Image流C# [英] System.Drawing.Image to stream C#

查看:608
本文介绍了为System.Drawing.Image流C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的程序有一个为System.Drawing.Image 。该文件是不是在内存中被关押在文件系统上。我需要从它创建一个流。我怎么会去这样做呢?

I have a System.Drawing.Image in my program. The file is not on the file system it is being held in memory. I need to create a stream from it. How would I go about doing this?

推荐答案

请尝试以下操作:

public static Stream ToStream(this Image image, ImageFormat format) {
  var stream = new System.IO.MemoryStream();
  image.Save(stream, format);
  stream.Position = 0;
  return stream;
}

然后你可以使用以下内容:

Then you can use the following:

var stream = myImage.ToStream(ImageFormat.Gif);

替换任何格式适合您的方案GIF。

Replace GIF with whatever format is appropriate for your scenario.

这篇关于为System.Drawing.Image流C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆