Can I “draw”/create an image with a given text with powershell?

前端 未结 1 1936
有刺的猬
有刺的猬 2021-02-14 00:08

I just wondered if it would be possible to create a small, simple jpg, png, gif with a given Text in powershell:

e.g: a small square, 250px × 61px, yellow background and

1条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-14 00:16

    Sure, if you're on PowerShell 2.0 try this:

    Add-Type -AssemblyName System.Drawing
    
    $filename = "$home\foo.png" 
    $bmp = new-object System.Drawing.Bitmap 250,61 
    $font = new-object System.Drawing.Font Consolas,24 
    $brushBg = [System.Drawing.Brushes]::Yellow 
    $brushFg = [System.Drawing.Brushes]::Black 
    $graphics = [System.Drawing.Graphics]::FromImage($bmp) 
    $graphics.FillRectangle($brushBg,0,0,$bmp.Width,$bmp.Height) 
    $graphics.DrawString('Hello World',$font,$brushFg,10,10) 
    $graphics.Dispose() 
    $bmp.Save($filename) 
    
    Invoke-Item $filename  
    

    0 讨论(0)
提交回复
热议问题