2017-04-06 124 views
0

如何從VBA中的文件加載的圖像創建並保存縮略圖?基於字符串從文件中加載的圖像創建並保存縮略圖

  1. 加載圖像到內存中,它定義路徑的文件,圖像
  2. 製作縮略圖保存該圖像文件夾爲JPG

我知道如何在C#寫。我試圖爲此目的創建一個外部的exe文件,但不知何故,當我嘗試通過VBA Shell運行它時,它不起作用,但是如果我通過Windows資源管理器正常傳遞參數,它就可以工作。

這是ThumbMaker的完整的C#代碼:

using System; 
using System.Drawing; 

class Program 
{ 
    static void Main(string[] args) 
    { 
     if (args.Length == 2) 
     { 
      string inputPath = args[0]; 
      string outputPath = args[1]; 
      try 
      { 
       Bitmap b = (Bitmap)Bitmap.FromFile(@inputPath); 
       b.GetThumbnailImage(160, 160, null, IntPtr.Zero).Save(outputPath); 
       Console.WriteLine(inputPath); 
       Console.WriteLine(outputPath); 
       Console.WriteLine(b.Height); 
       Console.ReadLine(); 

      } 
      catch 
      { 
       Console.WriteLine("Something went wrong, probably input file could not be converted to bitmap"); 
       Console.ReadLine(); 
      } 
     } 
     else 
     { 
      Console.WriteLine("You must run this program with 2 parameters"); 
      Console.WriteLine("1. is inputPath , 2. is outputPath"); 
      Console.ReadLine(); 
     } 
    } 
} 

這基本上是我需要的東西翻譯成VBA:

Bitmap b = (Bitmap)Bitmap.FromFile(@inputPath); 
b.GetThumbnailImage(160, 160, null, IntPtr.Zero).Save(outputPath); 

這VBA嘗試不工作:

Private Sub UploadButton_Click() 

    Dim strProgramName As String 
    Dim strArgument1 As String 
    Dim strArgument2 As String 

    strProgramName = Initialization.ImagesPath & "ThumbnailMaker.exe" 
    strArgument1 = PathTextBox.Text 
    'strArgument2 = ImageID & "_img" & ImageNumber & ".jpg" 
    strArgument2 = "newIMG.jpg" 

    Call Shell("""" & strProgramName & """ """ & strArgument1 & """ """ & strArgument2 & """", 1) 

End Sub 

回答

0

以及假設你正在嘗試使用外部程序創建縮略圖,我只想去做這件事:

Process.Start("C:/Path/ThumbnailMaker.exe", "Your Arguments for the external 
Program here i.e. the filepath: Initialization.ImagesPath") 

然後如果你可以用你的第三方程序指定文件的輸出名稱,你可以也只是使用該參數,然後搶了文件,因爲你已經得到了名字。否則,如果本程序出於任何原因使用隨機輸出文件名,請使用systemfilewatch並獲取文件名。

+2

VBA沒有'Process.Start()'。它使用一個稱爲[**'Shell()'**]的函數(https://msdn.microsoft.com/en-us/library/office/gg278437.aspx)。 –

+0

我的不好,我一定和標籤混淆了,他們被編輯了,先是vb.net – Alex

相關問題