1

URL からメモリに画像をダウンロードしようとしています。応答ストリームでエラーを取得しています。

"1" 個の引数を指定して "FromStream" を呼び出し中に例外が発生しました: "'null' の値は 'stream' には無効です。

Function Download-Image {
$req = [System.Net.WebRequest]::Create
("http://www.wired.com/images_blogs/business/2012/12/google.jpg")
    $response = $req.GetResponse()
    $ResponseStream = $Response.GetResponseStream()
    [System.IO.Stream]$stream = $ResponseStream
    #$response.close()
}

[byte[]]$image = Download-Image

Add-Type -AssemblyName System.Windows.Forms

$img = [System.Drawing.Image]::FromStream([System.IO.MemoryStream]$image)
[System.Windows.Forms.Application]::EnableVisualStyles()
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Width =  $img.Width
$pictureBox.Height =  $img.Height
$pictureBox.Image = $img

$form = new-object Windows.Forms.Form
$form.Width = $img.Width
$form.Height =  $img.Height
$form.AutoSize = $True
$form.AutoSizeMode = "GrowAndShrink"
$form.Icon = $icon
$form.controls.add($pictureBox)
$form.Add_Shown( { $form.Activate() } )
$form.ShowDialog()
4

1 に答える 1

0

割り当て後に関数から値を返す必要があります。

Function Download-Image {
$req = [System.Net.WebRequest]::Create
("http://www.wired.com/images_blogs/business/2012/12/google.jpg")
    $response = $req.GetResponse()
    $ResponseStream = $Response.GetResponseStream()
    [System.IO.Stream]$stream = $ResponseStream
    #$response.close()
    $req
}

あなたの場合、 $image 変数は $null の値を持っています:

[byte[]]$image = Download-Image

更新しました:

イメージをバイト配列としてダウンロードするには、この投稿のソリューションを使用できます。

var webClient = new WebClient();
byte[] imageBytes = webClient.DownloadData("http://www.google.com/images/logos/ps_logo2.png");
于 2015-09-24T07:21:49.580 に答える