好久没写东西了,今天写玩意来给大家看看: 远程上传图片 #region 远程上传图片public class RUpload...{ /**//// <summary> /// 生成缩略图 /// </summary> /// <param name="filePath"></param> /// <param name="fileName"></param> /// <param name="page">文件扩展名</param> /// <param name="picwidth"></param> /// <param name="picheight">缩略图高度</param> public void CreateSmallImg(string filePath, string fileName, Page page, int picwidth, int picheight) ...{ try ...{ Image image = Image.FromFile(page.Server.MapPath(filePath + "/" + fileName)); Image.GetThumbnailImageAbort callback = new Image.GetThumbnailImageAbort(this.ThumbnailCallback); image.GetThumbnailImage(picwidth, picheight, callback, new IntPtr()).Save(page.Server.MapPath(filePath + "/small_" + fileName)); image.Dispose(); } catch ...{ } } /**//// <summary> /// 添加文字水印效果 /// </summary> /// <param name="OldPath">上传地址</param> /// <param name="Path">保存地址</param> /// <param name="AddText">添加的文字</param> public void Fonts(string filePath, string fileName, Page page, string AddText) ...{ try ...{ Image image = Image.FromFile(page.Server.MapPath(filePath + "/" + fileName)); Image.GetThumbnailImageAbort callback = new Image.GetThumbnailImageAbort(this.ThumbnailCallback); Graphics graphics = Graphics.FromImage(image); graphics.DrawImage(image, 0, 0, image.Width, image.Height); Font font = new Font("Verdana", 16f); Brush brush = new SolidBrush(Color.White); graphics.DrawString(AddText, font, brush, (float)10f, (float)10f); graphics.Dispose(); image.Save(page.Server.MapPath(filePath + "/font_" + fileName)); image.Dispose(); } catch (Exception exception) ...{ } } /**//// <summary> /// 生成文件名 /// </summary> /// <returns></returns> private string GetFileName() ...{ string text = DateTime.Now.ToString("yyyymmddhhmmss").Replace(":", "").Replace("-", "").Replace(" ", ""); string text2 = this.RndNum(8); return (text + text2); } /**//// <summary> /// 远程保存缩略图的 /// </summary> /// <param name="strs"></param> /// <param name="oldFileName"></param> /// <param name="filePath"></param> /// <param name="sExt"></param> /// <param name="page">this.Page</param> /// <param name="picwidth"></param> /// <param name="picheight"></param> /// <returns></returns> private string RemoteSave(string strs, string oldFileName, string filePath, string sExt, Page page, int picwidth, int picheight) ...{ string[] textArray = oldFileName.Split(new char[] ...{ '|' }); string text2 = strs; for (int i = 0; i < textArray.Length; i++) ...{ string fileName = this.GetFileName() + "." + sExt; if (this.getHttpXmlContent(textArray.GetValue(i).ToString(), page, filePath, fileName)) ...{ text2 = text2.Replace(textArray.GetValue(i).ToString(), filePath + "/" + fileName); this.CreateSmallImg(filePath, fileName, page, picwidth, picheight); } } return text2; } /**//// <summary> /// 远程保存文字水印的 /// </summary> /// <param name="strs"></param> /// <param name="oldFileName"></param> /// <param name="filePath"></param> /// <param name="sExt"></param> /// <param name="page"></param> /// <param name="text"></param> /// <returns></returns> private string RemoteSave(string strs, string oldFileName, string filePath, string sExt, Page page, string text) ...{ string[] textArray = oldFileName.Split(new char[] ...{ '|' }); string text2 = strs; for (int i = 0; i < textArray.Length; i++) ...{ string fileName = this.GetFileName() + "." + sExt; if (this.getHttpXmlContent(textArray.GetValue(i).ToString(), page, filePath, fileName)) ...{ text2 = text2.Replace(textArray.GetValue(i).ToString(), filePath + "/" + fileName); this.Fonts(filePath, fileName, page, text); } } return text2; } /**//// <summary> /// 远程上传 /// </summary> /// <param name="strs">远程上传的源数据</param> /// <param name="page">this.Page</param> /// <param name="filePath">上传路径</param> /// <param name="fileExt">上传的文件扩展名,可用"|"分隔</param> /// <param name="picwidth">缩略图宽度</param> /// <param name="picheight">缩略图高度</param> /// <returns>文件名</returns> public string RemoteUploadFile(string strs, Page page, string filePath, string fileExt, int picwidth, int picheight) ...{ string[] textArray = fileExt.Split(new char[] ...{ '|' }); string oldFileName = ""; try ...{ for (int i = 0; i < textArray.Length; i++) ...{ oldFileName = this.ReplaceUrl(strs, textArray.GetValue(i).ToString()); strs = this.RemoteSave(strs, oldFileName, filePath, textArray.GetValue(i).ToString(), page, picwidth, picheight); } return strs; } catch ...{ return strs; } } public string RemoteUploadFileFont(string strs, Page page, string filePath, string fileExt, string text) ...{ string[] textArray = fileExt.Split(new char[] ...{ '|' }); string oldFileName = ""; try ...{ for (int i = 0; i < textArray.Length; i++) ...{ oldFileName = this.ReplaceUrl(strs, textArray.GetValue(i).ToString()); strs = this.RemoteSave(strs, oldFileName, filePath, textArray.GetValue(i).ToString(), page, text); } return strs; } catch ...{ return strs; } } /**//// <summary> /// 重写远程网址 /// </summary> /// <param name="strs"></param> /// <param name="filePath"></param> /// <param name="sExt"></param> /// <returns></returns> public string ReplaceRemoteSaveUrl(string strs, string filePath, string sExt) ...{ Regex regex = new Regex("((" + filePath + @")(S*/)((S)+[.]{1}(" + sExt + ")))", RegexOptions.IgnoreCase); string text2 = ""; int num = 0; for (Match match = regex.Match(strs); match.Success; match = match.NextMatch()) ...{ match.NextMatch(); if (num == 0) ...{ if (text2.IndexOf(match.Value) == -1) ...{ text2 = match.Value; } } else if (text2.IndexOf(match.Value) == -1) ...{ text2 = text2 + "|" + match.Value; } num++; } return text2; } /**//// <summary> /// 替换地址 /// </summary> /// <param name="strs"></param> /// <param name="sExt"></param> /// <returns></returns> private string ReplaceUrl(string strs, string sExt) ...{ Regex regex = new Regex(@"((http|https|ftp|rtsp|mms):(//|//){1}(([A-Za-z0-9_-])+[.]){1,}(net|com|cn|org|cc|tv|[0-9]{1,3})(S*/)((S)+[.]{1}(" + sExt + ")))", RegexOptions.IgnoreCase); string text2 = ""; int num = 0; for (Match match = regex.Match(strs); match.Success; match = match.NextMatch()) ...{ match.NextMatch(); if (num == 0) ...{ if (text2.IndexOf(match.Value) == -1) ...{ text2 = match.Value; } } else if (text2.IndexOf(match.Value) == -1) ...{ text2 = text2 + "|" + match.Value; } num++; } return text2; } /**//// <summary> /// 随机数的产生 /// </summary> /// <param name="VcodeNum"></param> /// <returns></returns> public string RndNum(int VcodeNum) ...{ string[] textArray = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z".Split(new char[] ...{ ',' }); string text2 = ""; int num = -1; Random random = new Random(); for (int i = 1; i < (VcodeNum + 1); i++) ...{ if (num != -1) ...{ random = new Random((i * num) * ((int)DateTime.Now.Ticks)); } int index = random.Next(0x23); if ((num != -1) && (num == index)) ...{ return this.RndNum(VcodeNum); } num = index; text2 = text2 + textArray[index]; } return text2; } private bool ThumbnailCallback() ...{ return false; } private bool getHttpXmlContent(string url, Page page, string filePath, string fileName) ...{ string text = ""; string path = filePath + "/" + fileName; try ...{ byte[] buffer = new byte[0]; WebResponse response = WebRequest.Create(url).GetResponse(); Stream responseStream = response.GetResponseStream(); if (!response.ContentType.ToLower().StartsWith("text/")) ...{ int count; byte[] buffer2 = new byte[0x400]; Stream stream2 = File.Create(page.Server.MapPath(path)); do ...{ count = responseStream.Read(buffer2, 0, buffer2.Length); if (count > 0) ...{ stream2.Write(buffer2, 0, count); } } while (count > 0); stream2.Close(); } else ...{ text = new StreamReader(responseStream, Encoding.Default).ReadToEnd(); StreamWriter writer = new StreamWriter(page.Server.MapPath(path)); writer.Write(text); writer.Flush(); writer.Close(); } responseStream.Close(); return true; } catch ...{ return false; } }}#endregion