title: Convert byte array to picture (c# => VB6)
body: <p>I have the following code in C# that converts a given byte array (FF 3C FF ...etc) to a Bitmap.</p>
  
  <p>Can you help me to get the VB6 version of this code?</p>
  
  <p>I have already tried the methods in here:</p>
  
  <p><a href="https://stackoverflow.com/questions/43040177/base64-string-to-byte-to-image">base64 string to byte to image</a></p>
  
  <p><a href="http://vb.mvps.org/samples/NetCam/" rel="nofollow noreferrer">http://vb.mvps.org/samples/NetCam/</a></p>
  
  <p><a href="https://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=29004&amp;lngWId=1" rel="nofollow noreferrer">https://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=29004&amp;lngWId=1</a></p>
  
  <p>But I cannot make it work. I try to load directly the picture into a picturebox or wirte it to .bmp file, but the result is not a picture...</p>
  
  <p>Thank you</p>
  
  <pre><code>public static Bitmap ByteArrayToBitmap(byte[] data, int width, int height)
          {
  
              Bitmap bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format1bppIndexed);
              // Prepare a locked image memory area
              System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, bmp.PixelFormat);
              // Move our data in
              System.Runtime.InteropServices.Marshal.Copy(data, 0, bmpData.Scan0, data.Length);
              bmp.UnlockBits(bmpData);
  
              return bmp;
          }
  </code></pre>
  
  <p>UPDATE:
  This is my VB6 code:</p>
  
  <pre class="lang-vb prettyprint-override"><code>Private Sub UpdatePictureBox
  Dim buff(1023) as byte
  
  ' fill the byte array ...
  
  Set PictureLCD.Picture = PictureFromBits(buff)
  
  End Sub
  
  Public Function PictureFromBits(abPic() As Byte) As IPicture  ' not a StdPicture!!
          Dim nLow As Long
          Dim cbMem  As Long
          Dim hMem  As Long
          Dim lpMem  As Long
          Dim IID_IPicture As GUID
          Dim istm As stdole.IUnknown '  IStream
          Dim ipic As IPicture
  
          ' Get the size of the picture's bits
  
  100     nLow = LBound(abPic)
  
  110     cbMem = (UBound(abPic) - nLow) + 1
  
          ' Allocate a global memory object
  120     hMem = GlobalAlloc(GMEM_MOVEABLE, cbMem)
  
  130     If hMem Then
  
              ' Lock the memory object and get a pointer to it.
  140         lpMem = GlobalLock(hMem)
  
  150         If lpMem Then
  
                  ' Copy the picture bits to the memory pointer and unlock the handle.
  160             MoveMemory ByVal lpMem, abPic(nLow), cbMem
  170             Call GlobalUnlock(hMem)
  
                  ' Create an ISteam from the pictures bits (we can explicitly free hMem
                  ' below, but we'll have the call do it...)
  180             If (CreateStreamOnHGlobal(hMem, CTrue, istm) = S_OK) Then
  190                 If (CLSIDFromString(StrPtr(sIID_IPicture), IID_IPicture) = S_OK) Then
  
                          ' Create an IPicture from the IStream (the docs say the call does not
                          ' AddRef its last param, but it looks like the reference counts are correct..)
  200                     Call OleLoadPicture(ByVal ObjPtr(istm), cbMem, CFalse, _
                                  IID_IPicture, PictureFromBits)
  
                      End If   ' CLSIDFromString
                  End If   ' CreateStreamOnHGlobal
              End If   ' lpMem
  
              '    Call GlobalFree(hMem)
          End If   ' hMem
  
      End Function
  </code></pre>
  
  <p>Code taken from here:
  <a href="http://www.vbforums.com/showthread.php?287495-load-a-picture-box-control-from-memory" rel="nofollow noreferrer">http://www.vbforums.com/showthread.php?287495-load-a-picture-box-control-from-memory</a></p>
  
  <p>I have checked and byte array is filled correctly, but I see no picture in the picture box...</p>
  
  <p>UPDATE 2:
  Byte array: </p>
  
  <blockquote class="spoiler">
    <p> FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF EF BF FF FF FF FF FF FF FF FF FF FF FF FF FF FF E7 3F FF FF FF FF FF FF FF FF FF FF FF FF FF FF E2 31 C7 12 53 1F FF FF FF FF FF FF FF FF FF FF E0 24 F2 72 42 4F FF FF FF FF FF FF FF FF FF FF E5 20 C2 12 4E 0F FF FF FF FF FF FF FF FF FF FF E7 27 93 92 4E 7F FF FF FF FF FF FF FF FF FF FF E7 30 C2 38 4F 0F FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF F9 3F FF FF FF FF FF FF FF FF FF FF FF FF FF FF F9 3F FF FF FF FF FF FF 80 00 7F FE 07 FF FC 3E 18 3F FF FF FF FF FF FF 80 00 7F F8 01 FF FC 3E 49 3F FF FF FF FF FF FF 80 00 7F F0 00 FF F8 3E 49 3F FF FF FF FF FF FF 80 00 7F E0 00 7F F8 3E 19 3F FF FF FF FF FF FF 80 00 7F E0 00 7F E0 3E 79 3F FF FF FF FF FF FF FF F0 7F C0 F0 3F 00 3E 7F FF FF FF FF FF FF FF FF E0 FF C1 F8 3F 00 3F FF FF FF FF FF FF FF FF FF E0 FF C1 F8 3F 00 3F FF FF FF FF FF FF FF FF FF C1 FF C1 F8 3F 08 3F FF FF FF FF FF FF FF FF FF C1 FF C1 F8 3F F8 3F FF FF FF FF FF FF FF FF FF 83 FF C1 F8 3F F8 3F FF FF FF FF FF FF FF FF FF 83 FF C1 F8 3F F8 3F FF FF FF FF FF FF FF FF FF 07 FF C1 F8 3F F8 3F FF FF FF FF FF FF FF FF FF 07 FF C1 F8 3F F8 3F FF FF FF FF FF FF FF FF FE 0F FF C1 F8 3F F8 3F FF FF FF FF FF FF FF FF FE 0F FF C1 F8 3F F8 3F FF FF FF FF FF FF FF FF FC 1F FF C1 F8 3F F8 3F FF FF FF FF FF FF FF FF FC 1F FF C1 F8 3F F8 3F FF FF FF FF FF FF FF FF F8 3F FF C1 F8 3F F8 3F FF FF FF FF FF FF FF FF F8 3F FF C1 F8 3F F8 3F FF FF FF FF FF FF FF FF F0 3F FF C1 F8 3F F8 3F FF 00 00 01 FF FF FF FF F0 7F FF C1 F8 3F F8 3F FF 06 7C C1 FF FF FF FF F0 7F FF C1 F8 3F F8 3F FF 09 11 21 FF FF FF FF E0 7F FF C0 F0 3F F8 3F FF 09 11 01 FF FF FF FF E0 FF F1 E0 00 7F F8 3F FF 0F 11 01 FF FF FF FF E0 FF E0 E0 00 7F F8 3F FF 09 11 01 FF FF FF FF C0 FF E0 F0 00 FF F8 3F FF 09 11 21 FF FF FF FF C1 FF E0 F8 01 FF F8 3F FF 09 10 C1 FF FF FF FF C1 FF F1 FE 07 FF F8 3F FF 00 00 01 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF C0 E0 07 F8 1F 73 FF FF FF FF FF FF FF FF FF FF 80 60 07 F0 0E AD FF FF FF FF FF FF FF FF FF FF 00 20 07 E0 07 6F FF FF FF FF FF FF FF FF FF FF 0C 23 FF E1 87 EF FF FF FF FF FF FF FF FF FF FF 1E 22 1F E3 C7 EF FF FF FF FF FF FF FF FF FF FF FE 20 0F E3 C7 ED FF FF FF FF FF FF FF FF FF FF FC 20 07 E3 C7 F3 FF FF FF FF FF FF FF FF FF FF F8 21 87 E3 C7 FF FF FF FF FF FF FF FF FF FF FF F0 63 C7 E3 C7 FF FF FF FF FF FF FF FF FF FF FF E0 FF C7 E3 C7 FF FF FF FF FF FF FF FF FF FF FF C1 FF C7 E3 C7 FF FF FF FF FF FF FF FF FF FF FF 83 E7 C7 E3 C7 FF FF FF FF FF FF FF FF FF FF FF 07 E3 86 61 87 FF FF FF FF FF FF FF FF FF FF FF 00 20 04 20 07 FF FF FF FF FF FF FF FF FF FF FF 00 30 0C 30 0F FF FF FF FF FF FF FF FF FF FF FF 00 38 1E 78 1F FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 00</p>
  </blockquote>