请求解决WIA.Vector对象调用ImageFile属性后像素透明度丢失问题(Alpha值失效)
I get exactly what you're dealing with—you're trying to render an ellipse on a transparent background using WIA.Vector in VBScript, but the saved BMP keeps showing an opaque white background instead of transparency. The core issue here is that the WIA.Vector object defaults to a 24-bit RGB pixel format (which doesn't support alpha channels), so even though you're setting alpha=0 for your background color, that alpha data gets discarded when converting to an ImageFile.
Fix Breakdown
The solution boils down to one critical adjustment: telling WIA to use a 32-bit ARGB pixel format (which preserves the alpha channel) for your Vector object. Here's how to implement it:
Modified VBScript Code
Const width = 500 Const height = 500 ' WIA constant for 32-bit ARGB (enables alpha channel support) Const WIA_PIXEL_FORMAT_32BPP_ARGB = &H26000A color_transparent = GetARGB(0, 255, 255, 255) ' Alpha=0 (transparent), RGB=white color_black = GetARGB(255, 0, 0, 0) PI = 4 * Atn(1) Dim oVector, oImageFile Set oVector = NewBlankPage() rasterizeEllipse oVector, 220, 120, 200, 100, color_black Set oImageFile = oVector.ImageFile(width, height) ' Optional debug line to confirm alpha is enabled WScript.StdOut.WriteLine "IsAlphaPixelFormat: " & oImageFile.IsAlphaPixelFormat oImageFile.SaveFile "ellipse_transparent.bmp" WScript.StdOut.WriteLine "Done! Press Enter to quit." WScript.StdIn.SkipLine Function NewBlankPage() Dim oVector, i WScript.StdOut.WriteLine "Creating a new blank page... Please wait..." Set oVector = CreateObject("WIA.Vector") ' Critical: Set pixel format to 32-bit ARGB to retain alpha data oVector.PixelFormat = WIA_PIXEL_FORMAT_32BPP_ARGB For i = 1 To (width * height) oVector.Add color_transparent Next Set NewBlankPage = oVector End Function Function getPointOnEllipse(cx, cy, rx, ry, d) Dim theta theta = d * Sqr(2 / (rx * rx + ry * ry)) ' Approximation for ellipse points Dim point(1) point(0) = Fix(cx + Cos(theta) * rx) point(1) = Fix(cy - Sin(theta) * ry) getPointOnEllipse = point End Function Function getEllipsePerimeter(rx, ry) getEllipsePerimeter = Fix(PI * Sqr(2 * (rx * rx + ry * ry))) End Function Sub SetPixel(oVector, x, y, color) x = x + 1 y = y + 1 If x > width Or x < 1 Or y > height Or y < 1 Then Exit Sub oVector(x + (y - 1) * width) = color End Sub Sub rasterizeEllipse(oVector, cx, cy, rx, ry, color) Dim perimeter, i WScript.StdOut.WriteLine "Rendering ellipse..." perimeter = getEllipsePerimeter(rx, ry) For i = 0 To (perimeter - 1) Dim point point = getPointOnEllipse(cx, cy, rx, ry, i) SetPixel oVector, point(0), point(1), color Next End Sub Function Get1ByteHex(val) Dim s s = Hex(val) Do While Len(s) < 2 s = "0" & s Loop Get1ByteHex = Right(s, 2) End Function Function GetARGB(a, r, g, b) Dim s s = "&h" & Get1ByteHex(a) & Get1ByteHex(r) & Get1ByteHex(g) & Get1ByteHex(b) GetARGB = CLng(s) End Function
What Changed & Why
- Added
WIA_PIXEL_FORMAT_32BPP_ARGB: This hex value corresponds to WIA's 32-bit ARGB pixel format, which is required to store alpha channel data. - Set
oVector.PixelFormat: By assigning this property right after creating the Vector, we ensure every pixel added to the object includes alpha information instead of discarding it. - Debug line: The
IsAlphaPixelFormatcheck will now returnTrue, confirming the ImageFile has an active alpha channel.
Testing the Result
Run the modified script with cscript.exe, then use your existing HTA file to test. The ellipse should now sit directly on the blue background without any white square behind it. Note that some desktop image viewers might ignore 32-bit BMP transparency, but web-based viewers like the HTA's <img> tag will recognize it correctly.
内容的提问来源于stack exchange,提问作者Javad Bayat

