2015-09-01 37 views
0

我想在一個文件夾中以不同大小的圖片水印。水印有3891 x 4118像素。我想要水印的圖片幾乎相同的大小或方式較低。在C#中調整大小和水印圖像

但是,水印圖像在圖像上應始終具有相同的大小。因此,我把我想要水印的圖像的寬度,乘以0.2(20%),水印圖像的高度由比率計算。 (見下面的代碼)。

之後,我調整了水印圖像的大小,並把它放在我想要水印的圖像上。到目前爲止這麼好,但問題是,圖像比它應該小得多。計算工作正常,即使我說,把圖像(3891 x 4118像素),它會正確計算它,但水印不會變大。它保持在我沒有cal size的尺寸上。

這是加載文件夾的按鈕。

private void DateiLaden_Click(object sender, RoutedEventArgs e) 
     { 
      FolderBrowserDialog fbd = new FolderBrowserDialog(); 
      DialogResult result = fbd.ShowDialog(); 


      try 
      { 
       string[] files = GetFiles(fbd.SelectedPath.ToString(), "*.jpg|*.jpeg"); //Only select JPG pictures, method below 

       int anzahlBilder = files.Length; 
       if (anzahlBilder <= 0) 
       { 
        System.Windows.Forms.MessageBox.Show("No images!"); 
       } // if there are no images, stop processing... 
       else 
       { 
        var res = checkImageSize(files); //check if they have a minmal size, method below 
        if (res == "") //if everythings fine, continue 
        { 
         Directory.CreateDirectory(fbd.SelectedPath.ToString() + "\\watermarked"); //create new directory of the selected folder, folder so save the watermarked images 


         System.Drawing.Image brand = System.Drawing.Image.FromFile("../../watermark.png"); //load watermark 
         double imageHeightBrand = Convert.ToDouble(brand.Height); //get height (4118px) 
         double imageWidthBrand = Convert.ToDouble(brand.Width); //get width (3891px) 
         double ratioBrand = imageWidthBrand/imageHeightBrand; //get ratio (0.94487615347) 

         foreach (var file in files) 
         { 
          System.Drawing.Image img = System.Drawing.Image.FromFile(file); //load image to watermerk to get width and height 

          double imageHeightBild = Convert.ToDouble(img.Height); //height of the image to watermark 
          double imageWidthBild = Convert.ToDouble(img.Width); //width of the image to watermark     

          //Landscape 
          if (imageWidthBild > 640.0 && imageHeightBild > 425.0) 
          { 
           var imageWidthTmpBranding = imageWidthBild * 0.2; //the watermark width, but only 20% size of the image to watermark 
           var imageHeightTmpBranding = imageWidthTmpBranding/ratioBrand; //height of watermark, preserve aspect ratio 

           int imageWidthBranding = Convert.ToInt32(imageWidthTmpBranding); //convert in into int32 (see method below) 
           int imageHeightBranding = Convert.ToInt32(imageHeightTmpBranding); //convert in into int32 (see method below) 


           var watermark = ResizeImage(brand, imageWidthBranding, imageHeightBranding); //resize temporally the watermark image, method below 

           System.Drawing.Image image = System.Drawing.Image.FromFile(file); //get image to watermark 
           Graphics g = Graphics.FromImage(image); //load is as graphic 
           g.DrawImage(watermark, new System.Drawing.Point(50, 50)); //draw the watermark on it 
           image.Save(fbd.SelectedPath.ToString() + "\\watermarked\\" + returnOnlyImageName(file)); //save it in the folder with the same file name, see method below. 
          } 

          //Portrait 
          /* if(imageWidthBild > 350 && imageHeightBild > 520) { 

          }*/ 

         } 
        } 
        else 
        { 
         System.Windows.Forms.MessageBox.Show(res); 
        } 

       } 

      } 
      catch (Exception ex) 
      { 
       System.Windows.Forms.MessageBox.Show(ex.Message); 
      } 


     } 

「的方法下面的」

private static string[] GetFiles(string sourceFolder, string filters) 
     { 
      return filters.Split('|').SelectMany(filter => System.IO.Directory.GetFiles(sourceFolder, filter)).ToArray(); 
     } 

     private static string checkImageSize(string[] files) 
     { 

      string message = "The following pictures are too small:\n"; 


      foreach (var file in files) 
      { 
       Bitmap img = new Bitmap(file); 

       var imageHeight = img.Height; 
       var imageWidth = img.Width; 

       if ((imageWidth < 640 && imageHeight < 425) || (imageWidth < 350 && imageHeight < 520)) 
       { 
        message += returnOnlyImageName(file) + "\n"; 
       } 
      } 

      if (message == "The following pictures are too small:\n") 
      { 
       return ""; 
      } 
      else 
      { 
       message += "\nPlease change those pictures!"; 
       return message; 
      } 

     } 

     private static string returnOnlyImageName(string file) 
     { 
      return file.Substring(file.LastIndexOf("\\")+1); 
     } 

     public static Bitmap ResizeImage(System.Drawing.Image image, int width, int height) 
     { 
      var destRect = new System.Drawing.Rectangle(0, 0, width, height); 
      var destImage = new Bitmap(width, height); 

      destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); 

      using (var graphics = Graphics.FromImage(destImage)) 
      { 
       graphics.CompositingMode = CompositingMode.SourceCopy; 
       graphics.CompositingQuality = CompositingQuality.HighQuality; 
       graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; 
       graphics.SmoothingMode = SmoothingMode.HighQuality; 
       graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; 

       using (var wrapMode = new ImageAttributes()) 
       { 
        wrapMode.SetWrapMode(WrapMode.TileFlipXY); 
        graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode); 
       } 
      } 

      return destImage; 
     } 

,是它無論如何也可以保存在右下角所有的水印?

感謝問候

回答

0

在這裏你去...

void waterMarkOnBottomRight(Image img, Image watermarkImage, string saveFileName) 
    { 
     double imageHeightBrand = Convert.ToDouble(watermarkImage.Height); 
     double imageWidthBrand = Convert.ToDouble(watermarkImage.Width); 
     double ratioBrand = imageWidthBrand/imageHeightBrand; 

     double imageHeightBild = Convert.ToDouble(img.Height); //height of the image to watermark 
     double imageWidthBild = Convert.ToDouble(img.Width); 
     var imageWidthTmpBranding = imageWidthBild * 0.2; //the watermark width, but only 20% size of the image to watermark 
     var imageHeightTmpBranding = imageWidthTmpBranding/ratioBrand; //height of watermark, preserve aspect ratio 
     int imageWidthBranding = Convert.ToInt32(imageWidthTmpBranding); //convert in into int32 (see method below) 
     int imageHeightBranding = Convert.ToInt32(imageHeightTmpBranding); 

     int watermarkX = (int)(imageWidthBild - imageWidthBranding); // Bottom Right 
     int watermarkY = (int)(imageHeightBild - imageHeightBranding); 

     using (Graphics g = Graphics.FromImage(img)) 
      g.DrawImage(watermarkImage, 
       new Rectangle(watermarkX, watermarkY, imageWidthBranding, imageHeightBranding), 
       new Rectangle(0, 0, (int)imageWidthBrand, (int)imageHeightBrand), 
       GraphicsUnit.Pixel); 
     img.Save(saveFileName); 
    } 

這一個對橫向(寬度>高度)的工作。

那麼..在你的代碼中,你不必爲圖像創建單獨的實例來計算,用於創建圖形,添加一個新的位圖和繪圖使用圖形和所有。你對圖像做任何事情,它在記憶中。它不會影響磁盤上的水印圖像。

+0

我用你的方法,它工作得很好!再次感謝!我在循環中調用方法,因爲我想爲整個文件夾加水印,對吧?如果該文件夾中有大約40張圖像,那麼一切正常,但可以說我想對大約100張圖像進行水印處理,我得到一個outOfMemoryException異常。解決方案:將應用程序切換到x64。但我也需要一個x86應用程序,你也知道這個問題的解決方案嗎? – Markus

+0

是的,這是一個非常簡單的解決方案 – Sayka

+0

不是100,但你甚至可以做1000或2000 – Sayka