写入临时文件
Writing temp files
我目前正在尝试在单击表单上的按钮时在图片框中显示共享点缩略图。似乎正在发生的事情是文件被锁定并且不会让我替换文件或任何东西。我什至创建了一个计数器,所以文件名总是不同的。当我 运行 第一次一切正常时,我相信它无法覆盖文件。我是不是做错了什么有更好的方法吗??
$User=GET-ADUser $UserName –properties thumbnailphoto
$Filename='C:\Support\Export'+$Counterz+'.jpg'
#$img = $Filename.Open( [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read )
[System.Io.File]::WriteAllBytes($Filename, $User.Thumbnailphoto)
$Picture = (get-item ($Filename))
$img = [System.Drawing.Image]::Fromfile($Picture)
$pictureBox.Width = $img.Size.Width
$pictureBox.Height = $img.Size.Height
$pictureBox.Image = $img
$picturebox.dispose($Filename)
Remove-Item $Filename
您应该能够在不创建临时文件的情况下执行此操作。
只需创建 $img
如:
$img = [System.Drawing.Image]::FromStream([System.IO.MemoryStream]::new($User.thumbnailPhoto))
$pictureBox.Width = $img.Width
$pictureBox.Height = $img.Height
$pictureBox.Image = $img
不要忘记在使用 $form.Dispose()
关闭后从内存中删除表格
如果您坚持使用临时文件,请注意 $img
对象会保留对该文件的引用,直到它被处理掉。
类似于:
# get a temporary file name
$Filename = [System.IO.Path]::GetTempFileName()
[System.IO.File]::WriteAllBytes($Filename, $User.thumbnailPhoto)
# get an Image object using the data from the temporary file
$img = [System.Drawing.Image]::FromFile($Filename)
$pictureBox.Width = $img.Width
$pictureBox.Height = $img.Height
$pictureBox.Image = $img
$form.Controls.Add($pictureBox)
$form.ShowDialog()
# here, when all is done and the form is no longer needed, you can
# get rid of the $img object that still has a reference to the
# temporary file and then delete that file.
$img.Dispose()
Remove-Item $Filename
# clean up the form aswell
$form.Dispose()
我目前正在尝试在单击表单上的按钮时在图片框中显示共享点缩略图。似乎正在发生的事情是文件被锁定并且不会让我替换文件或任何东西。我什至创建了一个计数器,所以文件名总是不同的。当我 运行 第一次一切正常时,我相信它无法覆盖文件。我是不是做错了什么有更好的方法吗??
$User=GET-ADUser $UserName –properties thumbnailphoto
$Filename='C:\Support\Export'+$Counterz+'.jpg'
#$img = $Filename.Open( [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read )
[System.Io.File]::WriteAllBytes($Filename, $User.Thumbnailphoto)
$Picture = (get-item ($Filename))
$img = [System.Drawing.Image]::Fromfile($Picture)
$pictureBox.Width = $img.Size.Width
$pictureBox.Height = $img.Size.Height
$pictureBox.Image = $img
$picturebox.dispose($Filename)
Remove-Item $Filename
您应该能够在不创建临时文件的情况下执行此操作。
只需创建 $img
如:
$img = [System.Drawing.Image]::FromStream([System.IO.MemoryStream]::new($User.thumbnailPhoto))
$pictureBox.Width = $img.Width
$pictureBox.Height = $img.Height
$pictureBox.Image = $img
不要忘记在使用 $form.Dispose()
如果您坚持使用临时文件,请注意 $img
对象会保留对该文件的引用,直到它被处理掉。
类似于:
# get a temporary file name
$Filename = [System.IO.Path]::GetTempFileName()
[System.IO.File]::WriteAllBytes($Filename, $User.thumbnailPhoto)
# get an Image object using the data from the temporary file
$img = [System.Drawing.Image]::FromFile($Filename)
$pictureBox.Width = $img.Width
$pictureBox.Height = $img.Height
$pictureBox.Image = $img
$form.Controls.Add($pictureBox)
$form.ShowDialog()
# here, when all is done and the form is no longer needed, you can
# get rid of the $img object that still has a reference to the
# temporary file and then delete that file.
$img.Dispose()
Remove-Item $Filename
# clean up the form aswell
$form.Dispose()