How can I download a picture in powershell and display it in C#?
-
I've tried code similar to what is below, I keep getting an error that says a parameter is missing and when I trace the error it appears the parameter name is "message" and it is blank. How can I download an image in powershell, keep it in memory (not save it to a file) and then display it in a picturebox in c#? I see this error mentioned 100's of times on the net and no one has an answer? [code]
byte[] pictureByteArray = new byte[0];
object pictureObject = new object();
Collection<PSObject> results = powershellExchange.Invoke();
foreach (PSObject result in results)
{
//MessageBox.Show(result.Properties["PictureData"].ToString());
pictureObject = (result.Properties["PictureData"].Value);
pictureByteArray = Encoding.ASCII.GetBytes(result.Properties["PictureData"].ToString());
//pictureByteArray = BinaryReader} byte\[\] originalCoverData; BinaryFormatter bf = new BinaryFormatter(); using (var ms = new MemoryStream()) { bf.Serialize(ms, pictureObject); originalCoverData = ms.ToArray(); } //http://stackoverflow.com/questions/3801275/how-to-convert-image-in-byte-array/16576471#16576471 //string imageUrl = "http://www.gazzetta.it/Media/Foto/2007/04/17/1900384--346x212.jpg"; //originalCoverData = new System.Net.WebClient().DownloadData(imageUrl); Image x = null; x = byteArrayToImage(originalCoverData); System.IO.MemoryStream stream = new System.IO.MemoryStream(originalCoverData); pbxDisplayPicture.Image = x; System.Drawing.Image img2 = System.Drawing.Image.FromStream(stream); pbxDisplayPicture.Image = img2;
[/code] [code]
public Image byteArrayToImage(byte[] byteArrayIn)
{
using (MemoryStream mStream = new MemoryStream(byteArrayIn))
{
return Image.FromStream(mStream);
}
}[/code]
-
I've tried code similar to what is below, I keep getting an error that says a parameter is missing and when I trace the error it appears the parameter name is "message" and it is blank. How can I download an image in powershell, keep it in memory (not save it to a file) and then display it in a picturebox in c#? I see this error mentioned 100's of times on the net and no one has an answer? [code]
byte[] pictureByteArray = new byte[0];
object pictureObject = new object();
Collection<PSObject> results = powershellExchange.Invoke();
foreach (PSObject result in results)
{
//MessageBox.Show(result.Properties["PictureData"].ToString());
pictureObject = (result.Properties["PictureData"].Value);
pictureByteArray = Encoding.ASCII.GetBytes(result.Properties["PictureData"].ToString());
//pictureByteArray = BinaryReader} byte\[\] originalCoverData; BinaryFormatter bf = new BinaryFormatter(); using (var ms = new MemoryStream()) { bf.Serialize(ms, pictureObject); originalCoverData = ms.ToArray(); } //http://stackoverflow.com/questions/3801275/how-to-convert-image-in-byte-array/16576471#16576471 //string imageUrl = "http://www.gazzetta.it/Media/Foto/2007/04/17/1900384--346x212.jpg"; //originalCoverData = new System.Net.WebClient().DownloadData(imageUrl); Image x = null; x = byteArrayToImage(originalCoverData); System.IO.MemoryStream stream = new System.IO.MemoryStream(originalCoverData); pbxDisplayPicture.Image = x; System.Drawing.Image img2 = System.Drawing.Image.FromStream(stream); pbxDisplayPicture.Image = img2;
[/code] [code]
public Image byteArrayToImage(byte[] byteArrayIn)
{
using (MemoryStream mStream = new MemoryStream(byteArrayIn))
{
return Image.FromStream(mStream);
}
}[/code]
-
Sorry for the delay, it always tells me "Parameter is not valid" for mStream
public Image byteArrayToImage(byte[] byteArrayIn)
{
using (MemoryStream mStream = new MemoryStream(byteArrayIn))
{
return Image.FromStream(mStream);
}
}The particular byte array in question is 8716 items, so it seems like the byte array is valid
-
Sorry for the delay, it always tells me "Parameter is not valid" for mStream
public Image byteArrayToImage(byte[] byteArrayIn)
{
using (MemoryStream mStream = new MemoryStream(byteArrayIn))
{
return Image.FromStream(mStream);
}
}The particular byte array in question is 8716 items, so it seems like the byte array is valid
-
turbosupramk3 wrote:
it seems like the byte array is valid
Are you sure it contains a valid image? The error message suggests that it does not.
No I am not, do you have a suggestion on another way I can check? The code under the Exception snapshot, always has the same error message with the Parameter is not valid at
Quote:
at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData) at System.Drawing.Image.FromStream(Stream stream) at Updator.PictureUpdater.btnViewPicture_Click(Object sender, EventArgs e) in c:\Users\xxxx\Documents\Visual Studio 2012\Projects\Updator\Updator\Form1.cs:line 245 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at Updator.Program.Main() in c:\Users\xxx\Documents\Visual Studio 2012\Projects\Exchange Picture Updator\Updator\Program.cs:line 19 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, O
-
No I am not, do you have a suggestion on another way I can check? The code under the Exception snapshot, always has the same error message with the Parameter is not valid at
Quote:
at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData) at System.Drawing.Image.FromStream(Stream stream) at Updator.PictureUpdater.btnViewPicture_Click(Object sender, EventArgs e) in c:\Users\xxxx\Documents\Visual Studio 2012\Projects\Updator\Updator\Form1.cs:line 245 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at Updator.Program.Main() in c:\Users\xxx\Documents\Visual Studio 2012\Projects\Exchange Picture Updator\Updator\Program.cs:line 19 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, O
I figured it out, I had to build my own byte array and not use memory stream. Memory stream was corrupting the file and making it an additional 28 bytes. I'm not sure why this wasn't on the internet, as it seems 100's of people encounter this? Thanks for pointing me in the right direction.
byte[] imgBytes = null;
List<byte> imgBytesList = new List<byte>();
object imgObj = null;
Image img = null;
foreach (PSObject obj in exResults)
{
imgObj = obj.Properties["PictureData"].Value;
}foreach (byte byt in (dynamic)imgObj) { imgBytesList.Add(byt); } imgBytes = imgBytesList.ToArray(); using (var ms = new MemoryStream(imgBytes)) { img = Image.FromStream(ms); } pbxDisplayPicture.Image = img;