/// <summary> /// 创建文件夹 /// </summary> /// <param name="uid"></param> private void CreateDir(string uid) { uid = Avatars.FormatUid(uid); string avatarDir = string.Format("{0}avatars/upload/{1}/{2}/{3}", BaseConfigs.GetForumPath, uid.Substring(0, 3), uid.Substring(3, 2), uid.Substring(5, 2)); if (!Directory.Exists(Utils.GetMapPath(avatarDir))) { Directory.CreateDirectory(Utils.GetMapPath(avatarDir)); } }
/// <summary> /// 保存头像文件 /// </summary> /// <param name="avatar"></param> /// <param name="uid"></param> /// <returns></returns> private bool SaveAvatar(string avatar, string uid) { byte[] b = FlashDataDecode(DNTRequest.GetString(avatar)); if (b.Length == 0) { return(false); } uid = Avatars.FormatUid(uid); string size = ""; if (avatar == "avatar1") { size = "large"; } else if (avatar == "avatar2") { size = "medium"; } else { size = "small"; } string avatarFileName = string.Format("{0}avatars/upload/{1}/{2}/{3}/{4}_avatar_{5}.jpg", BaseConfigs.GetForumPath, uid.Substring(0, 3), uid.Substring(3, 2), uid.Substring(5, 2), uid.Substring(7, 2), size); FileStream fs = new FileStream(Utils.GetMapPath(avatarFileName), FileMode.Create); fs.Write(b, 0, b.Length); fs.Close(); //当支持FTP上传头像时,使用FTP上传远程头像 if (FTPs.GetForumAvatarInfo.Allowupload == 1) { FTPs ftps = new FTPs(); string ftpAvatarFileName = string.Format("/avatars/upload/{0}/{1}/{2}/", uid.Substring(0, 3), uid.Substring(3, 2), uid.Substring(5, 2)); ftps.UpLoadFile(ftpAvatarFileName, Utils.GetMapPath(avatarFileName), FTPs.FTPUploadEnum.ForumAvatar); } return(true); }
protected void ChangeAvatars_Click(object sender, EventArgs e) { DataTable avatars = DbHelper.ExecuteDataset(string.Format("SELECT {0}users.uid,avatar FROM {0}users JOIN {0}userfields ON {0}users.uid={0}userfields.uid WHERE avatar LIKE '/%'", BaseConfigs.GetTablePrefix)).Tables[0]; int count = 0; foreach (DataRow dr in avatars.Rows) { string sourceAvatarPath = Utils.GetMapPath(dr["avatar"].ToString()); string uid = Avatars.FormatUid(dr["uid"].ToString()); if (File.Exists(sourceAvatarPath)) { string destDir = string.Format("{0}avatars/upload/{1}/{2}/{3}", BaseConfigs.GetForumPath, uid.Substring(0, 3), uid.Substring(3, 2), uid.Substring(5, 2)); if (!Directory.Exists(Utils.GetMapPath(destDir))) { Directory.CreateDirectory(Utils.GetMapPath(destDir)); } string destAvatarPath = Utils.GetMapPath(string.Format("{0}avatars/upload/{1}/{2}/{3}/{4}_avatar_{5}.jpg", BaseConfigs.GetForumPath, uid.Substring(0, 3), uid.Substring(3, 2), uid.Substring(5, 2), uid.Substring(7, 2), "large")); File.Copy(sourceAvatarPath, destAvatarPath, true); destAvatarPath = Utils.GetMapPath(string.Format("{0}avatars/upload/{1}/{2}/{3}/{4}_avatar_{5}.jpg", BaseConfigs.GetForumPath, uid.Substring(0, 3), uid.Substring(3, 2), uid.Substring(5, 2), uid.Substring(7, 2), "medium")); File.Copy(sourceAvatarPath, destAvatarPath, true); destAvatarPath = Utils.GetMapPath(string.Format("{0}avatars/upload/{1}/{2}/{3}/{4}_avatar_{5}.jpg", BaseConfigs.GetForumPath, uid.Substring(0, 3), uid.Substring(3, 2), uid.Substring(5, 2), uid.Substring(7, 2), "small")); string destPath = string.Format("{0}avatars/upload/{1}/{2}/{3}/{4}_avatar_{5}.jpg", BaseConfigs.GetForumPath, uid.Substring(0, 3), uid.Substring(3, 2), uid.Substring(5, 2), uid.Substring(7, 2), "small"); File.Copy(sourceAvatarPath, destAvatarPath, true); Thumbnail thumb = new Thumbnail(); thumb.SetImage(destPath); thumb.SaveThumbnailImage(48, 48); count++; } } info.Text = "<span style='color:blue'>提示:</span>头像转换程序已经成功转换了 <span style='color:red'>" + count + "</span> 个头像."; change.Text = "进入首页"; change.Attributes.Add("onclick", "window.location.href='../index.aspx';return false;"); }
/// <summary> /// 截取指定位置的图片信息 /// </summary> /// <param name="filePathName"></param> /// <param name="point"></param> /// <param name="size"></param> /// <returns></returns> private bool GrabImage(CredentialInfo creinfo, string filePathName, Point point, Size size) { if (!System.IO.File.Exists(filePathName)) { return(false); } Image oldImage = System.Drawing.Image.FromFile(filePathName); if (size.Width > oldImage.Width || size.Height > oldImage.Height) { oldImage.Dispose(); return(false); } //用指定的大小和格式初始化 Bitmap 类的新实例 Bitmap bitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppArgb); Color backColor = Color.Black;// bitmap.GetPixel(1, 1); bitmap.MakeTransparent(backColor); //从指定的 Image 对象创建新 Graphics 对象 Graphics graphics = Graphics.FromImage(bitmap); // 0,0 是graphics的起始位置,也就是从原点开始画 // new Rectangle是截取源图片的目标区域,用户只需要改变其中四个值即可 graphics.DrawImage(oldImage /*是原图片*/, 0, 0, new Rectangle(point.X, point.Y, size.Width, size.Height), GraphicsUnit.Pixel); // 将Bitmap转化成Image Image image = Image.FromHbitmap(bitmap.GetHbitmap()); ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo ici = null; foreach (ImageCodecInfo codec in codecs) { if (codec.MimeType.IndexOf("jpeg") > -1) { ici = codec; } } EncoderParameters encoderParams = new EncoderParameters(); long[] qualityParam = new long[1]; qualityParam[0] = 100; EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam); encoderParams.Param[0] = encoderParam; string uid = Avatars.FormatUid(creinfo.UserID.ToString()); string avatarFileName = string.Format("{0}avatars/upload/{1}/{2}/{3}/{4}_avatar_", BaseConfigs.GetForumPath, uid.Substring(0, 3), uid.Substring(3, 2), uid.Substring(5, 2), uid.Substring(7, 2)); avatarFileName = Utils.GetMapPath(avatarFileName); image.Save(avatarFileName + "large.jpg", ici, encoderParams); if (size.Width * 0.8 <= 130)//前台模版宽度 { Thumbnail.MakeThumbnailImage(avatarFileName + "large.jpg", avatarFileName + "medium.jpg", (int)(size.Width * 0.8), (int)(size.Height * 0.8)); Thumbnail.MakeThumbnailImage(avatarFileName + "large.jpg", avatarFileName + "small.jpg", (int)(size.Width * 0.6), (int)(size.Height * 0.6)); } else { Thumbnail.MakeThumbnailImage(avatarFileName + "large.jpg", avatarFileName + "medium.jpg", (int)(size.Width * 0.5), (int)(size.Height * 0.5)); Thumbnail.MakeThumbnailImage(avatarFileName + "large.jpg", avatarFileName + "small.jpg", (int)(size.Width * 0.3), (int)(size.Height * 0.3)); } try { oldImage.Dispose(); System.IO.File.Delete(filePathName); } catch { } //当支持FTP上传头像时,使用FTP上传远程头像 if (FTPs.GetForumAvatarInfo.Allowupload == 1) { FTPs ftps = new FTPs(); string ftpAvatarFileName = string.Format("/avatars/upload/{0}/{1}/{2}/", uid.Substring(0, 3), uid.Substring(3, 2), uid.Substring(5, 2)); ftps.UpLoadFile(ftpAvatarFileName, avatarFileName + "large.jpg", FTPs.FTPUploadEnum.ForumAvatar); ftps.UpLoadFile(ftpAvatarFileName, avatarFileName + "medium.jpg", FTPs.FTPUploadEnum.ForumAvatar); ftps.UpLoadFile(ftpAvatarFileName, avatarFileName + "small.jpg", FTPs.FTPUploadEnum.ForumAvatar); } return(true); }