C#图⽚Base64编码,图⽚格式转换
⼀. 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);
解码:getsavefilename
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#)
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按⽐例缩⼩图⽚
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
namespace www.CSFramework
{
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;
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);
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。