Search code examples
c#.netpictureboxwia

Load a picturebox from a WIA ImageFile?


I've taken over a poorly designed project from a co-worker and am looking to load a picture box directly from the WIA command that just completed to take a picture from an attached USB camera.

The current implementation waits until the file has been written to disk, then displays it from there probably re-reading the file from disk.

Item item = d.ExecuteCommand(WIA.CommandID.wiaCommandTakePicture);

WIA.ImageFile imagefile = item.Transfer(FormatID.wiaFormatJPEG) as WIA.ImageFile;

I tried casting an Image to the given picture box without success

picBox.Image = (Image)imageFile;

Solution

  • WIA.ImageFile is a COM object wrapper, not a System.Drawing.Image.

    You'll have to mess with WIA.ImageFile Save method with temporary files or try in-memory solution as shown here: http://www.pcreview.co.uk/forums/wia-imagefile-system-drawing-image-t2321026.html

    var imageBytes = (byte[])image.FileData.get_BinaryData(); 
    var ms = new MemoryStream(imageBytes);
    var img = Image.FromStream(ms);