GetImageStream
内容
 [
        
            隐藏
        ]
    GetImageStream()
返回图像流。
public Stream GetImageStream()
返回值
带有图像的流。
例子
以下示例显示了如何将图像保存到文件:
// 创建解析器类的实例
using (Parser parser = new Parser(filePath))
{
    // 从文档中提取图像
    IEnumerable<PageImageArea> images = parser.GetImages();
    
    // 检查是否支持图片提取
    if (images == null)
    {
        Console.WriteLine("Page images extraction isn't supported");
        return;
    }
    // 遍历图像
    foreach (PageImageArea image in images)
    {
        // 打开图像流
        using (Stream imageStream = image.GetImageStream())
        {
            // 创建文件保存图片
            using (Stream destStream = File.Create(Guid.NewGuid().ToString() + image.FileType.Extension))
            {
                byte[] buffer = new byte[4096];
                int readed = 0;
                do
                {
                    // 从图像流中读取数据
                    readed = imageStream.Read(buffer, 0, buffer.Length);
                    if (readed > 0)
                    {
                        // 向文件流中写入数据
                        destStream.Write(buffer, 0, readed);
                    }
                }
                while (readed > 0);
            }
        }
    }
}
也可以看看
- class PageImageArea
 - 命名空间 GroupDocs.Parser.Data
 - 部件 GroupDocs.Parser
 
GetImageStream(ImageOptions)
以不同的格式返回图像流。
public Stream GetImageStream(ImageOptions options)
| 范围 | 类型 | 描述 | 
|---|---|---|
| options | ImageOptions | 用于提取图像的选项。 | 
返回值
带有图像的流。
例子
以下示例显示了如何将图像保存到不同格式的文件中:
// 创建解析器类的实例
using (Parser parser = new Parser(filePath))
{
    // 从文档中提取图像
    IEnumerable<PageImageArea> images = parser.GetImages();
    
    // 检查是否支持图片提取
    if (images == null)
    {
        Console.WriteLine("Page images extraction isn't supported");
        return;
    }
    // 创建以 PNG 格式保存图像的选项
    ImageOptions options = new ImageOptions(ImageFormat.Png);
    
    // 遍历图像
    foreach (PageImageArea image in images)
    {
        // 打开图像流
        using (Stream imageStream = image.GetImageStream(options))
        {
            // 创建文件保存图片
            using (Stream destStream = File.Create(Guid.NewGuid().ToString() + ".png"))
            {
                byte[] buffer = new byte[4096];
                int readed = 0;
                do
                {
                    // 从图像流中读取数据
                    readed = imageStream.Read(buffer, 0, buffer.Length);
                    if (readed > 0)
                    {
                        // 向文件流中写入数据
                        destStream.Write(buffer, 0, readed);
                    }
                }
                while (readed > 0);
            }
        }
    }
}
也可以看看
- class ImageOptions
 - class PageImageArea
 - 命名空间 GroupDocs.Parser.Data
 - 部件 GroupDocs.Parser