Esempio n. 1
0
 public static string[] SaveImageAs(this HttpPostedFile postedFile, CreateFolderMode mode, WaterMarkInfo waterMarkInfo, params ThumbnailInfo[] thumbnailInfoArray)
 {
     string[] vrtualFilePaths = new string[1 + thumbnailInfoArray.Length];
     byte[]   buffer          = new byte[postedFile.ContentLength];
     postedFile.InputStream.Read(buffer, 0, buffer.Length);
     if (FileValidator.ValidateImage(buffer))
     {
         HttpContext context = HttpContext.Current;
         string      vitualFilePath, physicalFilePath;
         Image       image = Image.FromStream(new MemoryStream(buffer));
         if (image.RawFormat.Guid == ImageFormat.Gif.Guid)
         {
             vitualFilePath   = GetFilePath(postedFile, ref mode, waterMarkInfo.SavePath);
             physicalFilePath = context.Server.MapPath(vitualFilePath);
             image.Save(physicalFilePath);
             if (waterMarkInfo.Mode != WaterMarkInfo.ExecuteMode.Skip)
             {
                 if (waterMarkInfo.Mode == WaterMarkInfo.ExecuteMode.Image)
                 {
                     using (Bitmap waterImg = waterMarkInfo.GetImage())
                     {
                         float x = image.Width - waterImg.Width - 24F, y = image.Height - waterImg.Height - 24F;
                         GifHelper.WaterMark(physicalFilePath, waterImg, x, y, physicalFilePath);
                     }
                 }
                 else
                 {
                     float x = image.Width - waterMarkInfo.TextSize - 24F, y = image.Height - waterMarkInfo.TextSize - 24F;
                     GifHelper.SmartWaterMark(physicalFilePath, waterMarkInfo.Text, ColorTranslator.FromHtml(waterMarkInfo.TextColor), new Font("宋体", waterMarkInfo.TextSize), x, y, physicalFilePath);
                 }
                 vrtualFilePaths[0] = vitualFilePath;
             }
             string srcPhysicalFilePath = physicalFilePath;
             for (int i = 0; i < thumbnailInfoArray.Length;)
             {
                 ThumbnailInfo thumbnailInfo = thumbnailInfoArray[i];
                 vitualFilePath   = GetFilePath(postedFile, ref mode, thumbnailInfo.SavePath);
                 physicalFilePath = context.Server.MapPath(vitualFilePath);
                 SizeF size = GDIHelper.GetProportionSize(new SizeF(thumbnailInfo.Width, thumbnailInfo.Height), image.Size);
                 GifHelper.GetThumbnail(srcPhysicalFilePath, (double)size.Width / (double)image.Width, physicalFilePath);
                 vrtualFilePaths[++i] = vitualFilePath;
             }
         }
         else
         {
             if (waterMarkInfo.Mode != WaterMarkInfo.ExecuteMode.Skip)
             {
                 vitualFilePath   = GetFilePath(postedFile, ref mode, waterMarkInfo.SavePath);
                 physicalFilePath = context.Server.MapPath(vitualFilePath);
                 if (waterMarkInfo.Mode == WaterMarkInfo.ExecuteMode.Image)
                 {
                     using (Bitmap waterImg = waterMarkInfo.GetImage())
                     {
                         GDIHelper.MakeWaterMark(image, waterImg, ContentAlignment.BottomRight, 24);
                     }
                 }
                 else
                 {
                     GDIHelper.MakeWaterMark(image, waterMarkInfo.Text, new Font("宋体", waterMarkInfo.TextSize), ColorTranslator.FromHtml(waterMarkInfo.TextColor), ContentAlignment.BottomRight, 24);
                 }
                 image.Save(physicalFilePath);
                 vrtualFilePaths[0] = vitualFilePath;
             }
             for (int i = 0; i < thumbnailInfoArray.Length;)
             {
                 ThumbnailInfo thumbnailInfo = thumbnailInfoArray[i];
                 vitualFilePath   = GetFilePath(postedFile, ref mode, thumbnailInfo.SavePath);
                 physicalFilePath = context.Server.MapPath(vitualFilePath);
                 Bitmap thumbnailImg = GDIHelper.GetThumbnailImage(image, new SizeF(thumbnailInfo.Width, thumbnailInfo.Height), thumbnailInfo.Mode);
                 thumbnailImg.Save(physicalFilePath);
                 vrtualFilePaths[++i] = vitualFilePath;
             }
         }
         image.Dispose();
     }
     return(vrtualFilePaths);
 }