浅析用Base64编码的图片优化网页加载速度

   想必大家都知道网页加载的过程,从开始请求,到加载页面,开始解析和显示网页,遇到图片就再次向服务器发送请求,加载图片。如果图片很多的话,就会产生大量的http请求,从而影响页面的加载速度。所以现在有一种做法是将多张图片合并到一起,这样在打开页面的时候只需要一次http请求就可以加载多张图片,然后通过设置图片的背景偏移量来正确的显示。现在我们可以将图片转成base64编码,然后直接写在html页面或者css里面,这样在加载页面或者css的时候就可以直接将图片加载过去,这样也省去了设置图片背景偏移量带来的浏览器兼容性问题。

 

 一、在网页中显示base64编码的图片

1、在html中img标签中显示base64编码的图片 

 下面我们来看一下如何显示一张图片:

<img src="data:image/gif;base64,R0lGODlhDwAPAKECAAAAzMzM/////  
wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4ML  
wWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw=="  
alt="Base64 encoded image" width="150" height="150"/> 

 显示如下图:

Base64 encoded image

2、将base64编码的图片嵌入到css中

在css里面的实现如下:

.main {
    width: 600px;
    height: 300px;
    background-image:url(data:image/gif;base64,R0lGODlhCAAIAJEAAOnp6eTk5O7m8AAAACH5BAEAAAIALAAAAAAIAAgAAAINjAMJh2q6DnxOVsqmLQA7);
}

 二、将图片转化为base64字符串

   下面来说一下如何在C#中将图片转化成base64字符串,由于比较简单,这里只写一些主要的实现方法,大家可以自己画界面,做成一个工具。这里我新建一个控制台的项目。编写如下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConvertImgToBase64
{
   class Program
   {
      static void Main(string[] args)
      {
         FileInfo file = new FileInfo("1.png");
         var stream = file.OpenRead();
         byte[] buffer = new byte[file.Length];
         //读取图片字节流
         stream.Read(buffer, 0, Convert.ToInt32(file.Length));
         //将base64字符串保存到base64.txt文件中
         StreamWriter sw = new StreamWriter("base64.txt",false, Encoding.UTF8);
         //将字节流转化成base64字符串
         sw.Write(Convert.ToBase64String(buffer));
         sw.Close();
         Console.WriteLine("Convert successful!");
         Console.Read();
      }
   }
}

 然后我们打开base64.txt文件,讲里面的代码复制到img标签里面,代码如下:

<!DOCTYPE html>
<html>
<head>
    <title>Before | After</title>
    <meta charset="UTF-8">
</head>
<style>
.main {
    width: 200px;
    height: 100px;
    background-image:url(data:image/gif;base64,R0lGODlhCAAIAJEAAOnp6eTk5O7m8AAAACH5BAEAAAIALAAAAAAIAAgAAAINjAMJh2q6DnxOVsqmLQA7);
}
</style>
<body>
<div class="main"></div>
<img src="data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAMsAAAAZCAIAAAAQb01AAAAACXBIWXMAAA7EAAAOxAGVKw4bAAANc0lEQVR42u2bCVRTVxrHOXPqjGdm6jjaTjtdZlo7jkurVq2tti44XVxQQECrFaugYhVcQEHEDRVRKoLKGvZ9C/si+74l7IRAAoGwhEAS2ROy5735wrMplfBA1OM4Jzn/885NXt7l5vt+93+/mzy0/GIKNdLoxUmLTOnUSKOnU0NXVSOLQu9pbOVMJlJdOzG9SklYTjldI42eSnmk5uKqVkCtvhkPstKaNg1hGs1chZWMyoYuHMJAGsI0eiaBjT0rYXdieCDHCN7VYK6NL8fCvdfUpWfvLbbute7vLnZvtGGts+p6FUNDzKfv91UeX9HU0kPnd8a8QQuZV+c/r4Iwr9ht3itMWEgR6pkpv5cmu5ModSBKLkeKz4cILf1HTxBGj3rwVx6txrk8u4wWH+afk3QvLikjNfxaqJd9kKc9wdXO7eZZe0vjGCLRIfO2RYrZZGS/IL4xvAjV6NdnydOHLL2YWkCiZRZTc0qouWVN4Snl8MqUV10O5I3XeV/eGW+e2X2esRNv1w3ulsvc9ee5n1nyPjHnfnwwX20Pdgkd56IYFoHUPbeKdOwyNp1MgBcDbOcIH/mrlHHnjVfbw+wCuae8lNnd48jefpmlPZZaTCtMSfg9ZKamsmghNmZ70EenMEm7rAS0Y625Rnesdjn5Hb9ZdXMysgMLEJ9cxYz5xsfrUioXGlqrCfiQ3QtO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cIOFMLKFyoG+XLllwJ9UiAM8gpbJypT2FgjqKDzy6h4wVXh5fq2Ei9VTfb5drNpOtmKA1mGtiX6NgUgKK4BMszJfsWLOVrfKsAhDO8uGqN8DK9e517CWh/86aoKZnFNezapLaW4NS6fAaUOppdMGCgpr94jssA/rvRhYZ2uvg7J+5MB+s1wD1O1bybVNqs8TIiimPokaOcQ2shDKbxpeZhqFQZ5BMbN3i0wJ7Be20IFvEBGBWvCm702+H8ovHWB+t5rE3vwisieGFNVQMcLJ7hlK5cHfvDZGF7Jv/mhooiqcrKcMhrOB1m6N3XR7sSFhnGgysZuUEltV25lZwapI6XkV82MMKi9iN/GgnsRviCAJU8zmDiaMWG2N9zGL5Hw9Jnu3knLJevp7Szz+vjc0W/UvuGs9SUdHd2dekaPtWvPDn1DHT2DHXoGcNTR3TUlYRP1e512wAv0uP5NX+pJcVzt9jbgpZYw65t+0wwrTnCd5i8FvBJ94tXuLsHJ7Jz8p1gln20MNtbnn0ov8Zfvk7ZOGF7QeA73hwFk1uesfAgPXtbtK9rRC7ZFL1/mPP//+wasV4gwDLLxeGnuQNTof+DuHY00mkwFFYyKBg1hGr0wvMiUjjo6e+r/BNH8b5ZGMxC4Fz5etbTuhOwaIOy/VydVZdjFE/0AAAAASUVORK5CYII="  
alt="Base64 encoded image" /> 
</body>
</html>
View Code

显示结果如图:

 小结

   我们可以看到,上面那么小的一张图片,转换成base64字符串后,会变成很长的一串字符串,如果我们将所有的图片都转换的话,虽然是网页打开速度会略有提升,但是代码会变的臃肿,难以维护。所以,我们可以将一些很小的图片转换成base64编码,嵌入到css文件中,如小的图标。

 

 作者:雲霏霏

QQ交流群:243633526

 博客地址:http://www.cnblogs.com/yunfeifei/

 声明:本博客原创文字只代表本人工作中在某一时间内总结的观点或结论,与本人所在单位没有直接利益关系。非商业,未授权,贴子请以现状保留,转载时必须保留此段声明,且在文章页面明显位置给出原文连接。

如果大家感觉我的博文对大家有帮助,请推荐支持一把,给我写作的动力。

posted @ 2015-02-16 11:01  雲霏霏  阅读(16168)  评论(17编辑  收藏  举报