Automatically merge small images in ASP. NET and display them with CSS Sprite.

Source: Internet
Author: User
Document directory
  • 1. Create a folder named app_sprites in the program
  • 2. How to make these files automatically generated
  • 3. How to Use

A few days ago, the ms asp. NET team launched a small component: Sprite and image Optimization Framework, which is used to generate sprite CSS and generate a large image.

: Http://aspnet.codeplex.com/releases/view/50140

8189e6b8-fbe4-4f01-8f9f-5687c0ea9f59

The downloaded project contains a class library, an ASP. NET webform example, and an ASP. net mvc example.

The following is an example of ASP. net mvc.

 

The marked image in the figure is from a whole image.

~ \ App_sprites \ categories \ sprite0.png

~ \ App_sprites \ popular \ sprite0.png

The entire image is automatically merged by Sprite and image optimization framework with a few small images and generates CSS similar to the following

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

 

Let's take a look at how we use this tool in the program.

1. Create a folder named app_sprites in the program

The image files in each subdirectory of the created subdirectory are automatically merged into a large file and CSS is generated.

Figure

FileSmall image files

Sprite0.pngFor the automatically generated large image,Highcompat.css/lowcompat.cssFor automatically generated CSS files

Note: These two cssfiles are not the same, but in the future, lowcompat.css may only support mainstream IE7-9, FF browsers, highcompat will support other

Settings. xmlFor the configuration file:

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

Where

FileformatLarge image type

Based64encoding indicates whether the image address is base64 encoded. If base64 encoding is used, the generated CSS may be as follows:

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

QualityTo generate Image Quality

BackgroundcolorIs the background color, transparent by default

MaxsizeMaximum image size

2. How to make these files automatically generated

This is done by adding modules, that is, when the page is accessed for the first time, it will automatically generate a large image and CSS

Add

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

Or iis7 or above is added on the system. webserver Node

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

Can achieve the effect

3. How to Use

This is done in two steps.

First, reference the generated CSS

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

It is OK to specify the corresponding folder.

Generate

    <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" />

Or use the link label of highcompat.css.

Second, how to use a raw image

If you want to use the IMG label

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

You can use it like this and generate a tag similar to the following.


If you want to directly use CSS for reference, you can use the specified class to specify the sprite background image.

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

The large image is displayed on the background of the label, and the displacement of the small image to be displayed is automatically set.

Categories_windowsLogo-png

Indicates to display ~ /App_sprites/categories/windowslogo.png

 

Attach an original image

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.