在传统的.net应用中,使用base64编码字符串是一件很轻松的事情,比如下面这段代码演示了如何将本地文件转化为base64字符串,并且将base64字符串又还原为图片文件.
base64编码在传统.net程序中的应用(by 菩提树下的杨过 http://yjmyzz.cnblogs.com/)
using
System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace Base64Study
{
/// <summary>
/// base64编码在传统.net程序中的应用(by 菩提树下的杨过 http://yjmyzz.cnblogs.com/ )
/// </summary>
class Program
{
static void Main( string [] args)
{
string imgstring = GetBase64FromFile( " c:\\self.png " );
Console.WriteLine(imgstring);
Bitmap bitmap = GetImageFromBase64(imgstring);
bitmap.Save( " c:\\self2.png " , ImageFormat.Png);
Console.Read();
}
/// <summary>
/// 将文件转换为base64字符串
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
static string GetBase64FromFile( string filePath)
{
byte [] b = File.ReadAllBytes(filePath);
return Convert.ToBase64String(b);
}
/// <summary>
/// 从base64字符串还原图片
/// </summary>
/// <param name="base64string"></param>
/// <returns></returns>
static Bitmap GetImageFromBase64( string base64string)
{
byte [] b = Convert.FromBase64String(base64string);
MemoryStream ms = new MemoryStream(b);
Bitmap bitmap = new Bitmap(ms);
return bitmap;
}
}
}
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace Base64Study
{
/// <summary>
/// base64编码在传统.net程序中的应用(by 菩提树下的杨过 http://yjmyzz.cnblogs.com/ )
/// </summary>
class Program
{
static void Main( string [] args)
{
string imgstring = GetBase64FromFile( " c:\\self.png " );
Console.WriteLine(imgstring);
Bitmap bitmap = GetImageFromBase64(imgstring);
bitmap.Save( " c:\\self2.png " , ImageFormat.Png);
Console.Read();
}
/// <summary>
/// 将文件转换为base64字符串
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
static string GetBase64FromFile( string filePath)
{
byte [] b = File.ReadAllBytes(filePath);
return Convert.ToBase64String(b);
}
/// <summary>
/// 从base64字符串还原图片
/// </summary>
/// <param name="base64string"></param>
/// <returns></returns>
static Bitmap GetImageFromBase64( string base64string)
{
byte [] b = Convert.FromBase64String(base64string);
MemoryStream ms = new MemoryStream(b);
Bitmap bitmap = new Bitmap(ms);
return bitmap;
}
}
}
但是到了silverlight环境中,这种简单的操作方式却无法使用了,幸好网上有一个开源的免费组件FluxJpeg,同时国外有高人已经利用该组件写出了将位图转化为base64的方法,这里我们借用一下即可:
代码
<
UserControl
x:Class
="SLBase64.MainPage"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable ="d" d:DesignWidth ="640" d:DesignHeight ="480" >
< StackPanel x:Name ="LayoutRoot" >
< Image x:Name ="img" Height ="100" Stretch ="Uniform" Source ="img/self.png" ></ Image >
< Button x:Name ="btnToBase64" Content ="转为base64字符串" Width ="200" Margin ="0,3,0,0" Click ="btnToBase64_Click" ></ Button >
< TextBox Text ="" x:Name ="txtBase64" TextWrapping ="Wrap" Height ="150" Margin ="0,3" VerticalScrollBarVisibility ="Auto" ></ TextBox >
< Button x:Name ="btnToImg" Content ="将上述base64字符串还原为图片" Width ="200" Margin ="0,0,0,3" Click ="btnToImg_Click" ></ Button >
< Image x:Name ="img2" Height ="100" Stretch ="Uniform" ></ Image >
</ StackPanel >
</ UserControl >
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable ="d" d:DesignWidth ="640" d:DesignHeight ="480" >
< StackPanel x:Name ="LayoutRoot" >
< Image x:Name ="img" Height ="100" Stretch ="Uniform" Source ="img/self.png" ></ Image >
< Button x:Name ="btnToBase64" Content ="转为base64字符串" Width ="200" Margin ="0,3,0,0" Click ="btnToBase64_Click" ></ Button >
< TextBox Text ="" x:Name ="txtBase64" TextWrapping ="Wrap" Height ="150" Margin ="0,3" VerticalScrollBarVisibility ="Auto" ></ TextBox >
< Button x:Name ="btnToImg" Content ="将上述base64字符串还原为图片" Width ="200" Margin ="0,0,0,3" Click ="btnToImg_Click" ></ Button >
< Image x:Name ="img2" Height ="100" Stretch ="Uniform" ></ Image >
</ StackPanel >
</ UserControl >
代码
using
System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using FluxJpeg.Core;
using FluxJpeg.Core.Encoder;
namespace SLBase64
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
/// <summary>
/// 老外写的方法:将WriteableBitmap转化为base64位字符串
/// </summary>
/// <param name="bitmap"></param>
/// <returns></returns>
private string GetBase64Image(WriteableBitmap bitmap)
{
int width = bitmap.PixelWidth;
int height = bitmap.PixelHeight;
int bands = 3 ;
byte [][,] raster = new byte [bands][,];
for ( int i = 0 ; i < bands; i ++ )
{
raster[i] = new byte [width, height];
}
for ( int row = 0 ; row < height; row ++ )
{
for ( int column = 0 ; column < width; column ++ )
{
int pixel = bitmap.Pixels[width * row + column];
raster[ 0 ][column, row] = ( byte )(pixel >> 16 );
raster[ 1 ][column, row] = ( byte )(pixel >> 8 );
raster[ 2 ][column, row] = ( byte )pixel;
}
}
ColorModel model = new ColorModel { colorspace = ColorSpace.RGB };
FluxJpeg.Core.Image img = new FluxJpeg.Core.Image(model, raster);
MemoryStream stream = new MemoryStream();
JpegEncoder encoder = new JpegEncoder(img, 100 , stream);
encoder.Encode();
stream.Seek( 0 , SeekOrigin.Begin);
byte [] binaryData = new Byte[stream.Length];
long bytesRead = stream.Read(binaryData, 0 , ( int )stream.Length);
string base64String =
System.Convert.ToBase64String(binaryData,
0 ,
binaryData.Length);
return base64String;
}
private void btnToBase64_Click( object sender, RoutedEventArgs e)
{
WriteableBitmap wb = new WriteableBitmap(img, null );
txtBase64.Text = "" ;
txtBase64.Text = GetBase64Image(wb);
}
private void btnToImg_Click( object sender, RoutedEventArgs e)
{
if (txtBase64.Text == "" ) { return ; }
try
{
img2.Source = null ;
byte [] b = Convert.FromBase64String(txtBase64.Text);
MemoryStream ms = new MemoryStream(b);
BitmapImage bitImage = new BitmapImage();
bitImage.SetSource(ms);
img2.Source = bitImage;
}
catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
}
}
}
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using FluxJpeg.Core;
using FluxJpeg.Core.Encoder;
namespace SLBase64
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
/// <summary>
/// 老外写的方法:将WriteableBitmap转化为base64位字符串
/// </summary>
/// <param name="bitmap"></param>
/// <returns></returns>
private string GetBase64Image(WriteableBitmap bitmap)
{
int width = bitmap.PixelWidth;
int height = bitmap.PixelHeight;
int bands = 3 ;
byte [][,] raster = new byte [bands][,];
for ( int i = 0 ; i < bands; i ++ )
{
raster[i] = new byte [width, height];
}
for ( int row = 0 ; row < height; row ++ )
{
for ( int column = 0 ; column < width; column ++ )
{
int pixel = bitmap.Pixels[width * row + column];
raster[ 0 ][column, row] = ( byte )(pixel >> 16 );
raster[ 1 ][column, row] = ( byte )(pixel >> 8 );
raster[ 2 ][column, row] = ( byte )pixel;
}
}
ColorModel model = new ColorModel { colorspace = ColorSpace.RGB };
FluxJpeg.Core.Image img = new FluxJpeg.Core.Image(model, raster);
MemoryStream stream = new MemoryStream();
JpegEncoder encoder = new JpegEncoder(img, 100 , stream);
encoder.Encode();
stream.Seek( 0 , SeekOrigin.Begin);
byte [] binaryData = new Byte[stream.Length];
long bytesRead = stream.Read(binaryData, 0 , ( int )stream.Length);
string base64String =
System.Convert.ToBase64String(binaryData,
0 ,
binaryData.Length);
return base64String;
}
private void btnToBase64_Click( object sender, RoutedEventArgs e)
{
WriteableBitmap wb = new WriteableBitmap(img, null );
txtBase64.Text = "" ;
txtBase64.Text = GetBase64Image(wb);
}
private void btnToImg_Click( object sender, RoutedEventArgs e)
{
if (txtBase64.Text == "" ) { return ; }
try
{
img2.Source = null ;
byte [] b = Convert.FromBase64String(txtBase64.Text);
MemoryStream ms = new MemoryStream(b);
BitmapImage bitImage = new BitmapImage();
bitImage.SetSource(ms);
img2.Source = bitImage;
}
catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
}
}
}
最后是示例源码地址:http://files.cnblogs.com/yjmyzz/Base64Study.rar
转载请注明出处(菩提树下的杨过 http://www.cnblogs.com/yjmyzz/archive/2010/01/14/1647918.html)