如何将扫描图像从WIA ImageFile传输到TBitmap而不将其保存到磁盘?

时间:2012-10-28 12:19:36

标签: delphi delphi-xe2 wia

我正在使用此代码从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

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

使用此代码初始化DevMgrcbWIASource

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;

我想知道是否有办法复制扫描的文档而不先将其保存到磁盘。我在MSDN上看到我可以访问ARGBData的{​​{1}}成员来访问像素数据,但有一种简单的方法可以将整个图像从ImageFile复制到FileData吗?例如,我可以使用TBitmap吗?


就像更新一样,我在MSDN上找到了this example。我对VB一无所知,但我想TMemoryStream对象是Picture的包装器。那么,可以合理地断定HBITMAP属性是我需要的吗?

1 个答案:

答案 0 :(得分:1)

IImageFile有一个属性FileData,通过IVector.BinaryData提供对二进制图像数据的访问