Attempting to download an image from a URL into memory. Getting an error on the response stream.
Exception calling "FromStream" with "1" argument(s): "Value of 'null' is not valid for '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()
You need to return value from function after assignment:
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
}
In your case $image variable just have a value of $null:
[byte[]]$image = Download-Image
Updated:
To download image as byte array you can use solution from this post:
var webClient = new WebClient();
byte[] imageBytes = webClient.DownloadData("http://www.google.com/images/logos/ps_logo2.png");
Related
I've been struggling to display either OK.png / NOK.png on a specific column by testing if the path of the actual PV.pdf exists ,
$dataGrid = New-Object System.Windows.Forms.DataGridView
$dataGrid.Width = 503
$dataGrid.Height = 250
$dataGrid.location = new-object system.drawing.point(120,380)
$dataGrid.DataSource = $DataTable
$dataGrid.ReadOnly = $true
$dataGrid.RowHeadersVisible = $false
$dataGrid.AllowUserToAddRows = $false
$dataGrid.AutoSizeColumnsMode = 'Fill'
$ImageColumn = New-Object System.Windows.Forms.DataGridViewImageColumn
$ImageColumn.Width = 40
$dataGrid.Columns.Insert($dataGrid.Columns.Count, $ImageColumn)
$dataGrid.Columns[$dataGrid.Columns.Count - 1].HeaderText = "PV"
$dataGrid.Columns[$dataGrid.Columns.Count - 1].Name = "ImageColumn"
$OKImage = [System.Drawing.Image]::FromFile([string]$curDir+"\images\OK.png")
$NOKImage = [System.Drawing.Image]::FromFile([string]$curDir+"\images\NOK.png")
for($i=0;$i -lt $dataGrid.RowCount;$i++) {
Write-Host $dataGrid.Rows[$i].Cells[1].Value
$dir = [string]$curDir+"\pvs\"+[string]$dataGrid.Rows[$i].Cells[1].Value+".pdf"
if ((Test-Path $dir) -eq $true) {
$dataGrid.Rows[$i].Cells[0].Value = $OKImage
} else {
$dataGrid.Rows[$i].Cells[0].Value = $NOKImage
}
}
$dataGrid.Refresh()
i get this when i run the script :
Screenshot
Please help
i was expecting to display the OK.png (20x20) when the file is found and NOK.png when the file is not found .
I wrote these code for upload files through laravel 6.
But I only success for upload one file,even I am sure I got all files while uploading by check from dd($request->files)
in the begining the $index = 0 and in the end it change to 1,first round is ok but the it didn't continue to second run.
I don't know why,please help! thank you~
if($request->hasFile('files')){
$index = 0;
foreach($request->files as $key=>$file){
$originalName = $file[$index]->getClientOriginalName();
$size = $file[$index]->getClientSize();
$ext = $file[$index]->getClientOriginalExtension();
$newName = date('Ymd').mt_rand(100,999).$originalName;
$savePath = '/attachments/'.$newName;
$movePath = base_path().'/public/attachments/'.$newName;
$file[$index]->move(base_path().'/public/attachments',$newName);
$attachment = new attachment();
$attachment->bulletin_id = $bulletin->id; //already got this value before
$attachment->original_name = $originalName;
$attachment->name = $newName;
$attachment->type = $file[$index]->getClientMimeType();
$attachment->path = $savePath;
$attachment->size = ($size/1000);
$attachment->save();
++$index;
}
}
You try this way and it is simple
foreach($request->file('files') as $key=>$file){
$originalName = $file->getClientOriginalName();
$size = $file->getClientSize();
$ext = $file->getClientOriginalExtension();
$newName = date('Ymd').mt_rand(100,999).$originalName;
$savePath = '/attachments/'.$newName;
$movePath = base_path().'/public/attachments/'.$newName;
$file->move(base_path().'/public/attachments',$newName);
$attachment = new attachment();
$attachment->bulletin_id = $bulletin->id; //already got this value before
$attachment->original_name = $originalName;
$attachment->name = $newName;
$attachment->type = $file->getClientMimeType();
$attachment->path = $savePath;
$attachment->size = ($size/1000);
$attachment->save();
}
I have created an application in powershell, and now i need to add the GUI to the app.
In this application I need to give the user the opportunity to select or write the path of a folder in a textbok. I have written this piece of code but i haven't achieved to get the output of calling the get-FolderLocation function in the texbox that i create.
Any ideas about how to achieve this?
[void][System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)
[void][System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)
#####Define the form size & placement
$form = New-Object “System.Windows.Forms.Form”;
$form.Width = 500;
$form.Height = 150;
$form.Text = $title;
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
##############Define text label1
$textLabel1 = New-Object “System.Windows.Forms.Label”;
$textLabel1.Left = 25;
$textLabel1.Top = 15;
$textLabel1.Text = "select the folder";
############Define text box1 for input
$textBox1 = New-Object “System.Windows.Forms.TextBox”;
$textBox1.Left = 150;
$textBox1.Top = 10;
$textBox1.width = 200;
$textBox1.Text = "selected folder"
#############define select button
$button = New-Object “System.Windows.Forms.Button”;
$button.Left = 360;
$button.Top = 85;
$button.Width = 100;
$button.Text = “Browse”;
############# the output of calling the get-Folder Location function must be shown in the textbox1
$button.Add_Click({get-Folderlocation}) ;
#############Add controls to all the above objects defined
$form.Controls.Add($button);
$form.Controls.Add($textLabel1);
$form.Controls.Add($textBox1);
$form.ShowDialog();
$textBox1.Text
$selectedDirectory
function get-Folderlocation([string]$Message, [string]$InitialDirectory, [switch]$NoNewFolderButton)
{
$browseForFolderOptions = 0
if ($NoNewFolderButton) { $browseForFolderOptions += 512 }
$app = New-Object -ComObject Shell.Application
$folder = $app.BrowseForFolder(0, $Message, $browseForFolderOptions, $InitialDirectory)
if ($folder) { $selectedDirectory = $folder.Self.Path } else { $selectedDirectory = '' }
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($app) > $null
return $selectedDirectory
}
You simply assign the output from get-FolderLocation to $textBox1.Text.
Since the $textBox1 variable is not in the same scope as the add_Click() scriptblock, PowerShell 3.0 and 4.0 will have issues resolving it. Use Get-Variable -Scope 1 to work around this:
$button.Add_Click({(Get-Variable -Name textBox1 -Scope 1).Value.Text = Get-Folderlocation})
I am trying to get some dynamic array values into a PowerShell windows Form. When I use the same command on the PowerShell console it works but in form I get a weird .dot net class returned. Below I mark the code that results in wrong output.
Import-Module NetAdapter
$nics = Get-NetAdapter | Select-Object name, interfacedescription,macaddress
function GenerateForm
{
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
$frmVMTask = New-Object System.Windows.Forms.Form
$groupBox1 = New-Object System.Windows.Forms.GroupBox
$btnCancel = New-Object System.Windows.Forms.Button
# $btnOK = New-Object System.Windows.Forms.Button
$ChkVMTask = New-Object System.Windows.Forms.CheckedListBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
$handler_ChkVMTask_itemCheckChanged =
{
$VMTask = $ChkVMTask.CheckedItems
If ($ChkVMTask.CheckedItems.count -gt 1)
{
$buttons = [system.windows.forms.messageboxbuttons]::ok;
[System.Windows.Forms.MessageBoxDefaultButton]::Button1;
$icon = [system.windows.forms.messageboxicon]::error;
$tasks = [System.Windows.Forms.MessageBox]::Show("
############################
# Too Many VM Tasks are selected. #
#############################`n
Please Select 1 item only.
"
, "VM Task Error Summary", $buttons, $icon)
}
else
{
$VMTask = $ChkVMTask.CheckedItems
}
}
{
# Close the form if Cancel clicked
$frmVMTask.close()
}
$groupBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 16
$System_Drawing_Point.Y = 16
$groupBox1.Location = $System_Drawing_Point
$groupBox1.Name = "groupBox1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 940
$System_Drawing_Size.Width = 800
$groupBox1.Size = $System_Drawing_Size
$groupBox1.TabIndex = 0
$groupBox1.TabStop = $False
$groupBox1.Text = "NIC Automation Task"
$groupBox1.add_Enter($handler_groupBox1_Enter)
$frmVMTask.Controls.Add($groupBox1)
$btnCancel.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 280
$System_Drawing_Point.Y = 430
$btnCancel.Location = $System_Drawing_Point
$btnCancel.Name = "btnCancel"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 75
$btnCancel.Size = $System_Drawing_Size
$btnCancel.TabIndex = 15
$btnCancel.Text = "Cancel"
$btnCancel.UseVisualStyleBackColor = $True
$btnCancel.add_Click($handler_btnCancel_Click)
$groupBox1.Controls.Add($btnCancel)
$groupBox1.Controls.Add($lblTask)
$ChkVMTask.[Windows.Forms.SelectionMode]::One;
$ChkVMTask.CheckOnClick = $True;
$ChkVMTask.DataBindings.DefaultDataSourceUpdateMode = 0
$ChkVMTask.FormattingEnabled = $True
foreach ($nic in $nics)
{
Below is where output is code and not same as get-netadapter
$ChkVMTask.Items.Add("$Nic") | Out-Null
}
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 20
$System_Drawing_Point.Y = 54
$ChkVMTask.Location = $System_Drawing_Point
$ChkVMTask.Name = "ChkVMTask"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 158
$System_Drawing_Size.Width = 722
$ChkVMTask.Size = $System_Drawing_Size
$ChkVMTask.TabIndex = 1
$ChkVMTask.Tag = ""
$ChkVMTask.add_SelectedIndexChanged($handler_ChkVMTask_itemCheckChanged)
$groupBox1.Controls.Add($ChkVMTask)
#Save the initial state of the form
$InitialFormWindowState = $frmVMTask.WindowState
#Init the OnLoad event to correct the initial state of the form
$frmVMTask.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$frmVMTask.ShowDialog() | Out-Null
$frmVMTask.close() #new parent test close visible and hidden work perfectly. close not yet.
} #End Function
#Call the Function
GenerateForm
I try use this:
$image = new JImage();
$image->loadFile($item->logo);
$image->resize('208', '125');
$properties = JImage::getImageFileProperties($item->logo);
echo $image->toFile(JPATH_CACHE . DS . $item->logo, $properties->type);
But not work =\ any idea?
Try this out:
// Set the path to the file
$file = '/Absolute/Path/To/File';
// Instantiate our JImage object
$image = new JImage($file);
// Get the file's properties
$properties = JImage::getImageFileProperties($file);
// Declare the size of our new image
$width = 100;
$height = 100;
// Resize the file as a new object
$resizedImage = $image->resize($width, $height, true);
// Determine the MIME of the original file to get the proper type for output
$mime = $properties->mime;
if ($mime == 'image/jpeg')
{
$type = IMAGETYPE_JPEG;
}
elseif ($mime == 'image/png')
{
$type = IMAGETYPE_PNG;
}
elseif ($mime == 'image/gif')
{
$type = IMAGETYPE_GIF;
}
// Store the resized image to a new file
$resizedImage->toFile('/Absolute/Path/To/New/File', $type);