/**/ /// <summary> /// 生成缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> public static MemoryStream MakeThumbnail(string originalImagePath, int width, int height, string mode) { WebClientX xx = new WebClientX(); System.Drawing.Image originalImage = new Bitmap(width, height); try { FileInfo fi = new FileInfo(originalImagePath); if (fi.Exists) { originalImage = System.Drawing.Image.FromStream(fi.OpenRead()); } else { originalImage = Image.FromStream(xx.OpenRead(originalImagePath)); } } catch { originalImage = Image.FromStream(xx.OpenRead(originalImagePath)); } System.IO.MemoryStream ms = new System.IO.MemoryStream(); int towidth = width; int toheight = height; int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height; if (ow < towidth && oh < toheight) { originalImage.Save(ms, ConvertImageFormat(originalImagePath)); } else { switch (mode.ToUpper()) { case "HW": //指定高宽缩放(可能变形) break; case "W": //指定宽,高按比例 toheight = originalImage.Height * width / originalImage.Width; break; case "H": //指定高,宽按比例 towidth = originalImage.Width * height / originalImage.Height; break; case "CUT": //指定高宽裁减(不变形) if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = (originalImage.Width - ow) / 2; } else { ow = originalImage.Width; oh = originalImage.Width * height / towidth; x = 0; y = (originalImage.Height - oh) / 2; } break; case "AUTO": //自动适应高度 if (ow > oh) { //newwidth = 200; toheight = (int)((double)oh / (double)ow * (double)towidth); } else { //newheight = 200; towidth = (int)((double)ow / (double)oh * (double)toheight); } break; default: break; } //进行缩图 Bitmap img = new Bitmap(towidth, toheight); img.SetResolution(72f, 72f); //img.MakeTransparent(); Graphics gdiobj = Graphics.FromImage(img); gdiobj.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.Default; gdiobj.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default; gdiobj.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; gdiobj.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; gdiobj.FillRectangle(new SolidBrush(Color.White), 0, 0, towidth, toheight); Rectangle destrect = new Rectangle(0, 0, towidth, toheight); gdiobj.DrawImage(originalImage, destrect, 0, 0, ow, oh, GraphicsUnit.Pixel); System.Drawing.Imaging.EncoderParameters ep = new System.Drawing.Imaging.EncoderParameters(1); ep.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100); System.Drawing.Imaging.ImageCodecInfo ici = GetEncoderInfo("image/jpeg"); try { if (ici != null) { img.Save(ms, ici, ep); } else { img.Save(ms, ConvertImageFormat(originalImagePath)); } } catch (System.Exception e) { throw e; } finally { gdiobj.Dispose(); img.Dispose(); } } originalImage.Dispose(); return(ms); }
/// <summary> /// 导入图片到媒体库 /// </summary> /// <param name="PictureUrl">图片地址</param> /// <returns></returns> public String ImportPicture(String PictureUrl) { String Picture = ""; if (!String.IsNullOrEmpty(PictureUrl)) { //查看该图片是否已经存在过 KeyValueEntity PictureTemp = ImportPictureList.Find(r1 => r1.Key == PictureUrl); if (PictureTemp != null && !String.IsNullOrEmpty(PictureTemp.Key)) { Picture = PictureTemp.Value.ToString(); } else { DNNGo_DNNGalleryProGame_Files PhotoItem = new DNNGo_DNNGalleryProGame_Files(); //将图片的URL转换为相应的文件名,文件后缀等内容 PhotoItem.FileName = System.IO.Path.GetFileName(PictureUrl);//文件名 “Default.aspx” if (!String.IsNullOrEmpty(PhotoItem.FileName) && PhotoItem.FileName.IndexOf(".") > 0) { PhotoItem.FileExtension = System.IO.Path.GetExtension(PhotoItem.FileName).Replace(".", ""); PhotoItem.Name = PhotoItem.FileName.Replace(System.IO.Path.GetExtension(PictureUrl), ""); //判断哪些文件需要被下载 if (NeedExtension(PhotoItem.FileExtension)) { String FullFile = String.Format("{0}DNNGalleryProGame\\uploads\\{1}\\{2}\\{3}\\{4}", DNNGalleryProGame_PortalSettings.HomeDirectoryMapPath, DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, PhotoItem.FileName); FileInfo file = new FileInfo(FullFile); int ExistsCount = 1; //如果有重复的文件,需要改变文件名 while (file.Exists) { PhotoItem.FileName = String.Format("{0}_{1}.{2}", PhotoItem.Name, ExistsCount, PhotoItem.FileExtension); FullFile = String.Format("{0}DNNGalleryProGame\\uploads\\{1}\\{2}\\{3}\\{4}", DNNGalleryProGame_PortalSettings.HomeDirectoryMapPath, DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, PhotoItem.FileName); file = new FileInfo(FullFile); ExistsCount++; } try { if (!file.Directory.Exists) { file.Directory.Create(); } //下载图片 WebClientX web = new WebClientX(); web.DownloadFile(new Uri(PictureUrl), FullFile); file = new FileInfo(FullFile); if (file.Exists) { PhotoItem.ModuleId = ModuleID; PhotoItem.PortalId = DNNGalleryProGame_PortalSettings.PortalId; PhotoItem.FileSize = Convert.ToInt32(file.Length / 1024); PhotoItem.FileMate = FileSystemUtils.GetContentType(Path.GetExtension(PhotoItem.FileName).Replace(".", "")); PhotoItem.Status = (Int32)EnumFileStatus.Approved; PhotoItem.FilePath = String.Format("DNNGalleryProGame/uploads/{0}/{1}/{2}/{3}", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, PhotoItem.FileName); try { if (("png,gif,jpg,jpeg,bmp").IndexOf(PhotoItem.FileExtension) >= 0) { //图片的流 System.Drawing.Image image = System.Drawing.Image.FromFile(file.FullName); PhotoItem.ImageWidth = image.Width; PhotoItem.ImageHeight = image.Height; PhotoItem.Exif = Common.Serialize <EXIFMetaData.Metadata>(new EXIFMetaData().GetEXIFMetaData(image)); } } catch { } PhotoItem.LastTime = DateTime.Now; PhotoItem.LastIP = WebHelper.UserHost; //PhotoItem.LastUser = DNNGalleryProGame_PortalSettings.UserInfo.UserID; PhotoItem.ID = PhotoItem.Insert(); //返回图片的路径 Picture = String.Format("MediaID={0}", PhotoItem.ID); ImportPictureList.Add(new KeyValueEntity(PictureUrl, Picture)); } } catch (Exception ex) { } } else { Picture = PictureUrl; } } else { Picture = PictureUrl; } } } return(Picture); }