📄 impsportrait.cs
字号:
namespace Imps.Client.Resource
{
using Imps.Client;
using Imps.Utils;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.CompilerServices;
using System.Xml;
public class ImpsPortrait
{
private const string _boyPortrait = "default1.jpg";
private static volatile Image _boyPortraitImg;
private static volatile Image _defaultGroupPortraitImg;
private const string _defaultPortrait = "default.jpg";
private static volatile Image _defaultPortraitImg;
private const string _girlPortrait = "default0.jpg";
private static volatile Image _girlPortraitImg;
private const string _groupPortrait = "default.jpg";
private static string _groupPortraitDir;
private const string _groupRootNameSpace = "GroupPortraits";
private const string _mobilePortrait = "mobilebuddy.jpg";
private static volatile Image _mobilePortraitImg;
private static string _portraitDir;
private const string _portraitFileEx = "*.jpg";
private const string _rootNameSpace = "Portraits.";
private const string _vodafonePortrait = "vodafone.jpg";
private static volatile Image _vodafonePortraitImg;
private const string _xmlDesPath = "Portraits/file[@name='{0}']";
private static XmlDocument _xmlGroupPortrait;
private static XmlDocument _xmlPortrait;
private const string _xmlPortraitFileName = "Portraits.xml";
private const string _xmlPortraitPath = "Portraits/file";
static ImpsPortrait()
{
Init();
}
public static Image GetBoyPortrait(int width, int height)
{
if (_boyPortraitImg == null)
{
_boyPortraitImg = GetPortrait(Path.Combine(PortraitDir, "default1.jpg"));
}
return GetPortrait(_boyPortraitImg, width, height);
}
public static Image GetDefaultGroupPortrait(int width, int height)
{
if (_defaultGroupPortraitImg == null)
{
_defaultGroupPortraitImg = GetPortrait(Path.Combine(GroupPortraitDir, "default.jpg"));
}
return GetPortrait(_defaultGroupPortraitImg, width, height);
}
public static Imps.Client.Resource.Portrait[] GetDefaultGroupPortraits()
{
return GetDefaultPortraits(xmlGroupPortrait, GroupPortraitDir);
}
public static Image GetDefaultPortrait(int width, int height)
{
if (_defaultPortraitImg == null)
{
_defaultPortraitImg = GetPortrait(Path.Combine(PortraitDir, "default.jpg"));
}
return GetPortrait(_defaultPortraitImg, width, height);
}
public static Imps.Client.Resource.Portrait[] GetDefaultPortraits()
{
return GetDefaultPortraits(xmlPortrait, PortraitDir);
}
private static Imps.Client.Resource.Portrait[] GetDefaultPortraits(XmlDocument xmlDocPortrait, string portraitPath)
{
XmlNodeList list = xmlDocPortrait.DocumentElement.SelectNodes("Portraits/file");
List<Imps.Client.Resource.Portrait> list2 = new List<Imps.Client.Resource.Portrait>(list.Count);
foreach (XmlNode node in list)
{
string text = XmlHelper.ReadXmlAttributeString(node, "name");
string description = node.InnerText;
if (description.Length > 0)
{
string path = Path.Combine(portraitPath, text);
if (File.Exists(path))
{
Image portrait = GetPortrait(path);
if (portrait != null)
{
list2.Add(new Imps.Client.Resource.Portrait(portrait, description));
}
}
}
}
return list2.ToArray();
}
public static Image GetGirlPortrait(int width, int height)
{
if (_girlPortraitImg == null)
{
_girlPortraitImg = GetPortrait(Path.Combine(PortraitDir, "default0.jpg"));
}
return GetPortrait(_girlPortraitImg, width, height);
}
public static Image GetMobilePortrait(int width, int height)
{
if (_mobilePortraitImg == null)
{
_mobilePortraitImg = GetPortrait(Path.Combine(PortraitDir, "mobilebuddy.jpg"));
}
return GetPortrait(_mobilePortraitImg, width, height);
}
private static Image GetPortrait(string path)
{
try
{
return ImpsResources.LoadImage(path);
}
catch
{
return null;
}
}
internal static Image GetPortrait(Image image, int width, int height)
{
if (image == null)
{
return null;
}
try
{
if ((width == image.Width) && (height == image.Height))
{
return image;
}
if (width == height)
{
return ImageHelper.GetSquareThumbnailImage(image, width);
}
return ImageHelper.GetThumbnailImage(image, width, height, true);
}
catch
{
return null;
}
}
public static Image GetPortrait(string path, int width, int height)
{
try
{
if (width == height)
{
return ImageHelper.GetSquareThumbnailImage(path, width);
}
return ImageHelper.GetThumbnailImage(path, width, height);
}
catch
{
return null;
}
}
public static Image GetVodafonePortrait(int width, int height)
{
if (_vodafonePortraitImg == null)
{
_vodafonePortraitImg = ImpsResources.GetImage("Portraits.vodafone.jpg");
}
return GetPortrait(_vodafonePortraitImg, width, height);
}
private static void Init()
{
try
{
if (!Directory.Exists(PortraitDir))
{
Directory.CreateDirectory(PortraitDir);
}
if (!File.Exists(Path.Combine(PortraitDir, "default1.jpg")))
{
_boyPortraitImg = ImpsResources.GetImage("Portraits.default1.jpg");
if (_boyPortraitImg != null)
{
_boyPortraitImg.Save(Path.Combine(PortraitDir, "default1.jpg"), ImageFormat.Jpeg);
}
}
_mobilePortraitImg = ImpsResources.GetImage("Portraits.mobilebuddy.jpg");
if (!File.Exists(Path.Combine(PortraitDir, "default0.jpg")))
{
_girlPortraitImg = ImpsResources.GetImage("Portraits.default0.jpg");
if (_girlPortraitImg != null)
{
_girlPortraitImg.Save(Path.Combine(PortraitDir, "default0.jpg"), ImageFormat.Jpeg);
}
}
if (!File.Exists(Path.Combine(PortraitDir, "default.jpg")))
{
_defaultPortraitImg = ImpsResources.GetImage("Portraits.default.jpg");
if (_defaultPortraitImg != null)
{
_defaultPortraitImg.Save(Path.Combine(PortraitDir, "default.jpg"), ImageFormat.Jpeg);
}
}
}
catch
{
}
}
public static string GroupPortraitDir
{
get
{
if (_groupPortraitDir == null)
{
_groupPortraitDir = Path.Combine(ImpsResources.ResourcesDir, "GroupPortraits");
}
return _groupPortraitDir;
}
}
public static string PortraitDir
{
get
{
if (_portraitDir == null)
{
_portraitDir = Path.Combine(ImpsResources.ResourcesDir, "Portraits");
}
return _portraitDir;
}
}
internal static XmlDocument xmlGroupPortrait
{
get
{
if (_xmlGroupPortrait == null)
{
_xmlGroupPortrait = new XmlDocument();
bool flag = true;
string path = Path.Combine(GroupPortraitDir, "Portraits.xml");
if (File.Exists(path))
{
try
{
_xmlGroupPortrait.Load(path);
flag = false;
}
catch
{
}
}
if (flag)
{
_xmlGroupPortrait.Load(ImpsResources.GetResourceStream("GroupPortraitsPortraits.xml"));
}
}
return _xmlGroupPortrait;
}
}
internal static XmlDocument xmlPortrait
{
get
{
if (_xmlPortrait == null)
{
_xmlPortrait = new XmlDocument();
bool flag = true;
string path = Path.Combine(PortraitDir, "Portraits.xml");
if (File.Exists(path))
{
try
{
_xmlPortrait.Load(path);
flag = false;
}
catch
{
}
}
if (flag)
{
_xmlPortrait.Load(ImpsResources.GetResourceStream("Portraits.Portraits.xml"));
}
}
return _xmlPortrait;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -