///
/// 导入字体
///
private void btnImport_Click(
object sender, RoutedEventArgs e)
{
PhotoChooserTask photoChooserTask =
new PhotoChooserTask();
photoChooserTask.Completed +=
new EventHandler
(photoChooserTask_Completed);
photoChooserTask.Show();
}
void photoChooserTask_Completed(object sender, PhotoResult e)
{
var stream = e.ChosenPhoto;
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
stream.Seek(0, SeekOrigin.Begin);
if (!SaveFontFile(bytes))
{
MessageBox.Show("导入字体失败,图片文件不符合规定格式!");
}
else
{
MessageBox.Show("导入字体成功!");
}
}
///
/// 保存字体文件
///
private bool SaveFontFile(byte[] imageArray)
{
try
{
int headLeght = 300;
//截取头文件的二进制流并转换成字符串
if (imageArray.Length < headLeght) return false;
byte[] headerArray = new byte[headLeght];
Array.Copy(imageArray, imageArray.Length - headLeght, headerArray, 0, headLeght);
string headInfo = Encoding.UTF8.GetString(headerArray, 0, headerArray.Length);
//分离头文件信息
string[] items = headInfo.Split(',');
if (items.Length < 6) return false;
string checkChar = items[0].Trim();
if (checkChar != "BeginOOKWriteFonttransfer") return false;
string fontFileName = items[1].Trim();
string fontChineseName = items[2].Trim();
string fontEnglishName = items[3].Trim();
string fontLenght = items[4].Trim();
string fontPicLenght = items[5].Trim();
//截取字体文件的二进制流
Byte[] fontByteArray = new byte[int.Parse(fontLenght)];
Array.Copy(imageArray, int.Parse(fontPicLenght), fontByteArray, 0, fontByteArray.Length);
//写入独立存储
string fileName = string.Format("Fonts\\{0}", fontFileName);
using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!storage.DirectoryExists("Fonts")) storage.CreateDirectory("Fonts");
if (storage.FileExists(fileName)) storage.DeleteFile(fileName);
using (var fileStream = new IsolatedStorageFileStream(fileName, FileMode.Create, storage))
{
using (var writer = new BinaryWriter(fileStream))
{
writer.Write(fontByteArray);
}
}
}
}
catch (Exception exx)
{
MessageBox.Show(exx.ToString());
}
return true;
}