Question

I'm using this code to acquire the scanned image from WIA:

const
  wiaFormatJPEG = '{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}';
  wiaFormatPNG = '{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}';
var
  CommonDialog: ICommonDialog;
  AImage: IImageFile;
  i: Integer;
begin
  CommonDialog := CreateOleObject('WIA.CommonDialog') as ICommonDialog;

  for i := 1 to Scanner.Properties.Count do
  begin
    if (Scanner.Properties[i].Name = 'Horizontal Resolution') or
      (Scanner.Properties[i].Name = 'Vertical Resolution') then
      Scanner.Properties[i].Set_Value(72)
    else if Scanner.Properties[i].Name = 'Horizontal Extent' then
      Scanner.Properties[i].Set_Value(Round(8.27 * 72))
    else if Scanner.Properties[i].Name = 'Vertical Extent' then
      Scanner.Properties[i].Set_Value(Round(11.00 * 72));
  end;
  AImage := IUnknown(CommonDialog.ShowTransfer(Scanner, wiaFormatPNG, True)) as IImageFile;
  //Save the image
  AImage.SaveFile('D:\1.' + AImage.FileExtension);
  imgImage.Picture.LoadFromFile('D:\1.' + AImage.FileExtension);
  DeleteFile('D:\1.' + AImage.FileExtension);
end;

Scanner is initialized using this code:

Scanner := DevMgr.DeviceInfos[Integer(cbWIASource.Items.Objects[cbWIASource.ItemIndex])].Connect.Items[1];

And DevMgr and cbWIASource are initialized using this code:

DevMgr := CreateOleObject('WIA.DeviceManager') as IDeviceManager;
for i := 1 to DevMgr.DeviceInfos.Count do
    for j := 1 to DevMgr.DeviceInfos[i].Properties.Count do
      if DevMgr.DeviceInfos[i].Properties[j].Name = 'Name' then
      begin
        cbWIASource.Items.AddObject(DevMgr.DeviceInfos[i].Properties[j].Get_Value, TObject(i));
        Break;
      end;

I was wondering if there is a way to copy the scanned document without first saving it to the disk. I read on MSDN that I can access ARGBData member of ImageFile to access pixel data, but is there a simple way to copy the entire image from FileData to TBitmap? For instance, can I use a TMemoryStream?


Just as an update, I found this example on MSDN. I know nothing about VB, but I guess the Picture object is a wrapper around HBITMAP. So, is it logical to conclude that the ImageFile.Picture property is what I need?

Was it helpful?

Solution

IImageFile has a property FileData with provides access to the binary image data, via IVector.BinaryData

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top