开发者

How to convert an image file to BMP programmatically using native Windows XP capabilities?

开发者 https://www.devze.com 2023-01-14 06:14 出处:网络
Is it开发者_高级运维 possible to convert an image file to BMP format using WindowsXP\'s native libraries and scripting capabilities?

Is it开发者_高级运维 possible to convert an image file to BMP format using WindowsXP's native libraries and scripting capabilities?

I'm talking about WSH, JScript, VBS, etc...

C++ is also good for what I need if it can be compiled with Dev-C++


Yes, you can. Look at the Image class which is part of GDI+.


To convert images from scripts, you can use the WIA Automation Library. It's not strictly "native" library, but it's redistributable (refer to EULA).

Blow is a JScript example that shows how to convert an image to BMP. The original image can be PNG, GIF, JPEG or TIFF. Before running the script, register the wiaaut.dll library in the system.

var wiaIDUnknown = "{00000000-0000-0000-0000-000000000000}";
var wiaFormatBMP = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}";

// Load the original image
var img = new ActiveXObject("WIA.ImageFile");
img.LoadFile("D:\\MyFolder\\MyImage.gif");

switch (img.FormatID)
{
  case wiaIDUnknown:
    // Unknown image format or an invalid image file
    break;

  case wiaFormatBMP:
    // The image is already BMP
    break;

  default:
    // Specify the new format
    var ip  = new ActiveXObject("WIA.ImageProcess");
    ip.Filters.Add(ip.FilterInfos("Convert").FilterID);
    ip.Filters(1).Properties("FormatID").Value = wiaFormatBMP

    // Convert and save the image
    img = ip.Apply(img);
    img.SaveFile("D:\\MyFolder\\MyImage.bmp");
}

See also WIA documentation on MSDN.


I think from Windows 7 WIA DLL are provided with Windows. Otherwise you can download WIA

This is @Helen code converted to vbscript:

Const wiaIDUnknown = "{00000000-0000-0000-0000-000000000000}"
Const wiaFormatBMP = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}"

Sub die(msg)
    WScript.Echo(msg)
    WScript.Quit(1)
End Sub

'-----------------------------------------------------------------
' MAIN

Set args = WScript.Arguments
If args.Count <> 2 Then die "Usage: WIA_convert.vbs <filename_input> <filename_output>"

filename_in = args.Item(0)
filename_out    = args.Item(1)

Set img_in = CreateObject("WIA.ImageFile")
img_in.LoadFile filename_in
Wscript.Echo "Width  = " & img_in.Width
Wscript.Echo "Height = " & img_in.Height

Select Case img_in.FormatID
Case wiaIDUnknown
    Wscript.Echo "Unknown format"

Case wiaFormatBMP
    Wscript.Echo "Image is BMP"

Case Else
    Set ip  = CreateObject("WIA.ImageProcess")
    ip.Filters.Add(ip.FilterInfos("Convert").FilterID)
    ip.Filters(1).Properties("FormatID").Value = wiaFormatBMP

    Set img_out = ip.Apply(img_in)
    img_out.SaveFile(filename_out)
End Select
0

精彩评论

暂无评论...
验证码 换一张
取 消