65.9K
CodeProject 正在变化。 阅读更多。
Home

在 ASP.NET 中显示 .NET 图像编解码器

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1投票)

2002年11月8日

3分钟阅读

viewsIcon

68727

downloadIcon

147

使用 ASP.NET 和反射来显示服务器上可用的 ImageCodecInfo 对象。

Browser Output

引言

此示例演示了 System.Drawing.Imaging.ImageCodecInfo 类中的 GetImageEncoders()GetImageDecoders() 方法,并在两个动态生成的 HTML 表格中显示结果。

我最近在研究 System.Drawing.Imaging 命名空间的一些方面时想到了这个例子。我的目的是展示 .NET 中有哪些内置图像编解码器可用,但结果是一个动态 HTML 表格生成和使用 System.Reflection 命名空间的很好的例子。

背景

术语 *CODEC* 是“**co**mpression/**dec**ompression”(压缩/解压缩)的缩写,用于压缩和/或解压缩各种类型数据的模块。 正如您可能猜到的,图像编解码器是应用于数字图像的编解码器。.NET Framework 提供 ImageCodecInfo 类,以封装作为 System.Drawing.Imaging 命名空间一部分的此类模块。 Bitmap 类包含一个 Save() 方法,该方法接受 ImageCodecInfo 实例,并将当前位图以给定编解码器支持的格式保存。

目前,无法构建和添加客户编解码器模块到 .NET。提供了一些内置的压缩和解压缩模块,通常称为编码器和解码器,它们涵盖了最常见的文件格式。这些编解码器应该可以很好地满足您可能希望构建的大多数数字成像应用程序的需求。

使用代码

可以从本页面顶部的链接下载该项目。当然,您需要 Visual Studio .NET 或其他编译器,以及访问 Internet Information Server (IIS) 以充当服务器计算机。

要从 Visual Studio .NET 运行项目,请创建一个名为“ImageCodecs”的新 ASP.NET Web 应用程序项目。关闭新的解决方案,并将服务器上项目的目录(也称为“ImageCodecs”)替换为下载的 zip 文件的内容。在 Visual Studio .NET 中重新加载项目,您就可以开始了。

关注点

代码中有两个主要方法,每个编解码器类型一个。此处显示了加载编码器表的方法。解码器表的方法非常相似。

private void LoadEncoderTable()
{
    // Clear any existing rows
    EncoderTable.Rows.Clear();

    // Build header based on the properties in the ImageCodecInfo class
    Type t = typeof(ImageCodecInfo);
(1) PropertyInfo[] properties = t.GetProperties();

    HtmlTableRow headerRow = new HtmlTableRow();
(2) BuildHeaderRow(headerRow, properties);
    EncoderTable.Rows.Add(headerRow);

    // Load the set of encoders
    ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();

    // Add a row for each encoder to the table
    foreach (ImageCodecInfo info in encoders)
    {
        HtmlTableRow row = new HtmlTableRow();
(3)     BuildTableRow(row, properties, info);
        EncoderTable.Rows.Add(row);
    }
}

如您所见,(1) 使用 System.Reflection 命名空间中的 Type.GetProperties() 方法来检索 ImageCodecInfo 类类型的属性数组。此数组用于 (2) 生成表头和 (3) 为每个编码器生成一个表行。

BuildHeaderRow()BuildTableRow() 方法使用反射来枚举 ImageCodecInfo 类中的属性。 BuildTableRow() 方法稍微复杂一些,如下所示。

private void BuildTableRow(HtmlTableRow row, PropertyInfo[] properties, object obj)
{
    // Add a cell containing the value of each property in the given object
    foreach (PropertyInfo prop in properties)
    {
        // Create a new cell for the value
        HtmlTableCell cell = new HtmlTableCell();

        // Get the property value and display it as a string.
(4)     object propValue = prop.GetValue(obj, null);
        if (propValue != null)
            cell.InnerText = propValue.ToString();
        else
            cell.InnerText = "<none>";

        // Add the new cell to the row
        row.Cells.Add(cell);
    }
}

请注意使用 (4) GetValue() 方法来检索与当前 PropertyInfo 对象对应的属性的值。我们只是在表中显示此值的 ToString() 结果,除非该值为 null,在这种情况下,我们显示字符串“<none>”代替。

更多信息

.NET 中的 ImageCodeInfo 类基于 GDI+ 中的 ImageCodeInfo 类。.NET 版本的文档记录不是很广泛,但您可以从 Platform SDK 文档中学到很多东西。要获得有关此主题的更多信息,一个好的起点是 Platform SDK GDI+ 文档中的文章“使用图像编码器和解码器”。

Microsoft 还提供了 microsoft.public.dotnet.framework.drawing 新闻组,用于提出有关 .NET 绘图命名空间的问题和发表评论。

© . All rights reserved.