一. 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个字节的编码后的值,(00010000)(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;
把bmp图片转换为jpg图片(C#)
[csharp]
- using System.IO;
- using System.Drawing.Imaging;
- public class imgConvert
- {
- private void BMPToJPG(string bmpFileName,string jpgFileName)
- {
- System.Drawing.Image img;
- img=ReturnPhoto(bmpFileName);
- img.Save(jpgFileName,ImageFormat.Jpeg);
- }
- private Image ReturnPhoto(string bmpFileName)
- {
- System.IO.FileStream stream ;
- stream=File.OpenRead(bmpFileName);
- Bitmap bmp = new Bitmap(stream);
- System.Drawing.Image image = bmp;//得到原图
- //创建指定大小的图
- System.Drawing.Image newImage = image.GetThumbnailImage(bmp.Width, bmp.Height,null, new IntPtr());
- Graphics g=Graphics.FromImage(newImage);
- g.DrawImage(newImage,0,0, newImage.Width, newImage.Height); //将原图画到指定的图上
- g.Dispose();
- stream.Close();
- return newImage;
- }
- }
PNG ->JPG, BMP -> PNG按比例缩小图片
[csharp]
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.IO;
- using System.Drawing;
- using System.Drawing.Imaging;
- namespace www.CSFramework.com
- {
- public class CImageLibrary
- {
- public enum ValidateImageResult { OK, InvalidFileSize, InvalidImageSize }
- //检查图片大小
- public static ValidateImageResult ValidateImage(string file, int MAX_FILE_SIZE, int MAX_WIDTH, int MAX_HEIGHT)
- {
- byte[] bs = File.ReadAllBytes(file);
- double size = (bs.Length / 1024);
- //大于50KB
- if (size > MAX_FILE_SIZE) return ValidateImageResult.InvalidFileSize;
- Image img = Image.FromFile(file);
- if (img.Width > MAX_WIDTH || img.Height > MAX_HEIGHT) return ValidateImageResult.InvalidImageSize;
- return ValidateImageResult.OK;
- }
- //按宽度比例缩小图片
- public static Image GetOutputSizeImage(Image imgSource, int MAX_WIDTH)
- {
- Image imgOutput = imgSource;
- Size size = new Size(imgSource.Width, imgSource.Height);
- if (imgSource.Width <= 3 || imgSource.Height <= 3) return imgSource; //3X3大小的图片不转换
- if (imgSource.Width > MAX_WIDTH || imgSource.Height > MAX_WIDTH)
- {
- double rate = MAX_WIDTH / (double)imgSource.Width;
- if (imgSource.Height * rate > MAX_WIDTH)
- rate = MAX_WIDTH / (double)imgSource.Height;
- size.Width = Convert.ToInt32(imgSource.Width * rate);
- size.Height = Convert.ToInt32(imgSource.Height * rate);
- imgOutput = imgSource.GetThumbnailImage(size.Width, size.Height, null, IntPtr.Zero);
- }
- return imgOutput;
- }
- //按比例缩小图片
- public static Image GetOutputSizeImage(Image imgSource, Size outSize)
- {
- Image imgOutput = imgSource.GetThumbnailImage(outSize.Width, outSize.Height, null, IntPtr.Zero);
- return imgOutput;
- }
- /// <summary>
- /// 由图片文件转字节
- /// </summary>
- public static byte[] GetImageBytes(string imageFileName)
- {
- Image img = Image.FromFile(imageFileName);
- return GetImageBytes(img);
- }
- /// <summary>
- /// 图片转字节
- /// </summary>
- public static byte[] GetImageBytes(Image img)
- {
- if (img == null) return null;
- try
- {
- System.IO.MemoryStream ms = new MemoryStream();
- img.Save(ms, ImageFormat.Jpeg);
- byte[] bs = ms.ToArray();
- ms.Close();
- return bs;
- }
- catch { return null; }
- }
- /// <summary>
- /// 字节转图片
- /// </summary>
- public static Image FromBytes(byte[] bs)
- {
- if (bs == null) return null;
- try
- {
- MemoryStream ms = new MemoryStream(bs);
- Image returnImage = Image.FromStream(ms);
- ms.Close();
- return returnImage;
- }
- catch { return null; }
- }
- /// <summary>
- /// 将其它格式的图片转为JPG文件
- /// </summary>
- public static Image ToJPG(Image source)
- {
- //注意,先定义Bitmap类,否则会报A generic error occurred in GDI+
- Bitmap bmp = new Bitmap(source);
- MemoryStream ms = new MemoryStream();
- bmp.Save(ms, ImageFormat.Jpeg);
- return Bitmap.FromStream(ms);
- }
- /// <summary>
- /// 将其它格式的图片转为PNG文件
- /// </summary>
- public static Image ToPNG(Image source)
- {
- //注意,先定义Bitmap类,否则会报A generic error occurred in GDI+
- Bitmap bmp = new Bitmap(source);
- MemoryStream ms = new MemoryStream();
- bmp.Save(ms, ImageFormat.Png);
- return FromBytes(ms.ToArray());
- }
- //保存文件
- public static void SaveFile(string fileName, Image source)
- {
- source.Save(fileName, source.RawFormat);
- }
- }
- }