上2个部分讲到从文件夹或AD的thumbnailPhoto属性用代码同步照片到sharepoint2010的个人照片信息中去。但是大家发现没有sharepoint2010的照片库和方法支持jgp格式,我们一起去看看照片库或看看个人照片属性,都貌似只有JPG格式,否则同步会提示:参数错误。如下图:
但是实际情况我们的文件夹或AD的thumbnailPhoto属性并非全是jpg,可能还存在gif,png,bmp格式的头像,那我们怎么做呢?
翻看MSDN的资料我们可以看到如下例子:
bitmap.Save(newstream, System.Drawing.Imaging.ImageFormat.Jpeg);
就是把其他格式图片另存jpg的格式。因此我们在同步头像前,需要判断该照片是否jpg,如果是gif,png就需要用如上方法转换了。说道转换,大家知道上传文件的类型的判断。一般的人只知道判断扩展名是否合符要求即可,其实这个是错误的。最准确的办法就是判断二进制的前几位是固定的,附带如下格式的二进制前几位信息:
JPG = 255216
GIF = 7173
PNG = 13780
SWF = 6787
RAR = 8297
ZIP = 8075
_7Z = 55122
XLS = 208207
XLSX = 8075
bmp =6677
我们在文件夹下放一个png的图片如下图:
代码段:
//判断照片格式
public static bool IsAllowedExtension(byte[] imgArray, FileExtension[] fileEx)
{
MemoryStream ms = new MemoryStream(imgArray);
System.IO.BinaryReader br = new System.IO.BinaryReader(ms);
string fileclass = "";
byte buffer;
try
{
buffer = br.ReadByte();
fileclass = buffer.ToString();
buffer = br.ReadByte();
fileclass += buffer.ToString();
}
catch
{
}
br.Close();
ms.Close();
foreach (FileExtension fe in fileEx)
{
if (Int32.Parse(fileclass) == (int)fe)
return true;
}
return false;
}
//照片格式二进制头
public enum FileExtension
{
JPG = 255216,
GIF = 7173,
PNG = 13780,
SWF = 6787,
RAR = 8297,
ZIP = 8075,
_7Z = 55122,
XLS = 208207,
XLSX = 8075,
bmp =6677
}
同步代码:
/// <summary> /// 上传图片 /// </summary> private static void UploadPhotos() { SPSite mySites = new SPSite(MySiteHostUrl); SPWeb myWeb = mySites.OpenWeb(); SPFolder subfolderForPictures = myWeb.GetFolder("User Photos\\Profile Pictures"); if (subfolderForPictures == null) { return; } UserProfileManager userProfileManager = new UserProfileManager(SPServiceContext.GetContext(mySites)); //读取文档 string[] strCollection = Directory.GetFiles(@"c:\photoFolder"); foreach (string item in strCollection) { //FileInfo FileInfo fileinfo = new FileInfo(item); // byte[] buffer = null; string fileNameWithoutExtension = GetFileNameFromAccountName(MAccountName); string photoURL = fileinfo.FullName; //if(photoURL.ToLower().EndsWith("jpg")) //{ //System.Threading.Thread.Sleep(2500); buffer = ReadFile(fileinfo.FullName); int largeThumbnailSize = 0X90; int mediumThumbnailSize = 0X60; int smallThumbnailSize = 0X20; using (MemoryStream stream = new MemoryStream(buffer)) { using (Bitmap bitmap = new Bitmap(stream, true)) { //判断下是否为png,gif if (IsAllowedExtension(buffer, new FileExtension[] { FileExtension.GIF,FileExtension.PNG,FileExtension.bmp })) { using (MemoryStream newstream = new MemoryStream()) { bitmap.Save(newstream, System.Drawing.Imaging.ImageFormat.Jpeg); // using (Bitmap newbitmap = new Bitmap(newstream, true)) { CreateThumbnail(newbitmap, largeThumbnailSize, largeThumbnailSize, subfolderForPictures, fileNameWithoutExtension + "_LThumb.jpg"); CreateThumbnail(newbitmap, mediumThumbnailSize, mediumThumbnailSize, subfolderForPictures, fileNameWithoutExtension + "_MThumb.jpg"); CreateThumbnail(newbitmap, smallThumbnailSize, smallThumbnailSize, subfolderForPictures, fileNameWithoutExtension + "_SThumb.jpg"); } } } //判断下是否为jpg
if (IsAllowedExtension(buffer, new FileExtension[] { FileExtension.JPG })) { CreateThumbnail(bitmap, largeThumbnailSize, largeThumbnailSize, subfolderForPictures, fileNameWithoutExtension + "_LThumb.jpg"); CreateThumbnail(bitmap, mediumThumbnailSize, mediumThumbnailSize, subfolderForPictures, fileNameWithoutExtension + "_MThumb.jpg"); CreateThumbnail(bitmap, smallThumbnailSize, smallThumbnailSize, subfolderForPictures, fileNameWithoutExtension + "_SThumb.jpg"); } } } SetPictureUrl(MAccountName, subfolderForPictures, userProfileManager, mySites.Url); //} } myWeb.Dispose(); mySites.Dispose(); }
执行一下代码,去照片库和头像看看
效果达到,同样大家可以去测试下gif的格式
注意:如果AD用户过多,上传照片的时候会提示如下错误信息:
无法保存更改,因为此 SharePoint 网站已超过存储配额限制。
您必须将工作保存到其他位置。请与您的管理员联系以更改网站配额限制。
此时需要做如下更改:
打开【sharepoint2010管理中心】-【应用程序管理】-【配置配额和锁定】如下图: