Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How can I download a picture in powershell and display it in C#?

How can I download a picture in powershell and display it in C#?

Scheduled Pinned Locked Moved C#
questioncsharpcomgraphicswindows-admin
6 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    turbosupramk3
    wrote on last edited by
    #1

    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]

    L 1 Reply Last reply
    0
    • T turbosupramk3

      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]

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Where does the error occur?

      T 1 Reply Last reply
      0
      • L Lost User

        Where does the error occur?

        T Offline
        T Offline
        turbosupramk3
        wrote on last edited by
        #3

        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

        L 1 Reply Last reply
        0
        • T turbosupramk3

          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

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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.

          T 1 Reply Last reply
          0
          • L Lost User

            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.

            T Offline
            T Offline
            turbosupramk3
            wrote on last edited by
            #5

            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

            T 1 Reply Last reply
            0
            • T turbosupramk3

              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

              T Offline
              T Offline
              turbosupramk3
              wrote on last edited by
              #6

              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;
              
              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups