一. Base64的編碼規則html
Base64編碼的思想是是採用64個基本的ASCII碼字符對數據進行從新編碼。它將須要編碼的數據拆分紅字節數組。以3個字節爲一組。按順序排列24 位數據,再把這24位數據分紅4組,即每組6位。再在每組的的最高位前補兩個0湊足一個字節。這樣就把一個3字節爲一組的數據從新編碼成了4個字節。當所要編碼的數據的字節數不是3的整倍數,也就是說在分組時最後一組不夠3個字節。這時在最後一組填充1到2個0字節。並在最後編碼完成後在結尾添加1到2個 「=」。數組
例:將對ABC進行BASE64編碼:app
一、首先取ABC對應的ASCII碼值。A(65)B(66)C(67);
二、再取二進制值A(01000001)B(01000010)C(01000011);
三、而後把這三個字節的二進制碼接起來(010000010100001001000011);
四、 再以6位爲單位分紅4個數據塊,並在最高位填充兩個0後造成4個字節的編碼後的值,(00010000)(00010100)(00001001)(00000011),其中藍色部分爲真實數據;
五、再把這四個字節數據轉化成10進制數得(16)(20)(9)(3);
六、最後根據BASE64給出的64個基本字符表,查出對應的ASCII碼字符(Q)(U)(J)(D),這裏的值實際就是數據在字符表中的索引。編碼
注:BASE64字符表:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/spa
二.解碼規則.net
解碼過程就是把4個字節再還原成3個字節再根據不一樣的數據形式把字節數組從新整理成數據。orm
三. C#中的實現htm
編碼:blog
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
- 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;
- }
- }
http://www.csframework.com/archive/2/arc-2-20110617-1635.htm
PNG ->JPG, BMP -> PNG按比例縮小圖片
[csharp] view plain copy
- 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);
- }
-
- }
- }