在ASP.NET中自动合并小图片并使用CSS Sprite显示出来

前几天MS的ASP.NET小组推出了一个小组件:Sprite and Image Optimization Framework,用于生成Sprite CSS,及将小的图片生成一副大图。

下载地址:http://aspnet.codeplex.com/releases/view/50140

8189E6B8-FBE4-4F01-8F9F-5687C0EA9F59

下载后工程中有一个类库,一个ASP.NET WebForm示例以及一个ASP.NET MVC示例。

下面我们就看看ASP.NET MVC的示例

在ASP.NET中自动合并小图片并使用CSS Sprite显示出来_第1张图片

 

图中所标记的图片都是来源于一个整张的图片

~\App_Sprites\categories\sprite0.png

~\App_Sprites\popular\sprite0.png

而这个整张图片则是由Sprite and Image Optimization Framework自动以几个小图片合并并生成类似下面的CSS

.popular_azureLogo-png{width:32px;height:30px;background-image:url(sprite0.png);background-position:-0px 0;}

 

我们下面看看我们怎么在程序中使用这个工具吧

1.在程序中建立一个名为App_Sprites的文件夹

在其中建立子目录每一个子目录中的图片文件将会自动合并成一个大文件,并且生成CSS

在ASP.NET中自动合并小图片并使用CSS Sprite显示出来_第2张图片

图中

框中的文件为小的图片文件

sprite0.png为自动生成后的大图片、highCompat.css/lowCompat.css为自动生成的CSS文件

注:这两个CSS文件暂没有不同,但未来lowCompat.css可能只支持IE7-9、FF的主流浏览器,highCompat将支持其它

settings.xml为配置文件:

<?xml version="1.0" encoding="utf-8"?>
<ImageOptimizationSettings>
<FileFormat>png</FileFormat>
<Base64Encoding>false</Base64Encoding>
<Quality>80</Quality>
<BackgroundColor>00000000</BackgroundColor>
<MaxSize>500</MaxSize>
</ImageOptimizationSettings>

其中

FileFormat为大图片类型

Based64Encoding为图片地址是否使用Base64编码,如果使用则生成的CSS可能如下:

.categories_azureLogo-png{
width:16px;height:16px;
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAALGOfPtRkwAAACBjSFJNAACHDwAAjA8AAP1SAACBQAAAfXkAAOmLAAA85QAAGcxzPIV3AAAKOWlDQ1BQaG90b3Nob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w2pFSt/IQrNvPxq1fXp+4Z+BRtGZHSEFeKewm+JFJp5FTYxhT0+GVvsIkwDcZTaYmjwsvgRQXk3Q8EaUt4oPjPxd2FvAu8MJ1HrYxkHWgUN3VMaHFIqM9U3XoYiexLFF+7CElTtTZyclwGocbPyZT+4UfSTAMPvKtaTXWkQM/l/gqahTqih4mKnhvzLEj6+GAEt1YE6t5iTASi7NGVdbXGmtvUGpR3Xon+uJTfWDaswkx7lRCnt/9tkr/EKQ+EJAZ9diSgNzvDy/9a59sbk4hae5ZIbSwh5D907B8fuzVc7JBiuyjBevoei7igK2jmsaBzFl7bAuZwXX6rU6/UvP5GJ7/zSIkrhR7QXllZOGam5Pf1gmy0C02keJR3eRxu6AkMNLvG15/T63Ozs7AVPDdUXQ9EFh8elV9IxpliXkF0baU8+OCaoeRSeJ4r+Bdt2dOD4z+iLAAAAAElFTkSuQmCC) no
-repeat 0% 0%
;}

Quality为生成图片质量

BackgroundColor为背景色,默认透明

MaxSize为最大图片大小

2.如何让这这些文件自动生成的

这个其中是靠添加Modules来完成的,也就是首次访问页面时就会自动生成大图片以及CSS

在System.Web节点上添加

    <httpModules>
      <add type ="Microsoft.Samples.Web.ImageOptimizationModule" name ="Microsoft.Samples.Web.ImageOptimizationModule"/>
    </httpModules>

或IIS7以上在System.WebServer节点上添加

    <modules runAllManagedModulesForAllRequests="true">
      <add type="Microsoft.Samples.Web.ImageOptimizationModule" name ="Microsoft.Samples.Web.ImageOptimizationModule"/>
    </modules>

都可以达到效果

3.如何使用

这个分两步走

第一,引用生成的CSS

    <%: Microsoft.Samples.Web.ImageSprite.ImportStylesheet("~/App_Sprites/categories/") %>    
<%: Microsoft.Samples.Web.ImageSprite.ImportStylesheet("~/App_Sprites/popular/") %>

指定到相应的文件夹就OK了

最终会根据访问者的浏览器,生成

    <link href="App_Sprites/categories/lowCompat.css" media="all" rel="stylesheet" type="text/css" />    
<link href="App_Sprites/popular/lowCompat.css" media="all" rel="stylesheet" type="text/css" />

或使用highCompat.css的link标签

第二,如何使用某生图片

如果要用img标签的话

<%: Microsoft.Samples.Web.ImageSprite.Image("~/App_Sprites/popular/windowsLogo.png" ) %>

类似这样去用就可以了,会生成类似下面的标签

<img class="popular_mesh-png" src="data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" />

如果要直接使用CSS去引用,则可以使用指定的Class来规定Sprite的背景图片

<div class="categories_windowsLogo-png">windows</div> 

则会在此标签的背景上显示大图片,并自动设定到要显示小图片的位移。

categories_windowsLogo-png

则表示要显示~/APP_Sprites/categories/windowsLogo.png

 

附原图一张

在ASP.NET中自动合并小图片并使用CSS Sprite显示出来_第3张图片


你可能感兴趣的:(在ASP.NET中自动合并小图片并使用CSS Sprite显示出来)