title: Saving Image into memory stream Error
body: <p>I'm trying to:</p>
  
  <p>1) Scan an image and insert it to a memorystream.</p>
  
  <p>2) Show it in picturebox1.</p>
  
  <p>3) Read picturebox1 image into memorystream and save it to oledb.</p>
  
  <p>here's my code :</p>
  
  <p>1st) Scanning by WIA Dialog and inserting image to memorystream then showing it in picturebox1:</p>
  
  <pre><code> Try
  
     Dim CD As New WIA.CommonDialog
  
     Dim F As WIA.ImageFile = CD.ShowAcquireImage(WIA.WiaDeviceType.ScannerDeviceType)
  
     If F IsNot Nothing Then
  
              Dim MStream As IO.MemoryStream = Nothing
              Try
                  'Convert the raw scanner output into a byte array
                  Dim ImageBytes() As Byte = DirectCast(F.FileData.BinaryData, Byte())
                  'Read the image data bytes into a MemoryStream
                  MStream = New IO.MemoryStream(ImageBytes)
                  PictureBox1.Image = System.Drawing.Image.FromStream(MStream)
              Catch ex As Exception
                  MsgBox("An error occurred:  " &amp; ex.Message, MsgBoxStyle.Critical)
              End Try
              If MStream IsNot Nothing Then MStream.Dispose()
     End If
  
    Catch ex As Exception
        MsgBox(ex.message, MsgBoxStyle.Critical)
    End Try
  </code></pre>
  
  <p>This is working perfectly.</p>
  
  <p>The problem is coming,</p>
  
  <p>2nd) Reading image from picturebox1 into new memorystream then save it to oledb:</p>
  
  <pre><code>If conn.State = ConnectionState.Closed Then
          conn.ConnectionString = connstring
          conn.Open()
  
              'Reading image from picturebox1 and then insert it to a new memorystream.
              Dim arrImage() As Byte
              Dim myMs As New IO.MemoryStream
              PictureBox1.Image.Save(myMs, System.Drawing.Imaging.ImageFormat.Jpeg)
              arrImage = myMs.GetBuffer
  
              'Now save image to oledb from the memorystream.
              Dim query As String = "INSERT INTO Guestinfo ([IDImage]) VALUES (@IDImage)"
              Dim command As New OleDbCommand
              With command
                  .CommandText = query
                  .Parameters.Add("@IDImage", OleDb.OleDbType.Binary).Value = arrImage
                  .Connection = conn
                  .ExecuteNonQuery()
              End With
              MsgBox("Saved Successfully!", MsgBoxStyle.Information)
              conn.Close()
              ListView1.Items.Clear()
              loadlistview()
              Me.Close()
   End If
  </code></pre>
  
  <p>Error is on this line :</p>
  
  <pre><code>PictureBox1.Image.Save(myMs, System.Drawing.Imaging.ImageFormat.Jpeg)
  </code></pre>
  
  <p>I might see that this error because during the scanning process we were converting the image into byte array to put it into memorystream, but while reading process we were trying to read the picturebox image as Jpeg into memorystream.</p>
  
  <p>I can see it's complicated for me as a beginner in visualbasic any help would be appreciated.</p>