C# 图片Base64 编码,图片格式转换

来源:互联网 发布:全网分红系统源码 编辑:程序博客网 时间:2024/05/22 17:17

http://www.cnblogs.com/lindana/archive/2009/03/25/1421345.html


一. Base64的编码规则

Base64编码的思想是是采用64个基本的ASCII码字符对数据进行重新编码。它将需要编码的数据拆分成字节数组。以3个字节为一组。按顺序排列24 位数据,再把这24位数据分成4组,即每组6位。再在每组的的最高位前补两个0凑足一个字节。这样就把一个3字节为一组的数据重新编码成了4个字节。当所要编码的数据的字节数不是3的整倍数,也就是说在分组时最后一组不够3个字节。这时在最后一组填充1到2个0字节。并在最后编码完成后在结尾添加1到2个 “=”。

例:将对ABC进行BASE64编码:


1、首先取ABC对应的ASCII码值。A(65)B(66)C(67);
2、再取二进制值A(01000001)B(01000010)C(01000011);
 3、然后把这三个字节的二进制码接起来(010000010100001001000011);
4、 再以6位为单位分成4个数据块,并在最高位填充两个0后形成4个字节的编码后的值,(00
010000)(00010100)(00001001)(00000011),其中蓝色部分为真实数据;
 5、再把这四个字节数据转化成10进制数得(16)(20)(9)(3);
 6、最后根据BASE64给出的64个基本字符表,查出对应的ASCII码字符(Q)(U)(J)(D),这里的值实际就是数据在字符表中的索引。

注:BASE64字符表:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/

二.解码规则

解码过程就是把4个字节再还原成3个字节再根据不同的数据形式把字节数组重新整理成数据。


三. C#中的实现

 编码

byte[] bytes = Encoding.Default.GetBytes("要转换的字符");
string str = Convert.ToBase64String(bytes);

解码:

byte[] outputb = Convert.FromBase64String(str);
string orgStr = Encoding.Default.GetString(outputb);

C#图片的Base64编码和解码 

图片的Base64编码:

System.IO.MemoryStream m = new System.IO.MemoryStream();
System.Drawing.Bitmap bp 
= new System.Drawing.Bitmap(@“c:\demo.GIF”);
bp.Save(m, System.Drawing.Imaging.ImageFormat.Gif);
byte[]b= m.GetBuffer();
string base64string=Convert.ToBase64String(b);

Base64字符串解码:

byte[] bt = Convert.FromBase64String(base64string);
System.IO.MemoryStream stream 
= new System.IO.MemoryStream(bt);
Bitmap bitmap 
= new Bitmap(stream);
pictureBox1.Image 
= bitmap;


http://www.weste.net/html/200408/20040803QBI163429.html

把bmp图片转换为jpg图片(C#)

[csharp] view plain copy
  1. using System.IO;  
  2. using System.Drawing.Imaging;  
  3.    
  4. public class imgConvert  
  5. {  
  6.     private void BMPToJPG(string bmpFileName,string jpgFileName)  
  7.     {  
  8.            
  9.        System.Drawing.Image img;  
  10.        img=ReturnPhoto(bmpFileName);  
  11.      
  12.        img.Save(jpgFileName,ImageFormat.Jpeg);  
  13.     }  
  14.    
  15.     private Image ReturnPhoto(string bmpFileName)  
  16.     {  
  17.        System.IO.FileStream stream ;  
  18.        stream=File.OpenRead(bmpFileName);  
  19.        Bitmap bmp = new Bitmap(stream);  
  20.        System.Drawing.Image image = bmp;//得到原图  
  21.        //创建指定大小的图  
  22.        System.Drawing.Image newImage = image.GetThumbnailImage(bmp.Width, bmp.Height,nullnew IntPtr());  
  23.        Graphics g=Graphics.FromImage(newImage);  
  24.        g.DrawImage(newImage,0,0, newImage.Width, newImage.Height); //将原图画到指定的图上  
  25.        g.Dispose();  
  26.        stream.Close();  
  27.        return newImage;  
  28.     }  
  29. }  



http://www.csframework.com/archive/2/arc-2-20110617-1635.htm

 PNG ->JPG, BMP -> PNG按比例缩小图片


[csharp] view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.IO;  
  5. using System.Drawing;  
  6. using System.Drawing.Imaging;  
  7.   
  8. namespace www.CSFramework.com  
  9. {  
  10.    public class CImageLibrary  
  11.    {  
  12.       public enum ValidateImageResult { OK, InvalidFileSize, InvalidImageSize }  
  13.         
  14.       //检查图片大小   
  15.       public static ValidateImageResult ValidateImage(string file, int MAX_FILE_SIZE, int MAX_WIDTH, int MAX_HEIGHT)  
  16.       {  
  17.          byte[] bs = File.ReadAllBytes(file);  
  18.            
  19.          double size = (bs.Length / 1024);  
  20.          //大于50KB   
  21.          if (size > MAX_FILE_SIZE) return ValidateImageResult.InvalidFileSize;  
  22.          Image img = Image.FromFile(file);  
  23.          if (img.Width > MAX_WIDTH || img.Height > MAX_HEIGHT) return ValidateImageResult.InvalidImageSize;  
  24.          return ValidateImageResult.OK;  
  25.       }  
  26.         
  27.       //按宽度比例缩小图片   
  28.       public static Image GetOutputSizeImage(Image imgSource, int MAX_WIDTH)  
  29.       {  
  30.          Image imgOutput = imgSource;  
  31.            
  32.          Size size = new Size(imgSource.Width, imgSource.Height);  
  33.          if (imgSource.Width <= 3 || imgSource.Height <= 3) return imgSource; //3X3大小的图片不转换   
  34.            
  35.          if (imgSource.Width > MAX_WIDTH || imgSource.Height > MAX_WIDTH)  
  36.          {  
  37.             double rate = MAX_WIDTH / (double)imgSource.Width;  
  38.               
  39.             if (imgSource.Height * rate > MAX_WIDTH)  
  40.             rate = MAX_WIDTH / (double)imgSource.Height;  
  41.               
  42.             size.Width = Convert.ToInt32(imgSource.Width * rate);  
  43.             size.Height = Convert.ToInt32(imgSource.Height * rate);  
  44.               
  45.             imgOutput = imgSource.GetThumbnailImage(size.Width, size.Height, null, IntPtr.Zero);  
  46.          }  
  47.            
  48.          return imgOutput;  
  49.       }  
  50.         
  51.       //按比例缩小图片   
  52.       public static Image GetOutputSizeImage(Image imgSource, Size outSize)  
  53.       {  
  54.          Image imgOutput = imgSource.GetThumbnailImage(outSize.Width, outSize.Height, null, IntPtr.Zero);  
  55.          return imgOutput;  
  56.       }  
  57.         
  58.       /// <summary>   
  59.       /// 由图片文件转字节   
  60.       /// </summary>   
  61.       public static byte[] GetImageBytes(string imageFileName)  
  62.       {  
  63.          Image img = Image.FromFile(imageFileName);  
  64.          return GetImageBytes(img);  
  65.       }  
  66.         
  67.       /// <summary>   
  68.       /// 图片转字节   
  69.       /// </summary>   
  70.       public static byte[] GetImageBytes(Image img)  
  71.       {  
  72.          if (img == nullreturn null;  
  73.          try  
  74.          {  
  75.             System.IO.MemoryStream ms = new MemoryStream();  
  76.             img.Save(ms, ImageFormat.Jpeg);  
  77.             byte[] bs = ms.ToArray();  
  78.             ms.Close();  
  79.             return bs;  
  80.          }  
  81.          catch { return null; }  
  82.       }  
  83.         
  84.       /// <summary>   
  85.       /// 字节转图片   
  86.       /// </summary>   
  87.       public static Image FromBytes(byte[] bs)  
  88.       {  
  89.          if (bs == nullreturn null;  
  90.          try  
  91.          {  
  92.             MemoryStream ms = new MemoryStream(bs);  
  93.             Image returnImage = Image.FromStream(ms);  
  94.             ms.Close();  
  95.             return returnImage;  
  96.          }  
  97.          catch { return null; }  
  98.       }  
  99.         
  100.       /// <summary>   
  101.       /// 将其它格式的图片转为JPG文件   
  102.       /// </summary>   
  103.       public static Image ToJPG(Image source)  
  104.       {  
  105.          //注意,先定义Bitmap类,否则会报A generic error occurred in GDI+   
  106.          Bitmap bmp = new Bitmap(source);  
  107.          MemoryStream ms = new MemoryStream();  
  108.          bmp.Save(ms, ImageFormat.Jpeg);  
  109.          return Bitmap.FromStream(ms);  
  110.       }  
  111.         
  112.       /// <summary>   
  113.       /// 将其它格式的图片转为PNG文件   
  114.       /// </summary>   
  115.       public static Image ToPNG(Image source)  
  116.       {  
  117.          //注意,先定义Bitmap类,否则会报A generic error occurred in GDI+   
  118.          Bitmap bmp = new Bitmap(source);  
  119.          MemoryStream ms = new MemoryStream();  
  120.          bmp.Save(ms, ImageFormat.Png);  
  121.          return FromBytes(ms.ToArray());  
  122.       }  
  123.         
  124.       //保存文件   
  125.       public static void SaveFile(string fileName, Image source)  
  126.       {  
  127.          source.Save(fileName, source.RawFormat);  
  128.       }  
  129.         
  130.    }  
  131. }  

原创粉丝点击