2017-04-06 66 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