Load a picturebox from a WIA ImageFile?

2019-07-17 15:01发布

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;

1条回答
Juvenile、少年°
2楼-- · 2019-07-17 15:41

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);
查看更多
登录 后发表回答