将透明png转换为jpg powershell

将透明png转换为jpg powershell,powershell,Powershell,我正在尝试批量将一堆透明PNG转换为JPG,下面的鹅卵石powershell可以工作,但每当我转换所有图像时,都会显示黑色。我在这里试过答案 但是我得到的“使用”关键字不受支持(在模块中也找不到) 据我所知,你需要创建一个白色背景的新位图图形,并在上面绘制此图像,但就我而言,我不知道如何添加它 根据 您引用的答案是用C#编写的,而不是PowerShell。PowerShell没有保证对象处理的方法,这就是为什么会出现语法错误的原因。看。你刚才看到的帖子是使用c#。这是同样的事情,但是在Power

我正在尝试批量将一堆透明PNG转换为JPG,下面的鹅卵石powershell可以工作,但每当我转换所有图像时,都会显示黑色。我在这里试过答案 但是我得到的“使用”关键字不受支持(在模块中也找不到)

据我所知,你需要创建一个白色背景的新位图图形,并在上面绘制此图像,但就我而言,我不知道如何添加它

根据


您引用的答案是用C#编写的,而不是PowerShell。PowerShell没有保证对象处理的方法,这就是为什么会出现语法错误的原因。看。你刚才看到的帖子是
使用
c#。这是同样的事情,但是在PowerShell中调用相关的类和对象。
$files = Get-ChildItem "C:\Pictures\test" -Filter *.png -file -Recurse | 
foreach-object {

    $Source = $_.FullName
    $test = [System.IO.Path]::GetDirectoryName($source)
    $base= $_.BaseName+".jpg"
    $basedir = $test+"\"+$base
    Write-Host $basedir
    Add-Type -AssemblyName system.drawing
    $imageFormat = "System.Drawing.Imaging.ImageFormat" -as [type]
    $image = [drawing.image]::FromFile($Source)
    $image.Save($basedir, $imageFormat::jpeg)
}  
$files = Get-ChildItem "C:\Pictures\test" -Filter *.png -file -Recurse | 
foreach-object {

    $Source = $_.FullName
    $test = [System.IO.Path]::GetDirectoryName($source)
    $base= $_.BaseName+".jpg"
    $basedir = $test+"\"+$base
    Write-Host $basedir
    Add-Type -AssemblyName system.drawing
    $imageFormat = "System.Drawing.Imaging.ImageFormat" -as [type]
    $image = [drawing.image]::FromFile($Source)
    # $image.Save($basedir, $imageFormat::jpeg) Don't save here!

    # Create a new image
    $NewImage = [System.Drawing.Bitmap]::new($Image.Width,$Image.Height)
    $NewImage.SetResolution($Image.HorizontalResolution,$Image.VerticalResolution)

    # Add graphics based on the new image
    $Graphics = [System.Drawing.Graphics]::FromImage($NewImage)
    $Graphics.Clear([System.Drawing.Color]::White) # Set the color to white
    $Graphics.DrawImageUnscaled($image,0,0) # Add the contents of $image

    # Now save the $NewImage instead of $image
    $NewImage.Save($basedir,$imageFormat::Jpeg)
}